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

Posted on 2008-02-05
Medium Priority
Last Modified: 2012-06-27
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!

Question by:adamwj
LVL 41

Accepted Solution

graye earned 2000 total points
ID: 20832091
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)

Author Comment

ID: 20834227
That worked perfectly thank you VERY much!

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question