473,382 Members | 1,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

passing struct to unmanaged code

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
5 2810
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
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
thanks guys for the help, it was really appreciated!
Nov 15 '05 #4
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl...
2
by: Chris | last post by:
Hi, I seem to be having a problem I can't quite figure out. Currently, I have the following code (below). In the SendMsg function, I create a 'Qtkmsg' which converts the string 'text' into a...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
0
by: nygiantswin2005 | last post by:
I would like to know how do I pass a pointer to a struct from managed code to unmanaged code. For example if I create structure like this in managed code. StructLayout( LayoutKind.Sequential,...
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
2
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of...
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
5
by: craig1231 | last post by:
I have a problem trying to pass a structure to an unmanaged c++ DLL from C#. When I call PCSBSpecifyPilotLogon from C#, it throws an AccessViolationException This is what I have... The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.