如何创建将在 C# 中使用的 DLL

发布于 2024-12-12 08:53:34 字数 1874 浏览 0 评论 0原文

我有运行 Win CE 的 ICOP VDX-6354 板。我正在尝试从我的 C# 程序控制主板的蜂鸣器。我尝试了所有 playsound 等“coredll.dll”平台调用。到目前为止,他们都没有工作。所以我最后的机会是创建我自己的 DLL。

unsigned char inp(short addr)
{
    unsigned char cValue;
    _asm
    {
        mov dx, addr
        in ax, dx
        mov cValue, al
    }
    return cValue;
}
void outp(int addr, unsigned char val)
{
     __asm
    {
        push edx
        mov edx, DWORD PTR addr
        mov al, BYTE PTR val
        out dx, al
        pop edx
    }
}
bool MyBeep(DWORD dwFreq, DWORD dwDuration)
{
    outp(0x43, 0xb6); // Set Buzzer
    outp(0x42, (0x1234dc / dwFreq)); // Frequency LSB
    outp(0x42, (0x1234dc / dwFreq) >> 8); // Frequency MSB
    outp(0x61, inp(0x61) | 0x3); // Start beep
    Sleep(dwDuration);
    outp(0x61, inp(0x61) & 0xfc); // End beep
    return TRUE;
}

上述代码可在电路板的数据表中找到。我想将它编译为 DLL,然后在我的 C# 程序中调用它,就像

[DllImport("Buzzer.dll", EntryPoint = "MyBeep")]
public static extern void MyBeep(uint dwFreq, uint dwDuration);

我在编译时使用如下前缀一样:

extern "C" __declspec(dllexport) bool MyBeep(DWORD dwFreq, DWORD dwDuration)

这样我就可以控制蜂鸣器了。我的问题是我无法成功编译它。我按照此处的步骤操作,但它对我没有帮助。

我应该一步步做什么?

编辑:

我想我构建了 DLL。我尝试了另一种方法来构建 DLL 在此处找到

现在,我将 DLL 复制到我的 C# 启动项目的 Debug 文件夹中(项目的其他 DLL 也在这个文件夹中)。然后我尝试通过以下方式从 C# 项目中的 MyBeep.DLL 调用 MyBeep 函数:

[DllImport("MyBeep.dll", EntryPoint = "MyBeep")]
public static extern bool MyBeep(UInt32 dwFreq, UInt32 dwDuration);

但它给出了以下异常。

找不到 PInvoke DLL“MyBeep.dll”。

我错过了什么吗?请检查上面给出的我为构建 DLL 而作弊的链接,以了解我到目前为止所做的事情。 问候。

I have ICOP VDX-6354 board running Win CE. I'm trying to control the buzzer of the board from my C# program. I tried all the playsound etc "coredll.dll" platform invokes. none of them worked so far. So my last chance is to create my own DLL.

unsigned char inp(short addr)
{
    unsigned char cValue;
    _asm
    {
        mov dx, addr
        in ax, dx
        mov cValue, al
    }
    return cValue;
}
void outp(int addr, unsigned char val)
{
     __asm
    {
        push edx
        mov edx, DWORD PTR addr
        mov al, BYTE PTR val
        out dx, al
        pop edx
    }
}
bool MyBeep(DWORD dwFreq, DWORD dwDuration)
{
    outp(0x43, 0xb6); // Set Buzzer
    outp(0x42, (0x1234dc / dwFreq)); // Frequency LSB
    outp(0x42, (0x1234dc / dwFreq) >> 8); // Frequency MSB
    outp(0x61, inp(0x61) | 0x3); // Start beep
    Sleep(dwDuration);
    outp(0x61, inp(0x61) & 0xfc); // End beep
    return TRUE;
}

The code above is available in the datasheet of the board. I want to compile it as a DLL then invoke it in my C# program like

[DllImport("Buzzer.dll", EntryPoint = "MyBeep")]
public static extern void MyBeep(uint dwFreq, uint dwDuration);

I used a prefix as follows when I compiled:

extern "C" __declspec(dllexport) bool MyBeep(DWORD dwFreq, DWORD dwDuration)

So that hopefully I would be able to control the buzzer. My problem is I couldn't be successful compiling it. I followed the steps here but it didn't help me.

What should I do step by step?

EDIT:

I think I built the DLL. I tried another way to build the DLL found here.

Now, I copied the DLL to my C# startup project's Debug folder(Other DLLs of the project are also in this folder). Then I try to invoke MyBeep function from MyBeep.DLL in my C# project by:

[DllImport("MyBeep.dll", EntryPoint = "MyBeep")]
public static extern bool MyBeep(UInt32 dwFreq, UInt32 dwDuration);

But it gives the following exception.

Can't find PInvoke DLL 'MyBeep.dll'.

Am I missing something? Please check the links given above that I cheated to build the DLL to understand what I did so far.
Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

痴情换悲伤 2024-12-19 08:53:38

您遇到了两个问题:

  1. 您需要构建 dll,而不是尝试调试它。如果它与您的 C# 项目位于同一项目中,则将 C# 项目设置为启动项目(右键单击项目文件),如果不是,您只需选择“构建”而不是“开始调试”(如果您使用的是快捷方式)键,这可能是 Ctrl+Shift+B [如果使用 C# 环境设置] 或 F7 [如果您使用 C++ 环境设置])。

  2. 您需要将 DLL 放置在正确的位置。如果您想自动执行此操作,则只需向 C++ 项目添加一个构建后步骤(项目属性、构建操作(如果我没记错的话)以及构建后),该步骤的作用类似于 copy "$(TargetPath)" "$ (SolutionDir)\CsProj\bin\$(ConfigurationName)\*.*"

其中一些宏可能有点偏离,但您应该了解总体思路。

编辑:您还需要确保您的 C++ 项目是在 C# 项目之前构建的。右键单击 C# 项目文件,转到“项目依赖项”,然后在“依赖项”框中勾选 C++ 库。为了确保您的构建后步骤正常运行,请尝试单独构建 C++ 项目,并检查它将 ​​DLL 复制到 C# 项目中的正确目录。如果没有,它将在输出窗口中标记错误。如果您按照该教程创建了 DLL,那么应该没问题。

There's two issues that you've got:

  1. You need to build the dll, not try and debug it. If it's in the same project as your C# project then set the C# project as the startup project (right-click on the project file), if not you'll just have to select Build rather than Start Debugging (if you're using shortcut keys this will probably be Ctrl+Shift+B [if using the C# environment setup] or F7 [if you're using the C++ environment setup]).

  2. You need to have the DLL in the right location. If you want to automate this then just add a post-build step to the C++ project (project properties, build actions if I recall correctly, and post-build) which does something like copy "$(TargetPath)" "$(SolutionDir)\CsProj\bin\$(ConfigurationName)\*.*"

Some of those macros might be a little off, but you should get the general idea.

EDIT: You also need to make sure that your C++ project is building before your C# project. Right-click on your C# project file, and go to Project Dependencies, then tick the C++ library in the Depends on box. To make sure your post-build step is working try just building the C++ project on its own and checking it copies the DLL to the correct directory in your C# project. It'll flag errors in the output window if it doesn't. If you followed that tutorial to create a DLL you should be ok.

一向肩并 2024-12-19 08:53:38

即使错误消息另有提示,您也应该检查导出函数的名称。对于 Win32,我总是使用这个工具。对于CE dll,可能DUMPBIN /EXPORTS 有效。

函数名称很可能被称为 __MyBeep:我相信这个前缀是 C 约定。

Even though the error message suggests otherwise, you should check the name of the exported function. For Win32 I always use this tool. For a CE dll, maybe DUMPBIN /EXPORTS works.

The functon name is likely to be called __MyBeep: I believe this prefix is a C convention.

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