C# and old "C" Dlls
-
Im in the process of learning C#, and so far, I'm loving it. I've found a way to make calls to some old "C" DLLs, but I have a few questions regarding types. [DllImport("MYDLL.DLL")] public static extern ulong GetSerialNumbers ( byte f1, byte f2, byte NodeAddr, PIFS_CABLESER pserials, PUSHORT pcount ); I have a function in in MYDLL.DLL called GetSerialNumbers that returns an array of structures. I have no problems declaring functions with known types, but as soon as I add in a type that C# doesn't know, I'm stuck. I would like to take the data of these structures and copy them to a class I've created.... This is my C definition, would I have to make something equivalent that C# will understand? typedef struct SerialInfoMD { byte ID[8]; byte something; int new; } IFS_CABLESER, *PIFS_CABLE_SER; Anybody have any info to share? Thanks! Mike
-
Im in the process of learning C#, and so far, I'm loving it. I've found a way to make calls to some old "C" DLLs, but I have a few questions regarding types. [DllImport("MYDLL.DLL")] public static extern ulong GetSerialNumbers ( byte f1, byte f2, byte NodeAddr, PIFS_CABLESER pserials, PUSHORT pcount ); I have a function in in MYDLL.DLL called GetSerialNumbers that returns an array of structures. I have no problems declaring functions with known types, but as soon as I add in a type that C# doesn't know, I'm stuck. I would like to take the data of these structures and copy them to a class I've created.... This is my C definition, would I have to make something equivalent that C# will understand? typedef struct SerialInfoMD { byte ID[8]; byte something; int new; } IFS_CABLESER, *PIFS_CABLE_SER; Anybody have any info to share? Thanks! Mike
Yes, as with the requirement of defining the function signature in C# you also have to supply a description of the structure as well. For the structure you provided the sig would be [StructLayout(LayoutKind.Sequential)] struct SerialInfoMD { [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)] public byte[] ID; public byte Something; public int New; // note the capitalization change. Syntax error otherwise. } Things can get tricky when marshalling data between managed and unmanaged code. For example, in your function signature you have the return type as ulong. Is the return type for the C function an unsigned long? Unsigned long is 32-bits on Win32 but is 64-bits on .NET so the appropriate return type in the C# method is uint if the return type on the C function is unsigned long. It gets even more murky when dealing with arrays. You might wanna check out information on data marshalling on MSDN. Try this URL for a start. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconInteropMarshaling.asp