2012-09-24 37 views
0

我有下面的代碼:如何調用C從C#/ ++ DLL的WPF項目

namespace WPFMuskTest 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     [DllImport 
      ("myDll.DLL", 
      EntryPoint = "[email protected]", 
      CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl 
      ) 
     ] 
     public static extern System.IntPtr Func1(out System.IntPtr handle, int type, out DateTime date); 

     public MainWindow() 
     { 
      InitializeComponent(); 
      // 
      // 
      // 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      System.IntPtr MainParam; 
      int thetype = 1 
      DateTime date; 

      System.IntPtr res = GetFxIRMoveForDate(out MainParam, thetype _til, out date); 
     } 
    } 
} 

exe文件是爲調用的DLL,並在DLL確實存在功能(在驗證了相同的路徑DependacyWalker),但我不斷收到錯誤:

函數原型被稱爲是:

類__declspec(dllexport)的OUR_DATE_TYPE { .... }

typedef unsigned long TYPE; typedef DATE_TYPE OUR_DATE_TYPE;

namespace1 
{ 
namespace2 
{ 
void func1(MyClass &myclass, const TYPE& type, const DATE_TYPE& date); 
} 
} 

型 'System.AccessViolationException' 未處理的異常

誰能告訴我爲什麼?

+0

我們真的需要在C#DLL中看到該方法的簽名,才能知道是否正確鍵入了DllImport。 –

+0

添加了C++原型 - 我猜這就是你的意思? – Stefan

+0

你能提供'TYPE'和'DATE_TYPE'的宏定義嗎? – Dai

回答

0

默認情況下,C++不使用cdecl調用約定,它使用stdcall。你可能會有更多的成功編寫c封裝到C++ api並調用它,因爲C有一個明確定義的ABI

編輯:再次查看您的代碼,我懷疑DateTime與您在C++中使用的日期類型相同。例如,如果大小錯誤,則可能會發生此錯誤。

+0

那麼應該改變調用約定到CallingConvention.Stdcall的工作?我試過了,錯誤是一樣的。我不確定如果我在C中爲我的包裝創建另一個包裝庫來工作 - 這肯定可以做到嗎? – Stefan