Expert: Srini Nagarajan Date: 10/19/2005 Subject: C Data Structure in C#
Question I have a DLL that is written in C. I have to use it from within C# code.
Some of the available DLL functions must be implemented as callbacks. This means that when a given DLL function is called from C# managed code, the DLL unmanaged code will "callback" a method implemented in C# managed code.
For this purpose I have managed to use C# delegates with sucess for simple String datatypes (in C that would be a char *).
For example, the following code works just fine:
// Delegate
public delegate int OnCodeLine(String codeline);
[DllImport(DLL)]
private static extern int rdOnCodeLine(OnCodeLine codeline);
// Install the callback
public void Callback()
{
OnCodeLine codeline = new OnCodeLine OnCodeLineImplementation);
int rtnval = rdOnCodeLine(codeline);
}
// Implement the callback
public int OnCodeLineImplementation(String codeline)
{
System.Console.WriteLine("Codeline Debug: " + codeline);
return 1;
}
Things get out of hand when a given callback implementation must access a C complex data structure (defined has a struct).
The callback in question returns to the managed code a pointer to a data structure.
How can I acess the data stored inside the C data structure?
Please, if you require any detailed code just let me know.
Regards.
Joćo
Answer Hi
I haven't really worked with Unsafe/Unmanaged code interact with C#