DLL import and parameters
-
;)Hello, i'm using a dll with VC++ 6.0 i would like to use it with C#. My problem is that some type of data are not usable with c#. For exemple i can use char *xx as parameter , but when i want to use a pointer on a structure, it seems impossible to have it working. MyStruct { string field1; int field2; } [DllImport(@"C:\DLL\MyDll.dll",EntryPoint="myfunction",CharSet=CharSet.Ansi)] unsafe public static extern int myfunction(uint param1,short short param2, MyStruct *pointeronStruct); It is not working, [DllImport(@"C:\DLL\MyDll.dll",EntryPoint="ReadCard",CharSet=CharSet.Ansi)] unsafe public static extern int SecondFunction(uint param1,short param2, byte *field1, int *field2); so i need to split the structure, when there is only 2 fields, it's not a problem, but when you have 10 fields, it's not the same. As i cannot modify the dll, i decided to include this dll in a dll where is solve thoses kind of problem and then use the new dll with .NET. Does someone know how to solve this ? Thanks by advance
-
;)Hello, i'm using a dll with VC++ 6.0 i would like to use it with C#. My problem is that some type of data are not usable with c#. For exemple i can use char *xx as parameter , but when i want to use a pointer on a structure, it seems impossible to have it working. MyStruct { string field1; int field2; } [DllImport(@"C:\DLL\MyDll.dll",EntryPoint="myfunction",CharSet=CharSet.Ansi)] unsafe public static extern int myfunction(uint param1,short short param2, MyStruct *pointeronStruct); It is not working, [DllImport(@"C:\DLL\MyDll.dll",EntryPoint="ReadCard",CharSet=CharSet.Ansi)] unsafe public static extern int SecondFunction(uint param1,short param2, byte *field1, int *field2); so i need to split the structure, when there is only 2 fields, it's not a problem, but when you have 10 fields, it's not the same. As i cannot modify the dll, i decided to include this dll in a dll where is solve thoses kind of problem and then use the new dll with .NET. Does someone know how to solve this ? Thanks by advance
I had a similar problem with a dll developed in Delphi, and using the same in VC++. First you will have to find out the equivalent data types in C# for all the parameters used in the dll. Most of the data types will be the same except for the structure. Declare a similar structure in C#, create an object of the same, and then pass the address of the object as a long value( which would get type casted automatically in the dll ). Hope this would solve your problem. :) "A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors."