Results 1 to 8 of 8

Thread: Strings & DLLs

  1. #1
    Destined Soul
    Guest

    Strings & DLLs

    Hi everyone.

    I'm somewhat new to the area of DLLs, so I hope this doesn't seem too trivial.

    I made a test DLL in C++ that outputs a character pointer (char*). I'm wondering if anyone knows how I could access this string in visual basic?

    in C++, I have something like:

    extern "C" __declspec (dllexport) __stdcall char* myFunc()
    {
    char* myPointer = new char[32];
    myPointer = "Hello World";
    return myPointer;
    }

    I know how to pass VB strings INTO a C++ dll, but I don't know how to retrieve them. Does anyone know how to do this?

    Thanx in advance,

    Destined

  2. #2
    denniswrenn
    Guest
    Public Declare Function MyFunc Lib "dllname.dll" As String

    MsgBox MyFunc

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's a huge memory leak.

    http://www.parksie.net/StringDLL.zip
    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

  4. #4
    Destined Soul
    Guest
    True, parksie, it is a pretty big memory leak. It's been a long time since I've used C++, so my skills are quite rusty.

    However, looking over your program, you seem to just modify the character pointer passed to the function - it doesn't return anything.

    My C++ code is:

    extern "C" __declspec (dllexport) __stdcall char* ReturnHello()
    {
    char* NewString = "hello world";
    return NewString;
    }

    and my VB Code is:

    Private Declare Function ReturnHello Lib "c:\test\DLL2\blah1.dll" Alias "ReturnHello@0" () As String

    Private Sub Command1_Click()
    Dim myStr As String
    myStr = ReturnHello
    MsgBox myStr
    End Sub

    The program crashes when I run it.

    I'm using the Bloodshed C++ program/compiler & VB 6.0 Professional. I'm not sure if the C++ compiler is the issue, though.

    Destined

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    char* NewString = "hello world"; 
    return NewString;
    You can't guarantee the data will still exist (although it probably will ). My method is the same one as used in GetWindowText. The client provides a buffer and a length which the data is copied into, which is exactly the same effect as returning the data by value, if it weren't for VB and C++ having totally different string semantics.
    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

  6. #6
    Destined Soul
    Guest
    Ah.. okay. Now I see what you mean. Everytime VB tries to access this memory that has already been unallocated it crashes. It's too bad VB doesn't have a better implementation of pointers.

    However, I think your earlier trick will work. Thanx parksie.

    Destined

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    I post this question before, yet haven't found an answer for it. Hope can get somethings from here...

    My question is, how to write a CALLBACK function? So straight forward

    regards,

  8. #8
    jim mcnamara
    Guest
    For code sample on static callback try:

    http://msdn.microsoft.com/library/de...mpcallback.asp

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