Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OneDll.h
C++
typedef int (WINAPI *PFCALLBACK)(int Param1,char * Param2);
extern   "C"   __declspec(dllexport)   void   WINAPI   CallBack(PFCALLBACK   Func);


DemoDlg.cpp
C++
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;
}	


how can I write Same in C#???

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

C#
 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....
Posted
Updated 18-Jun-13 19:54pm
v3

You should get a look at:
Marshal.GetFunctionPointerForDelegate Method

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


And call the dll method (declared with p/invoke) :
C#
Callback(Marshal.GetFunctionPointerForDelegate(new TheCallbackDelegate(ProcessCallbackMessage)));
 
Share this answer
 
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.
 
Share this answer
 
Comments
johannesnestler 19-Jun-13 10:32am    
In general I agree - but for the code shown by OP I'd use p/invoke for the API-Functions (FindWindow, SendMessage) and just re-wrap the calls from C#. But if he is willing to do that (I assume he has more code than showed), he may also just call the existing C++ library and use interop... Porting code - always fun :-(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900