使用C#调用DLL

发布于 2024-11-08 23:51:49 字数 874 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮光沉寂 2024-11-15 23:51:49

fdOpen 使用默认参数 - 这只能意味着您正在尝试从 DLL 导出 C++ 函数。结果是“fdOpen”在导出表中被“名称损坏”,看起来像“fdOpen@YAXPB_W0I@Z”。

最好将此函数导出为 C。如下所示声明并定义 fdOpen:

extern "C" fdGlove* __stdcall fdOpen(char* pPort, bool bOnlyAllowSingleConnection);

其他可能的问题:

  1. DLL 与尝试加载它的 EXE 不在同一目录中。

  2. 您忘记从 DLL 中导出该函数。您需要在函数定义上使用 .DEF 文件或 __declspec(dllexport) 属性。使用“dumpbin /exports fglove.dll”来确定是否是这种情况。

  3. 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:

extern "C" fdGlove* __stdcall fdOpen(char* pPort, bool bOnlyAllowSingleConnection);

Other possible issues:

  1. The DLL isn't in the same directory as the EXE trying to load it.

  2. 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.

  3. Confusion between stdcall and cdecl compile options. I get it confused, so try replacing "_stdcall" with "_cdecl" above. Or try it without either attribute.

琉璃繁缕 2024-11-15 23:51:49

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.

享受孤独 2024-11-15 23:51:49

查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文