I have been given a DLL (‘InfoLookup.dll’) that internally allocates structures and returns pointers to them from a lookup function. The structures contain string pointers:
extern 'C' { struct Info { int id; char* szName; }; Info* LookupInfo( int id ); }
In C#, how can I declare the structure layout, declare the Interop call, and (assuming a non-null value is returned) utilize the string value? In other words, how do I translate the following into C#?
#include 'InfoLookup.h' void foo() { Info* info = LookupInfo( 0 ); if( info != 0 && info->szName != 0 ) DoSomethingWith( info->szName ); // NOTE: no cleanup here, the DLL is caching the lookup table internally }
Try the following layout. Code automatically generated using the PInvoke Interop Assistant. Hand coded LookpInfoWrapper()