将VC ++代码转换为C#时出错 [英] Error while converting VC++ code to C#

查看:104
本文介绍了将VC ++代码转换为C#时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OneDll.h

OneDll.h

typedef int (WINAPI *PFCALLBACK)(int Param1,char * Param2);
extern   "C"   __declspec(dllexport)   void   WINAPI   CallBack(PFCALLBACK   Func);





DemoDlg.cpp



DemoDlg.cpp

CallBack(ProcessCallBackMessage);
int WINAPI ProcessCallBackMessage(int Param1,char *Param2)
{
	if(m_DlgWnd==NULL)
        return 0;
	::PostMessage(m_DlgWnd,WM_MSG_DLG_WND,Param1,(LPARAM)Param2);
	return 0;
}	





如何在C#中写入相同的颜色???



这就是我试过的....





how can I write Same in C#???

and this is what i've tried to do....

 public delegate void CALLBACK_PROC(int iType, char * buffer);
       CALLBACK_PROC myCallbackFunc = new CALLBACK_PROC(ProcessCallBackMessage);

public static void ProcessCallBackMessage(int iType, String buffer)
        {
            //string strIp = Marshal.PtrToStringAnsi((IntPtr)buffer, 2000);
            int h1 =(int) sIntHandle;//0;
            
            h1 = FindWindow(null, "LedSign");
            if (h1 != 0)
                SendMessage(h1, 2000, iType, (IntPtr)buffer);
        }





但这似乎不起作用....



But this doesn't seem to work....

推荐答案

你应该看看:

Marshal。 GetFunctionPointerForDelegate方法



可以这样声明你的代表:

You should get a look at:
Marshal.GetFunctionPointerForDelegate Method

May be you can declare your delegate like this:
public delegate void TheCallbackDelegate(int iType, String buffer);





并调用dll方法(用p / invoke声明):



And call the dll method (declared with p/invoke) :

Callback(Marshal.GetFunctionPointerForDelegate(new TheCallbackDelegate(ProcessCallbackMessage)));


这不是C ++,而是WinAPI调用IN C ++。你需要弄清楚这个代码在做什么,并找到如何在C#中做到这一点,没有直接的转换代码,因为API是不同的。即使是p / invoke也无济于事,我不这么认为。回调函数在C#中很容易,那些是委托,但实际上没有用。
This is not C++, it's WinAPI calls IN C++. You need to work out what this code was doing and find how to do it in C#, there's no direct conversion code, because the API is different. Even p/invoke won't help here, I don't think. Callback functions are easy in C#, those are delegates, but that does not help, really.


这篇关于将VC ++代码转换为C#时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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