从C+引发事件时出现问题+;用C语言处理# 我试图编写一个使用C++ DLL的基于C的WPF应用程序。C#应用程序用于用户界面,它具有WPF的所有优点。C++ DLL使用Win32函数(例如枚举窗口)。 现在我希望C++ DLL能够提高在C语言应用程序中可以处理的事件。这就是我所尝试的(基于): //cpp文件 #使用 使用名称空间系统; 结构赢{ HWND手柄; 字符类名称; 字符标题; }; 代表无效wDel(WIN); 事件wDel^wE; 无效窗口(赢窗口){ 我们(窗口); }

从C+引发事件时出现问题+;用C语言处理# 我试图编写一个使用C++ DLL的基于C的WPF应用程序。C#应用程序用于用户界面,它具有WPF的所有优点。C++ DLL使用Win32函数(例如枚举窗口)。 现在我希望C++ DLL能够提高在C语言应用程序中可以处理的事件。这就是我所尝试的(基于): //cpp文件 #使用 使用名称空间系统; 结构赢{ HWND手柄; 字符类名称; 字符标题; }; 代表无效wDel(WIN); 事件wDel^wE; 无效窗口(赢窗口){ 我们(窗口); },c#,events,c++-cli,eventhandler,C#,Events,C++ Cli,Eventhandler,当我试图编译此代码时,会抛出以下错误: C3708:“wDel”:不正确使用“事件”;必须是兼容事件源的成员 C2059:语法错误:“事件” C3861:“我们”:未找到标识符 您的事件需要是某个托管类的成员,可能是静态的。e、 g: #include "stdafx.h" #include "windows.h" using namespace System; struct WIN { HWND Handle; char ClassName; char Title;

当我试图编译此代码时,会抛出以下错误:

C3708:“wDel”:不正确使用“事件”;必须是兼容事件源的成员

C2059:语法错误:“事件”

C3861:“我们”:未找到标识符


您的事件需要是某个托管类的成员,可能是静态的。e、 g:

#include "stdafx.h"
#include "windows.h"

using namespace System;

struct WIN {
    HWND Handle;
    char ClassName;
    char Title;
};

delegate void wDel(WIN);

ref class WindowEvents abstract sealed // abstract sealed ref class in c++/cli is like a static class in c#
{
    public:
        static event wDel^ wE;

        static void GotWindow(WIN Window) {
            wE(Window);
        }
};
更新

如果你需要,既然是,你可以考虑如下的事情:

#include "stdafx.h"
#include "windows.h"

using namespace System;

#pragma managed(push,off)

struct WIN {  // Unmanaged c++ struct encapsulating the unmanaged data.
    HWND Handle;
    char ClassName;
    char Title;
};

#pragma managed(pop)

public value struct ManagedWIN  // Managed c++/CLI translation of the above.
{
public:
    IntPtr Handle; // Wrapper for an HWND
    char   ClassName;
    char   Title;
    ManagedWIN(const WIN win) : Handle(win.Handle), ClassName(win.ClassName), Title(win.Title)
    {
    }
};

public delegate void wDel(ManagedWIN);

public ref class WindowEvents abstract sealed // abstract sealed ref class in c++/cli is like a static class in c#
{
    public:
        static event wDel^ wE;

    internal:
        static void GotWindow(WIN Window) {
            wE(ManagedWIN(Window));
        }
};

此处
ManagedWIN
仅包含安全的.Net类型。

事件关键字必须出现在
公共引用类中。此外,您必须使用托管的
value-struct
而不是本机的
struct
,以允许C#code访问结构成员。使用
value-struct
而不是
struct
@Jan Böhm-根据ArthurCPPCLI的建议更新答案。
#include "stdafx.h"
#include "windows.h"

using namespace System;

#pragma managed(push,off)

struct WIN {  // Unmanaged c++ struct encapsulating the unmanaged data.
    HWND Handle;
    char ClassName;
    char Title;
};

#pragma managed(pop)

public value struct ManagedWIN  // Managed c++/CLI translation of the above.
{
public:
    IntPtr Handle; // Wrapper for an HWND
    char   ClassName;
    char   Title;
    ManagedWIN(const WIN win) : Handle(win.Handle), ClassName(win.ClassName), Title(win.Title)
    {
    }
};

public delegate void wDel(ManagedWIN);

public ref class WindowEvents abstract sealed // abstract sealed ref class in c++/cli is like a static class in c#
{
    public:
        static event wDel^ wE;

    internal:
        static void GotWindow(WIN Window) {
            wE(ManagedWIN(Window));
        }
};