2012-02-08 124 views
3

我需要从C#应用程序调用VC++ dll的回调函数。以下是VC++中的回调函数。从C#调用VC++ dll的回调函数#

INT_PTR CALLBACK My_Proc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    . . . . . 
} 

我已经导入了dll,但我不知道如何从C#调用函数。有什么想法吗?

public class testClass 
{ 
    internal static class UnsafeNativeMethods 
    { 
     const string _dllLocation = "test.dll"; 

     [DllImport(_dllLocation, CallingConvention = CallingConvention.Cdecl)] 
     public static extern int My_Proc(int value1, int value2, Int64 value3, int value4);// am getting stuck here 

    } 
} 
+0

,什么是问题???另外'Int64'似乎是错误的。 – leppie 2012-02-08 08:44:32

回答

2

正确的声明是:

[DllImport("test.dll", CallingConvention = CallingConvention.StdCall)] 
    public static extern IntPtr My_Proc(IntPtr hDlg, int message, IntPtr wparam, IntPtr lparam); 

这是一个原生对话的对话过程声明。 Windows应该叫它,而不是你。当新的Windows消息可用于该对话框时,它会这样做。它很少从DLL中导出,这也可以解释问题。获得正确的窗口句柄(hDlg)也不容易。但是你没有很好地记录你的问题,所以我只能猜测。

在VC
0

++你应该使用DLL手柄:

hInst = ::GetModuleHandle("test.dll"); 
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, My_Proc);