1

I've been working on a project using C# on a WinCE7 device and I can't seem to be able to figure out how to use an API.dll i've been provided with. The API is used to control the gpio and contains interrupt functions for the input. The documentation I have for the api is very limited and I do not have access to the code itself. I've managed to get everything working using a polling mechanism but i can't seem to set up the interrupt. I've read a lot of articles on msdn and codeproject and browsed through some stack overflow questions but my code still fails. The function I'm trying to use looks like this:

BOOL GpioSetupInterruptPin(HANDLE hGPIO, PIN_GPIO_PORT port, UINT pin, INT_TRIGGER_MODE mode, INT_TRIGGER_POLARITY polarity, LPCWSTR szEventName, BOOL enable);

I've been given the structures for the PORT, TRIGGER_MODE and TRIGGER_POLARITY and the following description for szEventName: "event name of a named event". That is all i've been given for this function and i've been unable to contact the developer. I've found a few questions about using LPCWSTR with pInvoke and the fixes were either setting the CharSet to Charset.Unicode or marshalling as UnmanagedType.LPArray, but i'm note sure the fixes apply for this case. Furthermore, I'm new at event handling in c# and I'm not sure I'm doing that correctly either.

        public class StartEventArgs : System.EventArgs
{
    private GPIOapi.PIN_GPIO_PORT port;
    private uint pin;
    public StartEventArgs(GPIOapi.PIN_GPIO_PORT port,uint pin)
    {
        this.port = port;
        this.pin = pin;
    }
}

public delegate void StartEventHandler(StartEventArgs e);

public class GPIO_In
{
    public event StartEventHandler TriggerDown;
    public event StartEventHandler TriggerUp;

    protected virtual void OnTriggerDown(StartEventArgs e)
    {
        if (TriggerDown != null)
            TriggerDown(e);
    }

    protected virtual void OnTriggerUp(StartEventArgs e)
    {
        if (TriggerUp != null)
            TriggerUp(e);
    }

    public void DoTrigger(GPIOapi.PIN_GPIO_PORT port, uint pin)
    {
        OnTriggerDown(new StartEventArgs(port, pin));
        //....
        OnTriggerUp(new StartEventArgs(port, pin));
    }
}

    private GPIO_In input;

    public Form1()
    {
        InitializeComponent();
        input=new GPIO_In();
        connect();
    }

    void connect()
    {input.TriggerDown+=new StartEventHandler(this.Check);}

    private void Check(StartEventArgs e)
    {
        GetInput(pin1, port6, pin_level);
    }

Does anyone have any idea how to use this function and would they be kind enough to share some of their experience? Thank you. Some of the resources I've tired:

http://msdn.microsoft.com/en-gb/library/aa288459(v=vs.71).aspx
http://msdn.microsoft.com/en-gb/library/aa645739(v=vs.71).aspx
http://stackoverflow.com/questions/7609225/c-to-c-sharp-event-handling
http://stackoverflow.com/questions/17073386/raise-events-in-c-cli-dll-and-consume-in-c-sharp
http://stackoverflow.com/questions/12576944/triggering-event-in-c-sharp-from-c-dll
http://www.codeproject.com/Questions/155043/Raise-C-event-and-catch-in-C
http://stackoverflow.com/questions/2969654/how-to-marshall-a-lpcwstr-to-string-in-c
http://www.codeproject.com/Questions/460787/Which-data-type-in-Csharp-equivalent-with-LPCWSTR
http://msdn.microsoft.com/En-US/library/aa288468(VS.71).aspx#pinvoke_example1

EDIT*: Not sure if I got the title right but to clarify: I want to trigger an event in C# using the above mentioned function (which I believe is written in C++)

  • 2
    I don't really understand what you're after... Your code doesn't call `GpioSetupInterruptPin`, and this function can't have any knowledge of C# events. You'd have to pass it a callback function for it to be able to trigger anything. I guess `szEventName` is some *other* kind of event, not a C# event. – Lucas Trzesniewski Nov 24 '14 at 15:00
  • GpioSetupInterruptPin is the setup function and i call it after my win form initializes. As far as i can tell, there may be an event generating an interrupt in the DLL and I would have to send it a delegate to a method i want executed. I think it's similar to this question: [link](http://stackoverflow.com/questions/12576944/triggering-event-in-c-sharp-from-c-dll?lq=1) but i can't figure out how to pass it as szEventName. I've been struggling with this for about a week and I might have ended up confusing myself – Vlad Stefan Nov 24 '14 at 15:21

1 Answers1

1

Your code doesn't show the critical call to GpioSetupInterruptPin. That said, I think you're over-complicating it. Just define it as a string and be done with it.

Also, I'm not seeing you creating a named system event in your code. The event handlers, etc. that you posted are not at all relevant to the problem. What you need to do is P/Invoke CreateEvent with the same string name you pass to the setup API, then use WaitForSingleObject on the handle returned by that call.

The open-source SDF provides these things in a the managed object OpenNETCF.THreading.EventWaitHandle if you don't want to write them yourself.

ctacke
  • 66,480
  • 18
  • 94
  • 155