By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

passing struct to unmanaged code

P: n/a
hi i have the following problem:
i use functions from an external dll(normal w32 unmanaged library) which
takes a structure address as an argument..
how do i pass it in c#?
i tried using array instead od struct and fixed statement to get a pointer,
it seemed to work, but i bet there is a better and more elegant way to do
that

and a second issue which is more imprortant :)
one member of the structure given as an argument(or rather address of which
is given as an arg) is a pointer to a callback function... how do i get
function pointer in c# and pass it to that unmanaged code, so it can be
called from that code in the future?
please help me, i would be very grateful.

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
i use functions from an external dll(normal w32 unmanaged library) which
takes a structure address as an argument..
how do i pass it in c#?
Make it a ref parameter, that works in most cases.

one member of the structure given as an argument(or rather address of which
is given as an arg) is a pointer to a callback function... how do i get
function pointer in c# and pass it to that unmanaged code, so it can be
called from that code in the future?


Declare it as a delegate type with the right callback signature, and
the CLR will marshal it to a function pointer for you.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2

P: n/a
There is a section in MSDN on P/Invoke:

http://msdn.microsoft.com/library/de...structures.asp

and an article how to pass structure:
http://msdn.microsoft.com/library/de...structures.asp

A good idea is to look at Interop Marshalling for specific data types
marshalling, for instance strings in different cases. Have a look at the
following:

http://msdn.microsoft.com/library/de...edatatypes.asp

http://msdn.microsoft.com/library/de...edatatypes.asp

You should be most happy with:
http://msdn.microsoft.com/library/de...uctssample.asp
http://msdn.microsoft.com/library/de...infosample.asp

Articles are not long, and samples are very clear and meaningful. Worth
browsing and looking for an adequate sample you need.

--
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply

"adrin" <ad***@adrin.adrin> wrote in message
news:Xn***************************@193.110.122.80. ..
hi i have the following problem:
i use functions from an external dll(normal w32 unmanaged library) which
takes a structure address as an argument..
how do i pass it in c#?
i tried using array instead od struct and fixed statement to get a pointer, it seemed to work, but i bet there is a better and more elegant way to do
that

and a second issue which is more imprortant :)
one member of the structure given as an argument(or rather address of which is given as an arg) is a pointer to a callback function... how do i get
function pointer in c# and pass it to that unmanaged code, so it can be
called from that code in the future?
please help me, i would be very grateful.


Nov 15 '05 #3

P: n/a
thanks guys for the help, it was really appreciated!
Nov 15 '05 #4

P: n/a
> Declare it as a delegate type with the right callback signature, and
the CLR will marshal it to a function pointer for you.

sorry, but what do you mean by right callback signature :)?

Nov 15 '05 #5

P: n/a
sorry, but what do you mean by right callback signature :)?


Just that you have to get the parameter types (and in/out-ness) right,
just like you have for the DllImport function itself.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.