Using extern to call c/c++ libraries from c#

etienne_marais

Honorary Master
Joined
Mar 16, 2008
Messages
16,250
Reaction score
19,740
Location
Centurion
I have successfully used various c/c++ 3'rd party library functions by using dllimport and extern

Example:

[DllImport("ThirdPartyLibrary.dll ")]
internal static extern Int16 CreateRFIDCard(double parameter1);

Problem is that the libary now contains functions with TDateTime which seems incompatible with .NET CLR DateTime:

[DllImport("ThirdPartyLibrary.dll ")]
internal static extern Int16 CreateRFIDCard2(double parameter1, DateTime parameter2);

What do I need to do to pass a c# DateTime to the c/c++ function CreateRFIDCard2 (or better expressed, what is the c# type I need to use to corresponde to c/c++ TDateTime?)

Thanks
 
Seems I had to use double as parameter type where TDateTime is expected, and call ToOADateTime() on the DateTime object in c#
 
Well, you'll have to define TDateTime in c#. Hopefully its a struct (my borland exp was too long ago to recall:) )

Some more info here
http://stackoverflow.com/questions/9188523/marshaling-c-structures-in-c-sharp

https://msdn.microsoft.com/en-us/library/eshywdt7(v=vs.110).aspx

Good luck.


PS, alternatives: (if you don't manage to come right, using the right way ;) )
- would be to build your own native dll that wraps the calls to this external dll into a more c# friendly way)
OR
- you can write a mixed mode assembly ( managed c++ ) and use the third party dll natively (you'll still have to define TDateTime though as a normal c struct )
 
Top
Sign up to the MyBroadband newsletter
X