C# ';调用PInvoke函数..';c+错误+;c语言的DLL方法# 我必须使用C++代码中的C++项目中的DLL函数。
C++: 我使用DllImport导入如下内容: C#: 当我调用'ReadString'函数时,它会显示标准的'A call to PInvoke function'[…]”出现了“堆栈不平衡”错误C# ';调用PInvoke函数..';c+错误+;c语言的DLL方法# 我必须使用C++代码中的C++项目中的DLL函数。,c#,c++,dllimport,C#,C++,Dllimport,C++: 我使用DllImport导入如下内容: C#: 当我调用'ReadString'函数时,它会显示标准的'A call to PInvoke function'[…]”出现了“堆栈不平衡”错误 public string fooNameFunction(string strNAME, string strDefaultValue, int pathKey = Constants.REG_SUBT_TEMP, string serverName = null, long stnNumber
public string fooNameFunction(string strNAME, string strDefaultValue, int pathKey = Constants.REG_SUBT_TEMP, string serverName = null, long stnNumber = 0)
long lRegReturnValue;
lRegReturnValue = ReadString(strNAME, strValue, strDefaultValue, pathKey, serverName, stnNumber); //error here
return ""; //has nothing to do with my error
}
我已经尝试调用Convention.Cdecl,但错误仍然存在
在Windows中C++中, <代码>长< /COD>是32位,而<代码>长< /C> > C中为64位。所以
stnNumber
应该是int
。返回值也应该是int
,非零值表示true。这也有点像代码期望将一些文本参数返回给调用方。那需要<代码> StringBuilder <代码>,以及如何分配一个缓冲区的一些概念。@ DavidHeffernan,谢谢,我不知道C++中的<代码>长< /Cord>大小(我的坏)。
[DllImport("DHRegistry.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
static extern long ReadString([MarshalAs(UnmanagedType.LPTStr)] string valueName,
[MarshalAs(UnmanagedType.LPTStr)] string value,
[MarshalAs(UnmanagedType.LPTStr)] string defaultValue,
[MarshalAs(UnmanagedType.I4)] int subTree,
[MarshalAs(UnmanagedType.LPTStr)] string serverName,
[MarshalAs(UnmanagedType.I8)] long stnNumber);
public string fooNameFunction(string strNAME, string strDefaultValue, int pathKey = Constants.REG_SUBT_TEMP, string serverName = null, long stnNumber = 0)
long lRegReturnValue;
lRegReturnValue = ReadString(strNAME, strValue, strDefaultValue, pathKey, serverName, stnNumber); //error here
return ""; //has nothing to do with my error
}