I have a C++ Component class being written in a DLL and a DLL export funtion that returns the pointer to the Component Class.
class Component
{
char* name;
char* id;
char* version;
char* description;
Component* parent;
std::list
}
DLLEXPORT Component* Enumerate()
{
return Module::GetInstance();
}
In my C# Application, in order to access the Component class using the Enumerate funtion, do I need to use managed C++/CLI wrapper or use
Marshal.PtrtoStructure or ICustomMarshalling
What is the correct way to do this?
Bechir BejaouiPosted Dec 20, 2008, 11:29 AM
Bechir BejaouiPosted Dec 20, 2008, 7:14 PM
Vikas SirigiriPosted Dec 20, 2008, 1:44 PM
Well In my C# application, I have created the following structure and used Marshal.PtrToStructure.
[StructLayout(LayoutKind.Sequential)] Children;
public class Component
{
public string Name;
public string Id;
public string Version;
public string Description;
[MarshalAs(UnmanagedType.Struct)]
public Component Parent;
[MarshalAs(UnmanagedType.ByValArray)]
public List
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
int intptr = LoadLibrary("SampleModuleDLL.dll");
IntPtr procAd = (IntPtr)GetProcAddress(intptr, "TestEnumerate");
TestDelegate testStartfunc = (TestDelegate)Marshal.GetDelegateForFunctionPointer(procAd, typeof(TestDelegate));
IntPtr pointerToFunc = testStartfunc();
component = (Component)Marshal.PtrToStructure(pointerToFunc, typeof(Component));
When I run my application, Marshal.PtrToStructure is throwing an exception..
Cannot marshal field 'Parent' of type 'ConsoleApplication2.Component': There is no marshaling support for this type.System.TypeLoadException: Cannot marshal field 'Parent' of type 'ConsoleApplication2.Component': There is no marshaling support for this type.
at System.Runtime.InteropServices.Marshal.PtrToStructureHelper(IntPtr ptr, Object structure, Boolean allowValueClasses)
at System.Runtime.InteropServices.Marshal.PtrToStructure(IntPtr ptr, Type structureType)
Could you please tell me where is the error?