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

Passing delegates in a structure

P: n/a
I am a starter in C# ...

I will present my problem :
I am wrapping a C dll in C#. The C dll exports a function which takes a

structure MYSTRUCT holding 2 function pointers(callback functions).
struct MYSTRUCT
{
ontest1 t1;
ontest2 t2
};
where ontest1 and ontest2 are two function pointers.

In C# dll, I have declared this structure as
[StructLayout(LayoutKind.Sequential)]
public struct MYSTRUCT
{
[MarshalAs(UnmanagedType.FunctionPtr)]
ontest1 t1;
[MarshalAs(UnmanagedType.FunctionPtr)]
ontest2 t2

}
where ontest1 and ontest2 are two delegates.

//Importing the C dll function "MyFunctions"
[DllImport("mydll.dll")]
IntPtr MyFunctions(IntPtr myStruct);
//The .NET function wrapping the above function :
IntPtr MyFunctions1(ref MYSTRUCT mystruct)
{
//Converts MYSTRUCT to pointer
GCHandle callbackHandle = GCHandle.Alloc
(mystruct ,GCHandleType.Pinned);
IntPtr mystructptr = callbackHandle.AddrOfPinnedObject();
//Calls the imported C dll function
IntPtr handle = MyFunctions(mystructptr);
return handle;

}
The address in the delegates in MYSTRUCT are stored inside the C dll..
and the same functions are invoked whenever an event occurs. That is
the unmanaged code calls the managed code asynchronously.

The call to the C dll is working fine.. but whenever an event occurs
the registered function is not getting involked..
Do I have to add/remove anything more to the code?
Any help will be appreciated...
regards
Renjini

Jul 1 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.