【发布时间】:2012-12-12 17:28:30
【问题描述】:
我尝试在 VS 2010 中创建一个托管 C++ 程序集,以与 WinAPI 交互并在我的其他 C# 程序集中使用它。我已经阅读了所有帖子,甚至在 GitHub 中的代码中搜索过,但都没有成功。 也许是关于错误消息中的__clrcall,不应该是__stdcall吗?有什么想法吗?
确切的错误信息是:
错误 C2440: '=' : 无法从 'LRESULT (__clrcall xxx::Win32Demo::* )(HWND,UINT,WPARAM,LPARAM)' 转换为 'WNDPROC'
源代码:
#pragma once
using namespace System;
using namespace System::Drawing;
#include "stdafx.h"
#include "windows.h"
namespace xxx
{
ref class Win32Demo
{
private: HWND__ * handle;
private: static Char * windowClass;
public:
Win32Demo(void)
{
}
static Win32Demo()
{
tagWNDCLASSEXW w;
windowClass = (wchar_t*) L"Hello";
w.cbSize = sizeof(tagWNDCLASSEXW);
w.style = 0x803;
w.lpfnWndProc = WindowProc; // Error
w.cbClsExtra = 0;
w.cbWndExtra = 0;
w.hInstance = 0;
w.hIcon = 0;
w.hCursor = 0;
w.hbrBackground = CreateSolidBrush(0);
w.lpszMenuName = NULL;
w.lpszClassName = windowClass;
w.hIconSm = 0;
}
public :
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return 0;
}
};
}
【问题讨论】:
-
试试
w.lpfnWndProc = (WNDPROC) WindowProc; -
@BartekBanachewicz 这可能行不通。它将消除编译器错误,但您可能会遇到运行时错误。
-
试试
w.lpfnWndProc = &Win32Demo::WindowProc; -
OMG:2020 年有人对此投了赞成票 :-),顺便说一句。
标签: visual-c++ c++-cli