1

我有以下代码:

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 无符号长类型;typedef DATE_TYPE OUR_DATE_TYPE;

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

“System.AccessViolationException”类型的未处理异常

谁能告诉我为什么?

4

1 回答 1

1

By default, c++ does not use the cdecl calling convention, it uses stdcall. You would probably have more success writing a c wrapper to the c++ api and calling that instead, because C has a well-defined ABI.

EDIT: looking at your code again, I doubt DateTime is the same as the date type you're using in C++. If it's the wrong size, for example, this error could occur.

于 2012-09-24T15:35:15.050 回答