|
-
Aug 22nd, 2001, 02:53 PM
#1
Dll function returning char*
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
-
Aug 22nd, 2001, 03:49 PM
#2
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:
VB Code:
Dim myStr As String * 16 ' 16 chars long
GetString myStr
MsgBox "myStr = " & myStr
As 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
-
Aug 22nd, 2001, 03:58 PM
#3
But what should i return in my C function?
use char* ?
Graphite
-
Aug 23rd, 2001, 08:39 AM
#4
declare the return value as char*, and in vB "As String", I think it should work, or at least it works with parameters.
-
Aug 23rd, 2001, 08:52 AM
#5
Frenzied Member
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.
Harry.
"From one thing, know ten thousand things."
-
Aug 23rd, 2001, 10:10 AM
#6
Monday Morning Lunatic
www.parksie.net/StringDLL.zip 
Probably a bit different to what you need, but it might give an idea.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|