簡體   English   中英

如何從C#/ WPF項目中調用C ++ DLL

[英]How do I call a C++ DLL from C#/WPF project

我有以下代碼:

namespace WPFMuskTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport
            ("myDll.DLL", 
             EntryPoint = "?Func1@FUllNameCopiedFromDependancyWalker",
             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”的未處理異常

誰能告訴我為什么?

默認情況下,c ++不使用cdecl調用約定,而是使用stdcall 您可能會更成功地將ac包裝器寫入c ++ api並調用它,因為C具有定義明確的ABI

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM