Results 1 to 6 of 6

Thread: Dll function returning char*

  1. #1
    graphite
    Guest

    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

  2. #2
    Destined Soul
    Guest
    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:
    1. Dim myStr As String * 16   ' 16 chars long
    2. GetString myStr
    3. 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

  3. #3
    graphite
    Guest
    But what should i return in my C function?
    use char* ?

    Graphite

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    declare the return value as char*, and in vB "As String", I think it should work, or at least it works with parameters.

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width