Translate Visual C++ to VB.NET

M

M. Bruil

I tried to do some 'DLLImport' on a DLL. The actual source code was written
in Visual C++. Unfortunately I can't get the VB.NET syntax right for this
function call. Can someone provide me with the correct VB.NET syntax?

----------------------------------------




#define S00X_ANTICOLL_ARRAY 64

int MSTX_EXPORT S00X_ReadC220(BOOL useconfig, BOOL changeconfig, BOOL
anticollision, double listeningtime, unsigned char *nbchips, unsigned char
chips[][S00X_ANTICOLL_ARRAY], unsigned char *cmdstatus);



<DllImport("MedioSTX.dll")> _
Private Function S00X_ReadC220(ByVal useconfig As Boolean, ByVal
changeconfig As Boolean, ByVal anticollision As Boolean, ByVal listeningtime
As Double, ByRef nbchips As Char, ByRef chips()() As Char, ByRef cmdStatus
As Char) As Integer
End Function
 
D

Dragon

Hi,

First of all, char * is a String (LPStr actually).

But the chips parameter poses a difficulty indeed, since AFAIK [][]
means an continuous sequence of strings, not even an array of LPStr's
8=[.
And I don't see a way to pass this except allocating the memory and
positioning strings manually. So that's the declarations:

~
Const S00X_ANTICOLL_ARRAY As Integer = 64

Declare Ansi Function S00X_ReadC220 Lib "MedioSTX.dll" ( _
ByVal useconfig As Boolean, _
ByVal changeconfig As Boolean, _
ByVal anticollision As Boolean, _
ByVal listeningtime As Double, _
<MarshalAs(UnmanagedType.LPStr)> ByVal nbchips As String, _
ByVal chips As IntPtr, _
<MarshalAs(UnmanagedType.LPStr)> ByVal cmdstatus As String _
) As Integer
~

And the usage:

~
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
MyBase.OnClick(e)
Dim ChipsA() As String = {"JC Denton", "Alex Jacobson", "Jaime
Reyes", "Joseph Manderley"}
' I presume you have a string array called ChipsA

' strings' length must not exceed S00X_ANTICOLL_ARRAY,
' or you'll get garbage!

Dim base As IntPtr, cur As Integer
base = Marshal.AllocHGlobal(S00X_ANTICOLL_ARRAY * ChipsA.Length)
cur = base.ToInt32

For Each str As String In ChipsA
'Copy the string...
Marshal.Copy(System.Text.Encoding.Default.GetBytes(str), 0,
New IntPtr(cur), str.Length)
'... and append the terminating NULL.
Marshal.WriteByte(New IntPtr(cur), str.Length, CByte(0))
cur += S00X_ANTICOLL_ARRAY
Next

'Use function with base as chips:
'S00X_ReadC220(blah blah blah...)

Marshal.FreeHGlobal(base)
End Sub
~

Whew... This was a bit harder that I initially expected.. 8=]

Unfortunately, I don't have a library to test this on, but it should
work.

Hope this helps,
Roman
 

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