views:

36

answers:

2

I am working on a project where I have to import a DLL file into a VB project that was created a few years back. The DLL was created in C++, and looks like:

void CoordinateConversionService::convert( SourceOrTarget::Enum sourceDirection, SourceOrTarget::Enum targetDirection, CoordinateTuple* sourceCoordinates, Accuracy* sourceAccuracy, CoordinateTuple& targetCoordinates, Accuracy& targetAccuracy )

I am an intern at my job, and I haven't had to use this yet, so my understanding is extremely limited, along with my usage of VB (I'm a C++/C# guy). Here are a few questions:

1) Looking at Dllimport, it seems like the last part outside of the parameters is a return type. Example code from another site:

<DllImport("advapi32.dll")> _
  Public Function GetUserName( _
     ByVal lpBuffer As StringBuilder, _
     ByRef nSize As Integer) As Boolean

Is "As Boolean" the return type? If so, I tried using "Sub" and it says "Keyword does not name a type." Hence why I looked into declare, because it seems I CAN return void/sub as a return type.

2) Trying to use the types "CoordinateTuple" and "Accuracy" give me problems, saying that they aren't defined. How do I get around this since I don't think I can really define them, and what about the fact that they're pointers? Also - I cannot modify the C++ code in any way, so what I have is what I have.

+3  A: 

In VB you say either Public Function Whatever (params) As ReturnType (which is the same as public ReturnType Whatever(params) in C#) or Public Sub Whatever (params) which is for things that don't return anything (return void in C++/C#).

If your function/sub takes custom types you will need to declare .NET equivalents to those as well. This can make PInvoke hard work. Tools like the interop assistant can help.

Kate Gregory
So I'll essentially need to convert the types over from C++ to VB.net?
ibarczewski
Yes. If you use the interop assistant you can basically paste in the C++ defs and it will give you VB to put before your DllImport statement.
Kate Gregory
Okay, cool. I'll test your method in a bit and then give you the answer if it works :)
ibarczewski
+1 The interop assistant rocks! ibarczewski may need to copy in some bits of header files as well, so that the interop assistant knows what `CoordinateTuple` is etc
MarkJ
A: 

For the custom types, you may have to create a wrapper in managed C++, which has the ability both to consume native C++ APIs and expose managed APIs. Although dated, a walkthrough can be found here:

Dave
Only if the types are complicated. POD types are handled very well by the .NET marshaller.
Philipp