使用C#调用DLL
typedef struct
{
// The contents of this struct are platform-dependent and subject to
// change. You should not manipulate the contents of this struct directly.
/*New stuff*/
// HWND m_hWnd;
// HDEVNOTIFY m_hDevNotify;
} fdGlove;
FGLOVEAPI fdGlove *fdOpen(char *pPort, bool bOnlyAllowSingleConnection = false);
FGLOVEAPI int fdClose(fdGlove *pFG);
FGLOVEAPI int fdGetGloveHand(fdGlove *pFG);
FGLOVEAPI int fdGetGloveType(fdGlove *pFG);
我有一个名为 fglove.dll 的 DLL 文件,我需要使用 C# 来调用它。 我写了这样的代码:
[StructLayout(LayoutKind.Sequential)]
public class fdGlove
{
}
[DllImport("fglove.dll")]
public static extern fdGlove fdOpen(string pPort, bool bOnlyAllowSingleConnection);
但是当我调试程序时,它出现错误(无法在DLL“fglove.dll”中找到名为“fdOpen”的入口点。)
有人可以指出我做错了什么吗?
typedef struct
{
// The contents of this struct are platform-dependent and subject to
// change. You should not manipulate the contents of this struct directly.
/*New stuff*/
// HWND m_hWnd;
// HDEVNOTIFY m_hDevNotify;
} fdGlove;
FGLOVEAPI fdGlove *fdOpen(char *pPort, bool bOnlyAllowSingleConnection = false);
FGLOVEAPI int fdClose(fdGlove *pFG);
FGLOVEAPI int fdGetGloveHand(fdGlove *pFG);
FGLOVEAPI int fdGetGloveType(fdGlove *pFG);
I have a DLL file called fglove.dll and I need using C# to call it.
I have written code like this:
[StructLayout(LayoutKind.Sequential)]
public class fdGlove
{
}
[DllImport("fglove.dll")]
public static extern fdGlove fdOpen(string pPort, bool bOnlyAllowSingleConnection);
but when I debug the program it is has an error (Unable to find an entry point named 'fdOpen' in DLL 'fglove.dll'.)
Can someone point out what I have done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
fdOpen 使用默认参数 - 这只能意味着您正在尝试从 DLL 导出 C++ 函数。结果是“fdOpen”在导出表中被“名称损坏”,看起来像“fdOpen@YAXPB_W0I@Z”。
最好将此函数导出为 C。如下所示声明并定义 fdOpen:
其他可能的问题:
DLL 与尝试加载它的 EXE 不在同一目录中。
您忘记从 DLL 中导出该函数。您需要在函数定义上使用 .DEF 文件或 __declspec(dllexport) 属性。使用“dumpbin /exports fglove.dll”来确定是否是这种情况。
stdcall 和 cdecl 编译选项之间的混淆。我感到困惑,所以尝试将上面的“_stdcall”替换为“_cdecl”。或者尝试不使用任何一个属性。
fdOpen is using a default parameter - which can only mean you are trying to export a C++ function out of a DLL. The result is that "fdOpen" is getting "name mangled" in the export table as something that looks like "fdOpen@YAXPB_W0I@Z".
You're better off exporting this function as C. Declare and define fdOpen as follows:
Other possible issues:
The DLL isn't in the same directory as the EXE trying to load it.
You forgot to export the function from the DLL. You need to use a .DEF file or the __declspec(dllexport) attribute on the function definition. Use "dumpbin /exports fglove.dll" to figure out if this is the case.
Confusion between stdcall and cdecl compile options. I get it confused, so try replacing "_stdcall" with "_cdecl" above. Or try it without either attribute.
fglove 的编译器很可能会进行这种类型的名称修改。
使用
DUMPBIN fglove.dll
获取真实姓名。然后使用
[DllImport("fglove.dll", EntryPoint='...')]
其中 ... 是真实姓名。The compiler of fglove is most likely doing so type of name mangling.
Use
DUMPBIN fglove.dll
to get the real names.Then use
[DllImport("fglove.dll", EntryPoint='...')]
where ... is the real name.查看 Dependency Walker 中导出符号的名称。它很可能是 _fdImport 或类似的名称,您需要更新 DLLImportAttribute 以匹配导出的名称。
look at the name of the exported symbol in Dependency Walker. More than likely it will be _fdImport or something similar and you will need to update your DLLImportAttribute to match the exported name.