C# pointer to struct using C dll
-
Hi. I need to basically have an equivalent C# struct to my C struct. I need to make a pointer to it. This is the relevant code: C code: #if defined(WIN32) # define DLL_EXPORT __declspec(dllexport) #else # define DLL_EXPORT /**/ #endif #ifdef __cplusplus extern "C" { #endif struct vector { uint elem_count; /* number of items in the vector */ uint size; /* size of the vector */ uint elem_size; /* element size */ int (*cmp)(const void *, const void *); void *table; }; typedef struct vector VECTOR; #define CSTATS VECTOR //function definition: DLL_EXPORT CSTATS *screate(); // returns CSTATS pointer ... #ifdef __cplusplus } #endif C# code: [UnmanagedFunctionPointer(CallingConvention.Winapi)] unsafe public delegate int cmp(IntPtr a, IntPtr b); [StructLayout(LayoutKind.Sequential, Pack = 1)] unsafe public struct VECTOR { public uint elem_count; public uint size; public uint elem_size; [MarshalAs(UnmanagedType.FunctionPtr)] public cmp cp; //C-func: public int (*cmp)(const void *, const void *); public IntPtr table; } [UnmanagedFunctionPointer(CallingConvention.Winapi)] unsafe public delegate VECTOR screate(); [DllImport("myTest.dll", EntryPoint = "screate", ExactSpelling = true)] public static extern screate scrt(); Now, here is the problem. The C code I want to emulate in C# is this: CSTATS *st; st = screate(); In C#, I tried: VECTOR* st = screate(); VECTOR* st = scrt(); and so on... The error I get is: error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('myDll.VECTOR') So I am looking for help. What is the equivalent C# code that I need, in order to achieve "CSTATS *st; st = screate();" in C? Thank you for any help.
-
Hi. I need to basically have an equivalent C# struct to my C struct. I need to make a pointer to it. This is the relevant code: C code: #if defined(WIN32) # define DLL_EXPORT __declspec(dllexport) #else # define DLL_EXPORT /**/ #endif #ifdef __cplusplus extern "C" { #endif struct vector { uint elem_count; /* number of items in the vector */ uint size; /* size of the vector */ uint elem_size; /* element size */ int (*cmp)(const void *, const void *); void *table; }; typedef struct vector VECTOR; #define CSTATS VECTOR //function definition: DLL_EXPORT CSTATS *screate(); // returns CSTATS pointer ... #ifdef __cplusplus } #endif C# code: [UnmanagedFunctionPointer(CallingConvention.Winapi)] unsafe public delegate int cmp(IntPtr a, IntPtr b); [StructLayout(LayoutKind.Sequential, Pack = 1)] unsafe public struct VECTOR { public uint elem_count; public uint size; public uint elem_size; [MarshalAs(UnmanagedType.FunctionPtr)] public cmp cp; //C-func: public int (*cmp)(const void *, const void *); public IntPtr table; } [UnmanagedFunctionPointer(CallingConvention.Winapi)] unsafe public delegate VECTOR screate(); [DllImport("myTest.dll", EntryPoint = "screate", ExactSpelling = true)] public static extern screate scrt(); Now, here is the problem. The C code I want to emulate in C# is this: CSTATS *st; st = screate(); In C#, I tried: VECTOR* st = screate(); VECTOR* st = scrt(); and so on... The error I get is: error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('myDll.VECTOR') So I am looking for help. What is the equivalent C# code that I need, in order to achieve "CSTATS *st; st = screate();" in C? Thank you for any help.
Can you repost your code, except this time with <pre> tags around it? It's too hard to read all unformatted like that.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Lord Is So Good The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Can you repost your code, except this time with <pre> tags around it? It's too hard to read all unformatted like that.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Lord Is So Good The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Sorry, here it is. C code:
#if defined(WIN32)
define DLL_EXPORT __declspec(dllexport)
#else
define DLL_EXPORT /**/
#endif
#ifdef __cplusplus
extern "C" {
#endifstruct vector { uint elem\_count; /\* number of items in the vector \*/ uint size; /\* size of the vector \*/ uint elem\_size; /\* element size \*/ int (\*cmp)(const void \*, const void \*); void \*table; }; typedef struct vector VECTOR; #define CSTATS VECTOR //function definition: DLL\_EXPORT CSTATS \*screate(); // returns CSTATS pointer ...
#ifdef __cplusplus
}
#endifC# code:
\[UnmanagedFunctionPointer(CallingConvention.Winapi)\] unsafe public delegate int cmp(IntPtr a, IntPtr b); \[StructLayout(LayoutKind.Sequential, Pack = 1)\] unsafe public struct VECTOR { public uint elem\_count; public uint size; public uint elem\_size; \[MarshalAs(UnmanagedType.FunctionPtr)\] public cmp cp; //C-func: public int (\*cmp)(const void \*, const void \*); public IntPtr table; } \[UnmanagedFunctionPointer(CallingConvention.Winapi)\] unsafe public delegate VECTOR screate(); \[DllImport("myTest.dll", EntryPoint = "screate", ExactSpelling = true)\] public static extern screate scrt();