In article <11*********************@f14g2000cwb.googlegroups. com>,
ma*****@yahoo.com wrote:
I'm trying to call methods in a C++ dll from vb.net that is not a .com
component. I've read I can just use regular Declare statements just as
you do in VB6 except that some of the types need to be converted in the
declaration.
Is there any doco somewhere that identifies what needs to be changed in
hte Declare statement ? i.e. long = integer, etc...
Also, I'm having to build the declare statements based on some doco I
have from the DLL. Is there a tool or something that will build the
declare statements for me ?
Ok, one more thing. Some of these methods in the DLL return strings
that were passed by reference as an argument. Is there anything I
should be look out for when using strings, either passed as arguments
by reference and modified in the DLL or just returned from the function
? Seems like I've read somewhere the former that can be a problem.
Many TIA,
Mark
The best advice I can give you here is to read the documentation on
platform invoke and marshalling. Here is a good place to start:
http://msdn.microsoft.com/library/de...formInvoke.asp
Unfortunately, most of the example code is C# - but the concepts are
aplicable to VB.NET as well.
The do have a table in there that lists a lot of the common datatype
conversions. But, for the most part, its the same as in VB.CLASSIC -
you pick the type that has the same corresponding size.
C VB.NET
char Byte
short Short
int Integer
long Integer
Handle types, should usually be declared as IntPtr and any 64-bit values
should be Long.
Strings are a little different... If string is being passed as an out
parameter - it is being modified by the called procedure, then it really
should be declared as ByVal ParamName As System.Text.StringBuilder. If
it is a constant string, then ByVal ParamName As String is fine. String
will work for out params as well, but it results in a lot of extra work
for the VB.NET marshaller since .NET strings are immutable. Also, you
should be aware that VB.NET, unlike VB.CLASSIC, is able to directly
access Unicode functions via declare statements. In VB.CLASSIC the only
way to do it was by using StrPtr or using a typelibrary wrapper for the
function. The result of this is that in VB.CLASSIC you would often see
such declarations as:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, _
ByRef nSize As Long) As Long
In VB.NET, you would remove the alias and change the data types
somewhat...
Declare Auto Function GetUserName Lib "advapi32.dll" _
(ByVal lpBuffer As System.Text.StringBuilder, _
ByRef nSize As Integer) As Boolean
Anway, reading through the docs, experimenting, and then posting questions
to the groups - probably the interop group would be the most appropriate -
are really the best ways to learn this stuff...
--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 1 Days, 0 Hours, 1 Minutes, 32 Seconds