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!

adamwjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grayeCommented:
A slightly simpler way to define the function would be:

Private Declare Function rcg Lib "rcg.dll" Alias "rcg" (ByVal src_file As String, ByVal return_buf As StringBuilder, ByVal size_returnbuf As Integer) As IntPtr
                                                                                                                                                       ^^^^^^^^
Then you'd use it as follows:

Dim sb as new StringBuilder(24)
rcg("whatever", sb, 24)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adamwjAuthor Commented:
That worked perfectly thank you VERY much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.