Reputation: 1879
I have a C#.NET application and an unmanaged C Win32 .DLL program. How can i pass HANDLEs between the two app? i mean i wanna set a HANDLE from C code to a output parameter that comes from C#, and again, pass the HANDLE from C# to another function in the DLL? I guess it is related to IntPtr, but i don't know what the C & C# code should be!
thanks.
Upvotes: 0
Views: 4339
Reputation: 612964
HANDLE
is defined as void*
, i.e. something the size of a pointer. The equivalent in the managed world is IntPtr
.
Upvotes: 0
Reputation: 7943
To call C code from C#, you can use the DllImportAttribute to indicate the parameter as IntPtr:
[DllImport("mydll.dll", EntryPoint="my_c_function")]
public static extern void my_c_function(IntPtr myHandle);
[DllImport("mydll.dll", EntryPoint="my_c_function_with_out_param")]
public static extern void my_c_function(out IntPtr returnedHandle);
Just make sure that your C functions look like this:
void my_c_function(HANDLE myHandle)
{
// ....
}
void my_c_function_with_out_param(HANDLE * pReturnedHandle)
{
// ....
*pReturnedHandle = GenerateHandle();
}
Upvotes: 3