在C和x2B之间传递数据时结果不一致+;DLL与C#GUI 虽然我有一个合理的C++经验,但我还是C初学者的一部分。我当前的项目要求我在C++ DLL和C语言GUI之间来回传递数据。我主要是通过阅读stackoverflow上的回复来了解如何做到这一点的。不幸的是,我遇到了一个让我陷入困境的问题。DLL是使用g++(gcc版本4.2.1 mingw32-2)编译的,我正在使用VisualStudio2010构建GUI

在C和x2B之间传递数据时结果不一致+;DLL与C#GUI 虽然我有一个合理的C++经验,但我还是C初学者的一部分。我当前的项目要求我在C++ DLL和C语言GUI之间来回传递数据。我主要是通过阅读stackoverflow上的回复来了解如何做到这一点的。不幸的是,我遇到了一个让我陷入困境的问题。DLL是使用g++(gcc版本4.2.1 mingw32-2)编译的,我正在使用VisualStudio2010构建GUI,c#,c++,dll,C#,C++,Dll,我的问题是,我可以从一些DLL函数而不是其他函数将数据输入C#。令人恼火的是,它似乎是不一致的,因为有些函数工作,而有些不工作。为了说明我的意思,我已经包含了C++的导入代码和下面的C++导出声明。我真的很感激你给我一些建议,因为我一直在想如何解决这个问题 此功能工作正常: [DllImport("mstTools.dll", EntryPoint = "mstLastError", CallingConvention = CallingConvention.Cdecl)] private

我的问题是,我可以从一些DLL函数而不是其他函数将数据输入C#。令人恼火的是,它似乎是不一致的,因为有些函数工作,而有些不工作。为了说明我的意思,我已经包含了C++的导入代码和下面的C++导出声明。我真的很感激你给我一些建议,因为我一直在想如何解决这个问题

此功能工作正常:

[DllImport("mstTools.dll", EntryPoint = "mstLastError", CallingConvention =    CallingConvention.Cdecl)]
private static extern IntPtr LastError();

public static string mstGetLastError()
{
  return Marshal.PtrToStringAnsi(LastError());
}
在DLL头中声明如下:

extern "C" __declspec(dllexport) const char* mstLastError ();
此函数不起作用,返回空字符串:

[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 ));
}
它在DLL中声明如下:

extern "C" __declspec(dllexport) const char* mstGetMetadata ( char* CStgMapName );
使用VisualStudio调试器,我可以看到导入的DLL函数(GetMetadata)返回null

相反,返回布尔工作的函数,例如:

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

extern "C" __declspec(dllexport) bool mstMapExists ( char* CStgMapName );
这个函数的工作原理与我预期的完全相同,因为它在应该的时候返回true/false

但返回双精度的函数返回NaN:

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

extern "C" __declspec(dllexport) double mstGetResolution ( char* CStgName );
对发生的事情有什么想法吗

感谢和问候,
迈克< /P>编写测试C++程序,并调用C++中的方法来查看它们是否做了同样的事情。我通常会像这样做一个健全性检查,以确保我没有忘记一些初始化或导致方法按原来的方式工作的东西。这与GCC ABI不同。
[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);