PDA

Click to See Complete Forum and Search --> : Dll function returning char*


graphite
Aug 22nd, 2001, 02:53 PM
Dear all C++ and VB Expert,

i'm writing a DLL function using VC++ which returns a char*.

I declared local variable char * ReturnString. But when i use the sprintf function, it gives me an access violation.

//*********** DLL ***************
char * __stdcall GetString (){
char * ReturnString ;
sprintf(ReturnString,"%02x",144);

return ReturnString;
}

//*********** Test.exe ***********
void main(){

char * L_Add;

L_Add = GetString(); //<====== error after going in

printf("%s",L_Add);
}

'*******************************
I know it's the problem of the memory location but not sure how to fix it. Please direct me to a right position.

Also, this "GetString" function is intended to be use in a VB project. Thus another question has raised. Can i return a char * in my DLL so that VB can pick it up as string?

Any help will be greatly appreciated.


Graphite

Destined Soul
Aug 22nd, 2001, 03:49 PM
I'm not sure about C++ calling the DLL, but VB can't really handle a char* returned to it. From my experience before, the only way you can return a string from a DLL into VB is by first allocating the memory for the string in VB and then have the DLL copy the "returning" string into the one passed to the function.

ie:Dim myStr As String * 16 ' 16 chars long
GetString myStr
MsgBox "myStr = " & myStrAs for C++, I would try using the new statement and see if that works, but not forgetting to call delete after you're done with the returned string. I'm not sure if this will work, though...

Destined

graphite
Aug 22nd, 2001, 03:58 PM
But what should i return in my C function?
use char* ?

Graphite

CornedBee
Aug 23rd, 2001, 08:39 AM
declare the return value as char*, and in vB "As String", I think it should work, or at least it works with parameters.

HarryW
Aug 23rd, 2001, 08:52 AM
I think (I may be wrong) you need to make both strings (in both VB and C++) fixed-length strings. A good common length is 256 characters, for instance. So in C++ use this:

char ReturnString[256]

and in VB use this:

Dim ReturnString As String * 256


The problem you are having with the sprintf() call is that when you declare ReturnString as char*, you are just declaring a pointer. Although you have this pointer, you can't treat it as a pointer to a string because you haven't reserved any memory to place the string data into. You need to declare ReturnString as a char array, as indicated above. This will reserve 256 bytes of memory on the stack for your string to occupy, instead of just 4 bytes for a pointer and no more. Alternatively you could use new or malloc() to reserve some memory on the heap for your string, and continue using a pointer.

Basically, use char ReturnString[256] and you shouldn't have to worry any more about it unless your string gets longer than 255 characters.

parksie
Aug 23rd, 2001, 10:10 AM
www.parksie.net/StringDLL.zip :)

Probably a bit different to what you need, but it might give an idea.