Triggering an event in c# from c++ and declaring LPCWSTR

Vlad Stefan

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++)

ctacke

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Declaring C# properties

分類Dev

C ++でのLPCWSTRへの文字列

分類Dev

C - Declaring variables and calling malloc

分類Dev

Declaring an array of pointers in C++

分類Dev

C# FileSystemWatcher not triggering correctly in Service

分類Dev

C++ declaring a static object in a class

分類Dev

C++ Forward Declaring Classes Within Classes

分類Dev

Declaring variable inside functions of a class in C++

分類Dev

`mousemove` and triggering a custom event

分類Dev

Event triggering on slide appearance

分類Dev

C ++:WCHARをLPCWSTRに変換する-実際の動作例

分類Dev

C: Forward declaring a typedef that will be defined later for being used in declaring a function now

分類Dev

Stop Triggering Event on Page Load

分類Dev

Emitting a Node.js Event from a C++ callback function

分類Dev

Event to prevent C# DataGridView from changing the current row

分類Dev

C# preventing combox selectionChanged event from recalling itself

分類Dev

How to subscribe to C# GUI event from ProjectA in dll in ProjectB

分類Dev

Convert event declaration from C# to Visual Basic 2010

分類Dev

エラーC2440: '=': 'const char *'から 'LPCWSTR'に変換できません

分類Dev

C++ declaring static enum vs enum in a class

分類Dev

program returning garbage values even after declaring variables in c

分類Dev

Is declaring a class in C++ as "final : public virtual" EVER useful?

分類Dev

Will the future C++ standard support "Declaring type template parameters with auto"

分類Dev

Are data type identifiers necessary when declaring a function in C?

分類Dev

Declaring an array of class type (having problems with Borland C++)

分類Dev

c ++からc#でイベントをトリガーし、LPCWSTRを宣言します

分類Dev

jQuery: Finish dragging without triggering click event

分類Dev

React - Triggering click event on table row

分類Dev

Click event not triggering for every iteration of for loop

Related 関連記事

  1. 1

    Declaring C# properties

  2. 2

    C ++でのLPCWSTRへの文字列

  3. 3

    C - Declaring variables and calling malloc

  4. 4

    Declaring an array of pointers in C++

  5. 5

    C# FileSystemWatcher not triggering correctly in Service

  6. 6

    C++ declaring a static object in a class

  7. 7

    C++ Forward Declaring Classes Within Classes

  8. 8

    Declaring variable inside functions of a class in C++

  9. 9

    `mousemove` and triggering a custom event

  10. 10

    Event triggering on slide appearance

  11. 11

    C ++:WCHARをLPCWSTRに変換する-実際の動作例

  12. 12

    C: Forward declaring a typedef that will be defined later for being used in declaring a function now

  13. 13

    Stop Triggering Event on Page Load

  14. 14

    Emitting a Node.js Event from a C++ callback function

  15. 15

    Event to prevent C# DataGridView from changing the current row

  16. 16

    C# preventing combox selectionChanged event from recalling itself

  17. 17

    How to subscribe to C# GUI event from ProjectA in dll in ProjectB

  18. 18

    Convert event declaration from C# to Visual Basic 2010

  19. 19

    エラーC2440: '=': 'const char *'から 'LPCWSTR'に変換できません

  20. 20

    C++ declaring static enum vs enum in a class

  21. 21

    program returning garbage values even after declaring variables in c

  22. 22

    Is declaring a class in C++ as "final : public virtual" EVER useful?

  23. 23

    Will the future C++ standard support "Declaring type template parameters with auto"

  24. 24

    Are data type identifiers necessary when declaring a function in C?

  25. 25

    Declaring an array of class type (having problems with Borland C++)

  26. 26

    c ++からc#でイベントをトリガーし、LPCWSTRを宣言します

  27. 27

    jQuery: Finish dragging without triggering click event

  28. 28

    React - Triggering click event on table row

  29. 29

    Click event not triggering for every iteration of for loop

ホットタグ

アーカイブ