Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 9th, 2003, 07:06 AM
Registered User
 
Join Date: Jul 2003
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
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





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





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