KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to call the DhcpGetClientInfo API from C# but I have a question on conversion of this C struct to C#: typedef struct _DHCP_CLIENT_SEARCH_INFO { DHCP_SEARCH_INFO_TYPE SearchType; union { DHCP_IP_ADDRESS ClientIpAddress; DHCP_CLIENT_UID ClientHardwareAddress; LPWSTR ClientName; } SearchInfo; } DHCP_SEARCH_INFO, *LPDHCP_SEARCH_INFO; I think the Correct conversion is this: [StructLayout(LayoutKind.Explicit, Size=12)] public struct DHCP_SEARCH_INFO { [FieldOffset(0)] public DHCP_SEARCH_INFO_TYPE SearchType; [FieldOffset(4)] public DHCP_IP_ADDRESS ClientIpAddress; [FieldOffset(4)] public DHCP_BINARY_DATA ClientHardwareAddress; [FieldOffset(4), MarshalAs(UnmanagedType.LPWStr)] public string ClientName; }; But that gives an System.TypeLoadException: Additional information: Could not load type 'Dhcpsapi.DHCP_SEARCH_INFO' from assembly 'ConsoleApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 4 that is incorrectly aligned or overlapped by a non-object field. This is the conversion of the other types in case you want to compile: public enum DHCP_SEARCH_INFO_TYPE : uint { DhcpClientIpAddress = 0, DhcpClientHardwareAddress = 1, DhcpClientName = 2 }; [StructLayout(LayoutKind.Sequential)] public struct DHCP_BINARY_DATA { public uint DataLength; public IntPtr Data; }; [StructLayout(LayoutKind.Sequential)] public struct DHCP_IP_ADDRESS { public UInt32 IPAddress; } EDIT: I verified sizeof and offsets in C: #pragma comment(lib,"Dhcpsapi.lib") int _tmain(int argc, _TCHAR* argv[]) { DHCP_SEARCH_INFO si; printf("sizeof(DHCP_SEARCH_INFO)=%d\n", sizeof(DHCP_SEARCH
Tags (comma-separated)
Save Edits
Cancel