简体   繁体   English

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

[英]How to create a DLL that will be used in C#

I have ICOP VDX-6354 board running Win CE. 我有运行Win CE的ICOP VDX-6354板。 I'm trying to control the buzzer of the board from my C# program. 我正试图从我的C#程序中控制电路板的蜂鸣器。 I tried all the playsound etc "coredll.dll" platform invokes. 我尝试了所有的playound等“coredll.dll”平台调用。 none of them worked so far. 到目前为止他们都没有工作。 So my last chance is to create my own 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;
}

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 我想将它编译为DLL,然后在我的C#程序中调用它

[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. 我想我构建了DLL。 I tried another way to build the DLL found here . 我尝试了另一种方法来构建这里找到的DLL。

Now, I copied the DLL to my C# startup project's Debug folder(Other DLLs of the project are also in this folder). 现在,我将DLL复制到我的C#启动项目的Debug文件夹(该项目的其他DLL也在此文件夹中)。 Then I try to invoke MyBeep function from MyBeep.DLL in my C# project by: 然后我尝试通过以下方式从我的C#项目中的MyBeep.DLL调用MyBeep函数:

[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'. 找不到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. 请检查我上面给出的链接,以便构建DLL以了解到目前为止我做了什么。 Regards. 问候。

There's two issues that you've got: 你有两个问题:

  1. You need to build the dll, not try and debug it. 你需要构建dll,而不是尝试调试它。 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]). 如果它与C#项目位于同一项目中,则将C#项目设置为启动项目(右键单击项目文件),如果不是,则只需选择Build而不是Start Debugging(如果使用快捷方式)键可能是Ctrl + Shift + B [如果使用C#环境设置]或F7 [如果您使用的是C ++环境设置])。

  2. You need to have the DLL in the right location. 您需要将DLL放在正确的位置。 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)\\*.*" 如果你想自动执行此操作,那么只需在C ++项目中添加一个构建后的步骤(项目属性,构建操作,如果我没记错,以及后期构建),这样做会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. 编辑:您还需要确保您的C ++项目在C#项目之前构建。 Right-click on your C# project file, and go to Project Dependencies, then tick the C++ library in the Depends on box. 右键单击您的C#项目文件,然后转到Project Dependencies,然后在Depends on框中勾选C ++库。 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. 要确保您的构建后步骤正常工作,请尝试单独构建C ++项目并检查它将DLL复制到C#项目中的正确目录。 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. 如果您按照该教程创建DLL,那么您应该没问题。

Even though the error message suggests otherwise, you should check the name of the exported function. 即使错误消息另有说明,您应该检查导出函数的名称。 For Win32 I always use this tool . 对于Win32,我总是使用这个工具 For a CE dll, maybe DUMPBIN /EXPORTS works. 对于CE dll,可能DUMPBIN / EXPORTS有效。

The functon name is likely to be called __MyBeep : I believe this prefix is a C convention. functon名称可能被称为__MyBeep :我相信这个前缀是一个C约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM