i’m marshaling ‘unmanaged c’ code to my C# code given below.
[DllImport("ContainerDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NodeSearch(IntPtr firstNode, string key);
IntPtr firstNode = IntPtr.Zero;
private void button4_Click(object sender, EventArgs e)
{
IntPtr ret = NodeSearch(firstNode, "key_string");
}
//NodeSearch method will be called which is present in 'ContainerDll.dll'
//pointer to structure will be returned.
//my c-structure contains these fields.
// typedef struct container
// {
// char Name[20];
// void *VoidData;
// struct container *Link;
// }
// Node;
My C# variable ‘ret’ of type ‘IntPtr’ got the pointer to this structure now. It has the address returned from ‘NodeSearch’ method.
How to access this in C# form application(also in console application)?
I think I cannot use like this: ret->Name[0], ret->VoidData etc.
I am a beginner! Can you please me?
You will need to create a compatible
structdefinition in C#, and use the Marshal class to marshal the pointer to the struct.The struct definition might look like the following:
You should then be able to Marshal the pointer to this struct in a fashion similar to the following:
In order to retrieve the link, or the void data, you would also need to call
Marshal.PtrToStructure.