Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 9th, 2003, 07:06 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Char Error In Code - Compiling but strange results

Hi All,

I'm writing a DLL that will store (from VB) a machine name then share that between other methods in the DLL - but i've run into a problem in that I can share int types but the machine name is a problem - when i try to retrieve a set value i get a series of char such as /?> and if i try again I get , - this is consistent regardless of the data i send. If I set the machine name in the declarion or within the method (SET_CORBA_SERVER) It Works - but not if i store it then call it from another procedure (Ints work all the time).

Any suggestions people can make will be more than welcome - it's one of those silly little problems.

The code for the DLL is:

#include "stdafx.h"
#include "string.h"

char *CORBASERVERNAME;
char *CORBAPORTNUMBER;
int testdata;

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}

int _stdcall GET_SECONDS_FROM_SERVER(int serverSensorID)
{
return testdata;
}

int _stdcall SET_SECONDS_ON_SERVER(int serverSensorID , int updatedSecondsLevel )
{
    testdata = updatedSecondsLevel;
}

int _stdcall SET_CORBA_SERVER(char *newPort)
{
    CORBASERVERNAME = newPort;
}

char* GET_CORBA_SERVER()
{
    return CORBASERVERNAME;
}


Reply With Quote
  #2 (permalink)  
Old July 17th, 2003, 11:38 AM
Registered User
 
Join Date: Jul 2003
Location: Costa Mesa, CA, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem is that you are storing pointers to memory over which you have no control in CORBASERVERNAME and CORBAPORTNUMBER. The value received by SET_CORBA_SERVER() points to the contents of a VB string that is probably re/deallocated very soon after the function returns.

You must store a copy of what this function receives. The simplest solution is to change CORBASERVERNAME and CORBAPORTNUMBER to fixed length buffers
char CORBASERVERNAME[xx]; //'xx' and 'yy' are suitably generous sizes
char CORBAPORTNUMBER[yy];
and do
lstrcpyn(CORBASERVERNAME, newPort, sizeof(CORBASERVERNAME) - 1);
in SET_CORBA_SERVER(). (Use lstrcpyn(), not strncpy() for this. lstrcpyn() always null-terminates the string it copies, strncpy() doesn't.)

I also recommend that you declare newPort to be const char* to clarify that it is not written through by SET_CORBA_SERVER(). For a similar reason the return value of GET_CORBA_SERVER() should be const char* too.

-Evan Burkitt
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
Compiling JAVA code in .NET? sivavenugopal BOOK: Beginning ASP.NET 1.0 1 January 24th, 2007 01:52 AM
Error in source code compiling angelika J2EE 2 December 29th, 2003 11:45 AM



All times are GMT -4. The time now is 03:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.