Importing a function from a DLL

G

Guest

I'm trying to P/Invoke a function from a DLL in VB.NET

The prototype is

PSUUID0C_API int GetSerNum (TCHAR* SerNum, DWORD len)

I've tried several translation into Visual Basic, unsuccessfully so far
This one

Public Declare Function GetSerNum Lib "psuuid0c.dll"
(ByRef data As String, ByVal dwLen As Long) As Intege

causes a "NotSupported" exception
If I tr

Public Declare Function GetSerNum Lib "psuuid0c.dll"
(ByVal data As String, ByVal dwLen As Long) As Intege

I get a "MethodNotFound" exception instead.
I've tried Integer instead of Long, etc, with no luck

Since a TCHAR * seems to map to a String pointer, I was confident that the second one would work just fine, but it does not

Can anyone shed some light

Thanks.
 
C

Chris Tacke, eMVP

Long is 64-bit, so that's wrong - should be an Integer (actually a UInt32 to
be exact). If that still fails, make sure the name isn't mangled with
dumpbin.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
---
Principal Partner
OpenNETCF Consulting
www.OpenNETCF.com



Carlos Fernandez said:
I'm trying to P/Invoke a function from a DLL in VB.NET.

The prototype is

PSUUID0C_API int GetSerNum (TCHAR* SerNum, DWORD len);

I've tried several translation into Visual Basic, unsuccessfully so far.
This one:

Public Declare Function GetSerNum Lib "psuuid0c.dll" _
(ByRef data As String, ByVal dwLen As Long) As Integer

causes a "NotSupported" exception.
If I try

Public Declare Function GetSerNum Lib "psuuid0c.dll" _
(ByVal data As String, ByVal dwLen As Long) As Integer

I get a "MethodNotFound" exception instead.
I've tried Integer instead of Long, etc, with no luck.

Since a TCHAR * seems to map to a String pointer, I was confident that the
second one would work just fine, but it does not.
 
C

Chris Tacke, eMVP

dumpbin is showing the problem. The name is mangled.

Try this:

<DllImport("psuuid0c.dll"), SetLastError=true,
EntryPoint="?GetSerNum@@YAHPAGK@Z"> _
Public Function GetSerNum(ByRef sernum As String, ByRef len As UInt32 ) As
Integer
End Function

or by ordinal:

<DllImport("psuuid0c.dll"), SetLastError=true, EntryPoint="#2"> _
Public Function GetSerNum(ByRef sernum As String, ByRef len As UInt32 ) As
Integer
End Function

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
---
Principal Partner
OpenNETCF Consulting
www.OpenNETCF.com



Carlos Fernandez said:
I tried with all combinations of int, long, uint32, etc

dumpbin shows this


1 0 00001268 ?GetMACaddr@@YAHPAGK@Z
2 1 000010F0 ?GetSerNum@@YAHPAGK@Z
3 2 00001008 ?GetUUID@@YAHPAEK@Z

(I already expected it to be ok since the function is callable from C as expected).

This doesn't work:

Public Shared Function GetSerNum( _
ByRef sernum As String, ByRef len As UInt32 _
) As UInt32

(tried all combos of ByRef and ByVal as well... I don't know how .net
expects the strings)... I don't see why in the world it could want a uint32
passed by reference but it changes the exception from method not found to
not supported...
 
A

Alex Feinman [MVP]

<DllImport("psuuid0c.dll"), SetLastError:=True,
EntryPoint:="?GetSerNum@@YAHPAGK@Z"> _
Public Shared Function GetSerNum(ByVal sernum As StringBuilder, ByVal len As
Integer ) As Integer
End Function

To call to this:
Dim cbBuffer as Integer = 255
Dim buffer as new StringBuilder(cbBuffer)

cbBuffer = GetSerNum(buffer, cbBuffer)
Dim serNum as String = buffer.ToString(0, cbBuffer)


--
Alex Feinman
---
Visit http://www.opennetcf.org
Carlos Fernandez said:
I tried with all combinations of int, long, uint32, etc

dumpbin shows this


1 0 00001268 ?GetMACaddr@@YAHPAGK@Z
2 1 000010F0 ?GetSerNum@@YAHPAGK@Z
3 2 00001008 ?GetUUID@@YAHPAEK@Z

(I already expected it to be ok since the function is callable from C as expected).

This doesn't work:

Public Shared Function GetSerNum( _
ByRef sernum As String, ByRef len As UInt32 _
) As UInt32

(tried all combos of ByRef and ByVal as well... I don't know how .net
expects the strings)... I don't see why in the world it could want a uint32
passed by reference but it changes the exception from method not found to
not supported...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top