从 C++ 触发 C# 中的事件并声明 LPCWSTR [英] Triggering an event in c# from c++ and declaring LPCWSTR

查看:55
本文介绍了从 C++ 触发 C# 中的事件并声明 LPCWSTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 WinCE7 设备上使用 C# 开发一个项目,但我似乎无法弄清楚如何使用我提供的 API.dll.API 用于控制 gpio 并包含输入的中断函数.我拥有的 api 文档非常有限,我无法访问代码本身.我已经设法使用轮询机制让一切正常工作,但我似乎无法设置中断.我已经阅读了很多关于 msdn 和 codeproject 的文章,并浏览了一些堆栈溢出问题,但我的代码仍然失败.我尝试使用的函数如下所示:

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

我已经获得了 PORT、TRIGGER_MODE 和 TRIGGER_POLARITY 的结构以及 szEventName 的以下描述:命名事件的事件名称".这就是我为此功能提供的全部内容,但我一直无法联系开发人员.我发现了一些关于将 LPCWSTR 与 pInvoke 一起使用的问题,修复程序要么将 CharSet 设置为 Charset.Unicode,要么将编组为 UnmanagedType.LPArray,但我确信修复程序适用于这种情况.此外,我是 C# 事件处理方面的新手,我也不确定我是否正确地做到了这一点.

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

编辑*:不确定我的标题是否正确,但要澄清一下:我想使用上述函数(我认为是用 C++ 编写的)在 C# 中触发一个事件

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

推荐答案

您的代码未显示对 GpioSetupInterruptPin 的关键调用.也就是说,我认为你把它复杂化了.只需将其定义为一个字符串即可.

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.

此外,我没有看到您在代码中创建命名系统事件.您发布的事件处理程序等与问题完全无关.您需要做的是 P/Invoke CreateEvent 与您传递给设置 API 的字符串名称相同,然后在该调用返回的句柄上使用 WaitForSingleObject.

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.

开源 SDF 在托管对象 OpenNETCF.THreading.EventWaitHandle 中提供这些东西 如果您不想自己编写它们.

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

这篇关于从 C++ 触发 C# 中的事件并声明 LPCWSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆