Results 1 to 7 of 7

Thread: Howto return a String from c++ to vb

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    3

    Howto return a String from c++ to vb

    Hi,

    I've tryed to return a string from my own made dll to visual basic but i'm unsuccesful doing this.

    Does anybody know how to do this?

    This is my code(this does'nt work!)

    void DBSOCK_API test(LPSTR returnStr){

    char buffer[]="1234 testing";
    strcpy(returnStr,buffer);
    }


    and in visual basic:

    Declare Sub test Lib "dbSock" (ByVal st As String)


    Private Sub Command1_Click()
    Dim myString
    test (myString)
    MsgBox myString
    End Sub


    The message box displayed nothing

    Does anybody know what I'm doing wrong

    note that I'm using Microsoft eMbedded Visual Tools.

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    3
    Anybody alive here?
    maybe i'll try to order some pizza. That always helps

  3. #3
    Zaei
    Guest
    I dont actually know the answer, but I can give you some info to perhaps help you out.

    VB strings are Unicode, which means that each character uses two bytes. The C++ equivalent is a BSTR.

    You can typecast to to an LPSTR from a BSTR to get standard 1 byte characters.

    Z.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Actually, BSTR is nominally for OLE. For normal C++, you want LPWSTR, or in real terms, wchar_t* (as opposed to char*).

    www.parksie.uklinux.net/StringDLL.zip (I think it's still there).

    VB will pass an ANSI string to a DLL (or a pointer to one if you're filling it from the DLL), unless you specifically convert it to Unicode using StrConv(..., vbUnicode, ...).
    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

  5. #5
    Zaei
    Guest
    ::is enlightened:: Thanks parksie (again =).

    Z.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    3
    Some good advice, thank you


    I've got some something working. If you send a pointer to an array of unsigned shorts it works. This function does the trick:

    <code>

    void charToShort (unsigned short *outStr,char *inStr,int strLen)
    {
    int counter = 0;
    for(counter=0;counter < strLen;counter++)
    {
    outStr[counter] = inStr[counter];
    }
    //terminate the string with a 0
    outStr[counter]=0;
    }


    unsigned short DBSOCK_API *test (void){

    unsigned short tmpBuffer[255];

    charToShort(tmpBuffer,"1234",4);
    return tmpBuffer;
    }
    </code>
    Last edited by Chris_CJM; Nov 17th, 2001 at 06:54 PM.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you want to convert char* to wchar_t*, then use the MultiByteToWideChar function from the Windows API, or wcsmbcs (it's something like that, I can't remember) from the C standard library.

    Don't just try and convert the number values.
    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