【发布时间】:2018-07-19 14:55:35
【问题描述】:
我正在尝试从 C# 代码调用我在 C++ 中构建的 dll。 但是,我收到以下错误:
抛出异常:'System.Runtime.InteropServices.SEHException' in dlltest_client.exe 类型未处理的异常 'System.Runtime.InteropServices.SEHException' 发生在 dlltest_client.exe 外部组件抛出异常。
我正在使用 cpp 代码构建 C++ dll,然后导入头文件:
dlltest.cpp:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "dlltest.h"
using namespace std;
// DLL internal state variables:
static string full_;
static string piece_;
void jigsaw_init(const string full_input, const string piece_input)
{
full_ = full_input;
piece_ = piece_input;
}
void findPiece()
{
cout << full_;
cout << piece_;
}
在哪里dlltest.h:
#pragma once
#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif
extern "C" DLLTEST_API void jigsaw_init(
const std::string full_input, const std::string piece_input);
extern "C" DLLTEST_API void findPiece();
这成功构建了 dlltest.dll
我应该使用 dll 的 C# 代码是
dlltest_client.cs
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport(@"path\dlltest.dll")]
private static extern void jigsaw_init(string full_input, string piece_input);
[DllImport(@"path\dlltest.dll")]
private static extern void findPiece();
static void Main(string[] args)
{
string full = @"path\monster_1.png";
string piece = @"path\piece01_01.png";
jigsaw_init(full, piece);
findPiece();
}
}
【问题讨论】: