none
pinvoke passing class object instead of struct giving "The tag is invalid" error RRS feed

  • Question

  • Following in the struct which I pass to pinvoke managed call. If I just replace struct keyword with class I get

      “The tag is invalid” error (RPC_S_INVALID_TAG = 1733).

        Does anyone know how to fix it or the relavant team alias with which I can followup? I want to convert the struct to class so that I can clean the unmanaged memory inheriting idisposable interface.

     

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]

        public struct DHCP_SUBNET_ELEMENT_DATA_V4

        {

     

            /// DHCP_SUBNET_ELEMENT_TYPE->_DHCP_SUBNET_ELEMENT_TYPE_V5

            public DHCP_SUBNET_ELEMENT_TYPE ElementType;

     

            /// _DHCP_SUBNET_ELEMENT_DATA_V4__DHCP_SUBNET_ELEMENT_UNION_V4

            public _DHCP_SUBNET_ELEMENT_DATA_V4__DHCP_SUBNET_ELEMENT_UNION_V4 Union1;

     

            public void Dispose()

            {

                Union1.Dispose();

            }

        }

     

     

            [System.Runtime.InteropServices.DllImportAttribute("dhcpsapi.dll", EntryPoint = "DhcpAddMScopeElement", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]

            public static extern uint DhcpAddMScopeElement([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string ServerIpAddress, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string MScopeName, ref DHCP_SUBNET_ELEMENT_DATA_V4 AddElementInfo);

     

    Monday, October 22, 2012 12:56 PM

Answers

  • You can't simply swap struct for class when doing interop. With a struct "ref DHCP_SUBNET_ELEMENT_DATA" ends up as a pointer to struct on the native side. With a class you get a pointer to pointer on the native side. Try without "ref".
    Tuesday, October 23, 2012 7:31 AM
    Moderator

All replies

  • class cannot behave as structure. That's why you get that error.

    What is the problem in using struct? you can still implement IDisposable on struct.

    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct DHCP_SUBNET_ELEMENT_DATA_V4 : IDisposable
    {
        ...
        public void Dispose() 
        { 
            Union1.Dispose(); 
        }
    }
    

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, October 23, 2012 5:34 AM
  • You can not implement idisposable. And all the point is to clean resources in finalizer. struct does not have finalizer.
    Tuesday, October 23, 2012 5:47 AM
  • You can't simply swap struct for class when doing interop. With a struct "ref DHCP_SUBNET_ELEMENT_DATA" ends up as a pointer to struct on the native side. With a class you get a pointer to pointer on the native side. Try without "ref".
    Tuesday, October 23, 2012 7:31 AM
    Moderator