Results 1 to 6 of 6

Thread: returning a string c++ to vb

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    returning a string c++ to vb

    I'm pretty sure this has been asked before, but I couldn't find it when I searched for it. How do I return a string the same way any other windows api function does it (such as GetSystemDirectory)?

    I need to pass a buffer to the dll and have the dll fill the buffer. How can I do this in vc++?

    (As you can probably tell, I'm just learning c++, but I have a strong knowledge of vb)

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Well, have your function with it asking for a count and a char[]. Then, you can simply loop through and add your data from 0 to count - 2. Then, at count - 1, simply add the \0 at the end (string terminator). In any case, return the number of characters in the string. It would be called like this:
    VB Code:
    1. Private Declare Function MyFunct Lib "MyLib.dll" (ByVal Count As Long, ByRef Str As String) As Long
    2.  
    3. Function GetFunct() As String
    4. Dim Q As String
    5. Dim Ret As Long
    6.     Q = Space(255)
    7.     Ret = MyFunct(255, Q)
    8.     GetFunct = Left(Q, Ret)
    9. End Function
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    Actually, I meant the other side of the Declare. In c++, how would I create the function.

    in MyLib.cpp...

    int __stdcall Myfunct(int Count, ??? Str)
    {
    //...

    return sizeof(Str);
    }

    what type should I use in place of the ??? in the above example

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    i modified a previous post that returned the string as the result of the function (i.e. 'return <string>;') to try to get this to work right. here is the source code of both the dll and the vb. this now causes "Bad Dll calling convention".

    i have a .def file for the dll that exports 'test'

    c++
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    extern "C" __declspec(dllexport) int test(int nCount, BSTR lpTesting)
    {
        char *tmp = new char[nCount];
        memset(tmp, ' ', nCount);
        sprintf(tmp, "testing testing testing");
        lpTesting = SysAllocStringByteLen(tmp, strlen(tmp));
        return sizeof(lpTesting);
    }
    vb
    Code:
    Option Explicit
    Declare Function test Lib "f:\vs\VC98\MyProjects\ID3\Debug\ID3.dll" (ByVal nCount As Long, ByVal lpReturn As String) As String
    
    Sub t()
        Dim a As String
        a = Space(255)
        a = Left(a, test(255, a))
        Debug.Print a
    End Sub
    
    Sub main()
        t
    End Sub

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Well- you're no longer using STDCALL. VB can only use STDCALL functions.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This has indeed been asked very often.

    You need a def file. You can't use sizeof(lpTesting) to determine the length of the string.

    This function will simply allocate memory, the let the pointer stray. There are at least 3 threads that explain this well. Search this forum for "BSTR".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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