Inconsistent results when transferring data between C ++ DLL and C # GUI

I am still new to C #, although I have sufficient experience with C ++. My current project requires me to pass data back and forth between the C ++ DLL and the C # GUI. I basically learned how to do this by reading the answers here in stackoverflow. Unfortunately, I have a problem that made me stuck. The DLL is compiled using g ++ (gcc version 4.2.1 mingw32-2), and I use Visual Studio 2010 to create a GUI.

My problem is that I can get data in C # from some DLL functions, and not from others. The crazy thing is that it seems inconsistent, as some functions work and some do not. To show you what I mean, I have included the C # import code and the C ++ export declarations below. I would really appreciate some advice on this, as I am really fixated on how to fix this.

This function works fine:

[DllImport("mstTools.dll", EntryPoint = "mstLastError", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr LastError(); public static string mstGetLastError() { return Marshal.PtrToStringAnsi(LastError()); } 

This is declared in the DLL header as follows:

 extern "C" __declspec(dllexport) const char* mstLastError (); 

This function does not work and returns an empty string:

 [DllImport("mstTools.dll", EntryPoint = "mstGetMetadata", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr GetMetadata([MarshalAs(UnmanagedType.LPStr)]string StgMapName); public static string mstGetMetadata( string StgMapName ) { return Marshal.PtrToStringAnsi(GetMetadata( StgMapName )); } 

It is declared in the DLL as follows:

 extern "C" __declspec(dllexport) const char* mstGetMetadata ( char* CStgMapName ); 

Using the Visual Studio debugger, I see that the imported DLL function (GetMetadata) returns null.

Conversely, functions that return a bool operation, for example:

 [DllImport("mstTools.dll", EntryPoint = "mstMapExists", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] public static extern bool mstMapExists([MarshalAs(UnmanagedType.LPStr)]string StgMapName); 

which is declared in the DLL as follows:

 extern "C" __declspec(dllexport) bool mstMapExists ( char* CStgMapName ); 

This function works exactly as I expect, since it returns true / false when it should.

But the function returning double NaN returns:

 [DllImport("mstTools.dll", EntryPoint = "mstGetResolution", CallingConvention =CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.R8)] public static extern double mstGetResolution([MarshalAs(UnmanagedType.LPStr)]string StgMapName); 

which is declared in the DLL as:

 extern "C" __declspec(dllexport) double mstGetResolution ( char* CStgName ); 

Any ideas on what's going on?

Thanks and greetings, Mike

+6
source share
1 answer
 [DllImport("mstTools.dll", EntryPoint = "mstGetResolution")] public static extern decimal mstGetResolution([In]string StgMapName); [DllImport("mstTools.dll", EntryPoint = "mstGetMetadata")] private static extern IntPtr GetMetadata([In]string StgMapName); 
+1
source

Source: https://habr.com/ru/post/890464/


All Articles