I would like to use this C function in C#:
typedef void (*WRITE_CALLBACK)(int hMountEnv, unsigned __int64 NumBytesWritten,
void* pContext);
How would I go about defining it so I can call it?
Do I need to do anything else to make this work?
Take a look at http://msdn.microsoft.com/en-us/library/ektebyzx(v=vs.80).aspx for how to marshal function pointers. There is an example on the bottom of that page.
I’ll do the C# for you, and since I don’t know which direction the pointer is going, I’ll show both. There are some gotchas which could be problematic for you involving calling conventions.
Marshal.GetDelegateForFunctionPointerandMarshal.GetFunctionPointerForDelegateassume the function pointer will be StdCall so if you don’t have access to the unmanaged library to make sure the function pointer is standard call (I think C defaults to cdecl unfortunately), you’d have to create an unmanaged shim library to change the calling convention, unless there is some other way I don’t know about.This would be the header of a C DLL I named “UnmanagedLib.dll”.
This would be the CPP file of the DLL.
And lastly, this is a C# program to make use of the DLL.