9

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.

Oğuz Sezer
  • 320
  • 3
  • 15
  • 1
    What failed? Error messages? How did you build? In short, more details please. – David Heffernan Oct 27 '11 at 14:56
  • +1 seems like a very real question, not sure why someone voted to close. – Chris Marisic Oct 27 '11 at 15:03
  • it just didn't compile successfully. As I stated in my question, I followed a "how to" tutorial step by step. When I tried to compile it asked for "Executable for Debug Session". And the only executable available on the dropbox is RegSvr32. When I choose RegSvr32 and continue, it says usage : regsrv32 [/u][/s][/n][/i] etc. on a seperate warning screen and it doesn't give any error in the compiler. – Oğuz Sezer Oct 27 '11 at 15:13
  • 1
    You can't Start a dll for debugging (without an exe to host it). Instead of hitting the "Play" (eg, Debug) button, find the "Compile without debugging" command. – Sam Axe Oct 27 '11 at 15:26
  • Visual Studio 2005. empty C++ project (application type DLL). – Oğuz Sezer Oct 27 '11 at 15:27
  • @Boo Can you be more clear please, I didn't do this before. – Oğuz Sezer Oct 27 '11 at 15:28
  • uhm, no. That's as clear as I am capable of being. Good luck. – Sam Axe Oct 27 '11 at 15:29
  • @OğuzSezer - If you want help then post the errors the compiler provided. A great first start would be to stop trying to debug something that isn't an application. We need more informatin..as is...this question cannot be given an answer. – Security Hound Oct 27 '11 at 16:52
  • @Ramhound VS doesn't give any errors. regsrv32 warning screen pops up and compilation stops. On the warning pop-up screen, it says usage : regsrv32 [/u][/s][/n][/i]. Maybe someone did this before can tell me how to build a DLL that will be used in C# – Oğuz Sezer Oct 28 '11 at 05:33
  • @Oğuz Sezer - If you application cannot find the dll then its not in the right location. Where are you putting it? Why are you trying to register it, you don't need to do that, this isn't a COM library. – Security Hound Oct 28 '11 at 11:00
  • @Ramhound I copied the DLL to my C# startup project's Debug folder(Other DLLs of the project are also in this folder) – Oğuz Sezer Nov 04 '11 at 12:33

2 Answers2

1

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.

blaaaaaaah
  • 184
  • 1
  • 4
0

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.