【发布时间】:2021-01-21 01:42:14
【问题描述】:
我尝试在我的 C# 项目中实现一个 C++ DLL。我给出了以下头文件:
#ifdef EBEXPORT
const long EBDECL
#else
const long EBDECL __declspec(dllimport)
#endif
const long EBCALL __stdcall
#if defined (__cplusplus)
extern "C"
{
#endif
EBDECL long EBCALL EbInitTcp(long nUnit, const char* pIP, long nIPSize);
#if defined (__cplusplus)
}
#endif
这是我用来在 C# 中实现 DLL 的代码:
using System.Runtime.InteropServices;
namespace NoName
{
class DLLImport
{
[DllImport("C:/lib/host.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern long EbInitTcp(long nUnit, string pIP, long nIPSize);
public void Init()
{
string ip = "192.168.0.1";
EbInitTcp(1, ip, ip.Length));
}
}
}
如果我执行代码,我会得到 PInvokeStackImbalance 异常。 你能帮帮我吗?
【问题讨论】:
-
int EbInitTcp(int nUnit, string pIP, int nIPSize). -
调用约定通常用
#define定义,即#define EBCALL __stdcall