Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# pointer to struct using C dll

C# pointer to struct using C dll

Scheduled Pinned Locked Moved C#
helpquestioncsharpgraphics
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dfn
    wrote on last edited by
    #1

    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.

    J 1 Reply Last reply
    0
    • D dfn

      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.

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • J Judah Gabriel 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

        D Offline
        D Offline
        dfn
        wrote on last edited by
        #3

        Sorry, here it is. 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();
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups