DLL wrapper and structs
-
Hi Everyone - I am trying to create a C# wrapper around a legacy C .DLL I am stuck on one implementation area... There is a DLL function called GetDateTime extern "C" bool GetDateTime( DateTimeStruct &dateTime ) is how the SDK says it is to be implementated. I have created a wrapper shell for this function below... [DllImport("CarChipSDK", EntryPoint="GetDateTime")] public static extern bool GetDateTime ( //// what goes here??? ); I have create the struc in BOTH the main program, and the shell [StructLayout(LayoutKind.Sequential)] public struct DateTimeStruct { public int year; public int month; public int day; public int hour; public int minute; public int second; } Here are the questions... 1) Do I need to create a struct in both the caller and callee classes? 2) How do i setup the caller class to send the struct to the callee class? thanks tony
-
Hi Everyone - I am trying to create a C# wrapper around a legacy C .DLL I am stuck on one implementation area... There is a DLL function called GetDateTime extern "C" bool GetDateTime( DateTimeStruct &dateTime ) is how the SDK says it is to be implementated. I have created a wrapper shell for this function below... [DllImport("CarChipSDK", EntryPoint="GetDateTime")] public static extern bool GetDateTime ( //// what goes here??? ); I have create the struc in BOTH the main program, and the shell [StructLayout(LayoutKind.Sequential)] public struct DateTimeStruct { public int year; public int month; public int day; public int hour; public int minute; public int second; } Here are the questions... 1) Do I need to create a struct in both the caller and callee classes? 2) How do i setup the caller class to send the struct to the callee class? thanks tony
I haven't done this before, but have been researching for my own project. This is how I think it should work... define the struct in your "callee" class namespace:
namespace myNS { [StructLayout(LayoutKind.Sequential)] public struct DateTimeStruct{...} class Callee{ [DllImport("CarChipSDK", EntryPoint="GetDateTime")] public static extern bool GetDateTime (out DateTimeStruct dateTime); } }
in "Caller" add MyNS as reference, add new DateTimeStruct variable and then call your function, passing this variable by reference:using MyNS; ... DateTimeStruct dts; ... Callee.GetDateTime(out dts); ... // now you can use dts members: int hr = dts.hour;
Let me know if this works. Tym!