Hello everybody
i have developed a small program in c# that calls a function form a DLL (function originally have written by C++, i have not any problem in calling or sending data into function, i mean other functions works fine also this function does it's job but i think the returned value by parameter is not going to be marshaled properly so the parameters were null after execution.
please consider the following code that is original c++ function (in my DLL):
__declspec(dllimport) int __stdcall ReadFile (int type, char* data1, char* data2, char* data3);
this method signature show a function that reads some data from a hardware and returns back them in data1, data2, data3 into my c# program, consider that i have not access to the source code of DLL to change it, anyway i must solve the problem in c# program.
i know there was 2 major way to deal with DLL, my approach is :
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int ReadFile(int arg1, [MarshalAs(UnmanagedType.LPStr)] StringBuilder data1, [MarshalAs(UnmanagedType.LPStr)] StringBuilder data2, [MarshalAs(UnmanagedType.LPStr)] StringBuilder data3);
IntPtr dll = LoadLibrary("mydll.dll");
IntPtr readFileAdrs = GetProcAddress(dll, "ReadFile");
ReadFile read = (ReadFile)Marshal.GetDelegateForFunctionPointer(readFileAdrs, typeof(ReadFile));
StringBuilder d1 = new StringBuilder(100);
StringBuilder d2 = new StringBuilder(100);
StringBuilder d3 = new StringBuilder(100);
ReadFile(1, d1, d2, d3);
i am completely sure that function(DLL) works fine (because it was tested in other environment like Delphi, C++) but in C# it does not returns the data and all parameters is null after execution.
actually i have tested other types instead of
StringBuilder like
char[], string ,
IntPtr,
unit[] also i have test them with ref and out qualifier .
tanks in advance;