PC Review


Reply
Thread Tools Rate Thread

Accessing C dll

 
 
tdcrotty
Guest
Posts: n/a
 
      5th Nov 2003
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
 
Reply With Quote
 
 
 
 
Thomas
Guest
Posts: n/a
 
      5th Nov 2003
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.

"tdcrotty" <(E-Mail Removed)> a écrit dans le message de
news:0d8501c3a3ae$2bc5dbd0$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      5th Nov 2003
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:

http://groups.google.com/groups?hl=e...mpactframework

Paul T.

"Thomas" <(E-Mail Removed)> wrote in message
news:uQCWl%(E-Mail Removed)...
> 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.
>
> "tdcrotty" <(E-Mail Removed)> a écrit dans le message de
> news:0d8501c3a3ae$2bc5dbd0$(E-Mail Removed)...
> > 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

>
>



 
Reply With Quote
 
tdcrotty
Guest
Posts: n/a
 
      6th Nov 2003
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:
>
>http://groups.google.com/groups?hl=e...ang_en&ie=UTF-

8&oe=UTF-
8&group=microsoft.public.dotnet.framework.compactframework
>
>Paul T.
>
>"Thomas" <(E-Mail Removed)> wrote in message
>news:uQCWl%(E-Mail Removed)...
>> 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.
>>
>> "tdcrotty" <(E-Mail Removed)> a écrit dans le message

de
>> news:0d8501c3a3ae$2bc5dbd0$(E-Mail Removed)...
>> > 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

>>
>>

>
>
>.
>

 
Reply With Quote
 
tdcrotty
Guest
Posts: n/a
 
      6th Nov 2003
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
>-----Original Message-----
>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.
>
>"tdcrotty" <(E-Mail Removed)> a écrit dans le message de
>news:0d8501c3a3ae$2bc5dbd0$(E-Mail Removed)...
>> 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

>
>
>.
>

 
Reply With Quote
 
Thomas
Guest
Posts: n/a
 
      6th Nov 2003
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" <(E-Mail Removed)> a écrit dans le message de
news:08a201c3a423$75e55400$(E-Mail Removed)...
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
>-----Original Message-----
>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.
>
>"tdcrotty" <(E-Mail Removed)> a écrit dans le message de
>news:0d8501c3a3ae$2bc5dbd0$(E-Mail Removed)...
>> 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

>
>
>.
>



 
Reply With Quote
 
Geoff Schwab [MSFT]
Guest
Posts: n/a
 
      6th Nov 2003
Have you looked at the FAQ? There are several links in the following entry
to articles.

6.1. How do I call a function that is in a native DLL?
http://msdn.microsoft.com/mobility/p...fault.aspx#6.1

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/p...Q/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
"tdcrotty" <(E-Mail Removed)> wrote in message
news:0d8501c3a3ae$2bc5dbd0$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(IFS filter driver) Accessing user buffer from kernel thread or accessing handles within user context RA Windows XP Drivers 4 21st Dec 2005 11:40 AM
IE slow accessing IIS6 versus being fast accessing IIS4 =?Utf-8?B?dmFkaW0=?= Windows XP Internet Explorer 0 20th Oct 2004 08:51 PM
IE slow when accessing IIS6 versus fast accessing IIS4 =?Utf-8?B?dmFkaW0=?= Windows XP Internet Explorer 0 20th Oct 2004 08:49 PM
Problem accessing Internet Explorer 6 when initially accessing internet inowcu2 \(removethis\) @yahoo.com Windows XP Internet Explorer 0 10th Dec 2003 04:40 AM
Re: accessing a C-DLL Qiu Zhang Microsoft C# .NET 1 31st Jul 2003 05:47 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:14 PM.