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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. MarshalDirectiveException

MarshalDirectiveException

Scheduled Pinned Locked Moved C#
csharphelp
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.
  • K Offline
    K Offline
    korrea80
    wrote on last edited by
    #1

    Hi all!. I would like use unmanaged code from C in C#. I built a DLL with C code with this functions:

    struct GetPluginData
    {
    int data[22];
    };

    DLLEXPORT extern "C" __declspec (dllexport) GetPluginData GetDataArray(int number);

    In C# I've got this code:

    [StructLayoutAttribute(LayoutKind.Sequential, Pack=1)]
    public unsafe struct GetPluginData
    {
    /// int[22]
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 22, ArraySubType = UnmanagedType.I4)]
    public int[] data;
    }

    [DllImport("RBRPlugin.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "GetDataArray")]
    [return: MarshalAs(UnmanagedType.Struct)]
    public static extern GetPluginData GetDataArray(int number);

    In button event or othe place code, I wrote this:

    GetPluginData tes = GetDataArray(1);

    And I'm getting the error: The type signature of this method is not PInvoke compatible. I'm looking for information throught google, but no result found... Thanks in advance!

    Richard Andrew x64R 1 Reply Last reply
    0
    • K korrea80

      Hi all!. I would like use unmanaged code from C in C#. I built a DLL with C code with this functions:

      struct GetPluginData
      {
      int data[22];
      };

      DLLEXPORT extern "C" __declspec (dllexport) GetPluginData GetDataArray(int number);

      In C# I've got this code:

      [StructLayoutAttribute(LayoutKind.Sequential, Pack=1)]
      public unsafe struct GetPluginData
      {
      /// int[22]
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 22, ArraySubType = UnmanagedType.I4)]
      public int[] data;
      }

      [DllImport("RBRPlugin.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "GetDataArray")]
      [return: MarshalAs(UnmanagedType.Struct)]
      public static extern GetPluginData GetDataArray(int number);

      In button event or othe place code, I wrote this:

      GetPluginData tes = GetDataArray(1);

      And I'm getting the error: The type signature of this method is not PInvoke compatible. I'm looking for information throught google, but no result found... Thanks in advance!

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      There is another way you can marshal the return type from that function. First, declare the return type from the C function to be a pointer to the GetPluginData structure instead of the structure itself. Declare the return type as an IntPtr in C#, and then use the functions in the Marshal class to read the structure from unmanaged memory into a managed structure.

      The difficult we do right away... ...the impossible takes slightly longer.

      K 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        There is another way you can marshal the return type from that function. First, declare the return type from the C function to be a pointer to the GetPluginData structure instead of the structure itself. Declare the return type as an IntPtr in C#, and then use the functions in the Marshal class to read the structure from unmanaged memory into a managed structure.

        The difficult we do right away... ...the impossible takes slightly longer.

        K Offline
        K Offline
        korrea80
        wrote on last edited by
        #3

        Hi! Thanks for the info Richard. The solution was write for Hans Passans in StackOverflow:

            \[DllImport("RBRPlugin.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "GetDataArray")\]
            public static extern GetPluginData GetDataArray(int number);
            
            \[StructLayoutAttribute(LayoutKind.Sequential)\]
            public struct GetPluginData
            {
                public unsafe fixed int data\[22\];
            }
        
            public GetPluginData temp1 = new GetPluginData();
        
            private void button1\_Click(object sender, EventArgs e)
            {
                GetPluginData test = GetDataArray(1);
                temp1 = test;
                unsafe
                {
                    fixed (GetPluginData\* p = &temp1)
                    {
                        p->data\[0\] = 1;
                    }
                }
            }
        

        Like you read, the principal problem was the array in GetPluginData struct, it must be fixed. Thaks!

        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