Accessing C dll

  • Thread starter Thread starter tdcrotty
  • Start date Start date
T

tdcrotty

Hi,

I'm trying to use a custom dll written in C in my VB
program. The C library uses many structs but none are
very complicated. I cannot use MarshalAs to Marshal the
data.

I'm wondering if anyone has experience with this and can
provide me with a couple of examples.

Thanks,

tdcrotty
 
One way is to convert your parameters which are structure type into bytes
arrays.
You have to make a 2 functions :
- one to transform your struct into an array of bytes in order to be
sent to your DLL's functions
- one to transform bytes arrays that you'll receive from your DLL into
your managed structure

Thomas.
 
Thanks for the link. I'll check it out.

td
-----Original Message-----
There are examples of this all over the OpenNetCF library. I'm not sure if
the site is back up or not, but it's www.opennetcf.org. There are lots of
p/invoke examples in the archives of this newsgroup, also:
8&oe=UTF-
8&group=microsoft.public.dotnet.framework.compactframework

Paul T.




.
 
Thanks,

SO is this a form of manually marshalling? Just send the
data as raw bytes and return raw bytes from the dll
functions?

td
 
Maybe we can say that.
You must take care of the type of the data you have in your structures
(about the length in bytes of each variable).

A sample of a Getbytes/Decode functions (sorry but the comments are in
French) :

'--------------------------------------------------------

'Transforme une instance de ActionKey en tableau de bytes

'

'Valeur retournée :

' TRUE si OK, FALSE sinon

'--------------------------------------------------------

Private Function GetBytes(ByRef TabBytes() As Byte) As Boolean

Dim i As Integer

Dim max As Integer

Dim max2 As Integer

Try

'On redimensionne le tableau

ReDim TabBytes(1324)

'Entier IDBouton = 4 bytes

BitConverter.GetBytes(IDBouton).CopyTo(TabBytes, 0)

'Entier TypeClick = 4 bytes

BitConverter.GetBytes(TypeClick).CopyTo(TabBytes, 4)

'Entier Typeaction = 4 bytes

BitConverter.GetBytes(TypeAction).CopyTo(TabBytes, 8)

'Booléen Son = 4 bytes

BitConverter.GetBytes(Son).CopyTo(TabBytes, 12)

'Tableau de bytes Keyboard = 260 * 1 byte

If Not IsNothing(Keyboard) Then

max = Keyboard.GetUpperBound(0)

For i = 0 To max

TabBytes(i + 16) = Keyboard(i)

Next

End If

'Entier TailleKeyboard = 4 bytes

BitConverter.GetBytes(TailleKeyboard).CopyTo(TabBytes, 276)

'Entier DureeClick = 4 bytes

BitConverter.GetBytes(DureeClick).CopyTo(TabBytes, 280)

'Tableau de caractères Application = 260 * 2 bytes

If Not IsNothing(Application) Then

max = Application.GetUpperBound(0)

For i = 0 To max

BitConverter.GetBytes(Application(i)).CopyTo(TabBytes, 284 + (2 * i))

Next

End If

'Tableau de caractères Paramètres = 260 * 2 bytes

If Not IsNothing(Parametres) Then

max = Parametres.GetUpperBound(0)

For i = 0 To max

BitConverter.GetBytes(Parametres(i)).CopyTo(TabBytes, 804 + (2 * i))

Next

End If

GetBytes = True

Catch ex As Exception

GetBytes = False

End Try



End Function



'---------------------------------------------------------------------------
----------

'Analyse le tableau de bytes retourné par lecture des infos d'un bouton du
Gotive

'et inscrit ces données dans les membres de la classe

'

'Valeur retournée :

' TRUE si OK, FALSE sinon

'---------------------------------------------------------------------------
----------

Private Function Decode(ByVal TabBytes() As Byte) As Boolean

Dim i As Integer

Try

'Entier IDBouton = 4 bytes

IDBouton = BitConverter.ToUInt32(TabBytes, 0)

'Entier TypeClick = 4 bytes

TypeClick = BitConverter.ToUInt32(TabBytes, 4)

'Entier Typeaction = 4 bytes

TypeAction = BitConverter.ToUInt32(TabBytes, 8)

'Booléen Son = 4 bytes

Son = BitConverter.ToBoolean(TabBytes, 12)

'Tableau de bytes Keyboard = 260 * 1 byte

For i = 0 To 259

Keyboard(i) = TabBytes(i + 16)

Next

'Entier TailleKeyboard = 4 bytes

TailleKeyboard = BitConverter.ToUInt32(TabBytes, 276)

'Entier DureeClick = 4 bytes

DureeClick = BitConverter.ToUInt32(TabBytes, 280)

'Tableau de caractères Application = 260 * 2 bytes

For i = 0 To 259

Application(i) = BitConverter.ToChar(TabBytes, 284 + (2 * i))

Next

'Tableau de caractères Paramètres = 260 * 2 bytes

For i = 0 To 259

Parametres(i) = BitConverter.ToChar(TabBytes, 804 + (2 * i))

Next

Decode = True

Catch ex As Exception

Decode = False

End Try

End Function


Thomas.

"tdcrotty" <[email protected]> a écrit dans le message de
Thanks,

SO is this a form of manually marshalling? Just send the
data as raw bytes and return raw bytes from the dll
functions?

td
 
Back
Top