2012-02-08 119 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);