Link to home
Start Free TrialLog in
Avatar of adamwj
adamwj

asked on

Using Unmanaged C++ DLL in VB.NET With Pointers

I have source written in C++ as an example that I'm trying to port to VB.NET.  It uses a 3rd party DLL that I do not have the source from.

This is the declaration for the function in C++
char* rcg(const char *src_file, char* return_buf, int size_retbuf);

Here is my declaration
 Private Declare Function rcg Lib "rcg.dll" Alias "rcg" (ByVal src_file As String, ByVal return_buf As IntPtr, ByVal size_returnbuf As Integer) As IntPtr

This is a snippet of the code I am trying to duplicate in C++
      #define RETURN_BUFFER_SIZE 24
        char lpszFileName[] = "r.rcg";
      char lpszReturnBuffer[RETURN_BUFFER_SIZE] = {0};
        rcg( lpszFileName, lpszReturnBuffer, RETURN_BUFFER_SIZE );

And after a lot of stuff that didn't work I have this in VB.net
        Dim d As IntPtr = Marshal.AllocCoTaskMem(24)
        Dim result As IntPtr = rcg("r.rcg", d, 24)

I am trying to output the result of d in my code (the lpszReturnBuffer in the C++).  I'm not sure how to copy that memory into a byte array or string that I can work with.  I do realize that I am perhaps a bit over my head but I'm trying hard to learn here.  I have been changing this around for about 24 hours and figured it was time to ask the experts!

ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adamwj
adamwj

ASKER

That worked perfectly thank you VERY much!