从 C# 调用 C++ dll 抛出 SEHException
calling a C++ dll from C# throwing a SEHException
我正在尝试从 C# 代码调用我用 C++ 构建的 dll。
但是,我收到以下错误:
Exception thrown: 'System.Runtime.InteropServices.SEHException' in
dlltest_client.exe An unhandled exception of type
'System.Runtime.InteropServices.SEHException' occurred in
dlltest_client.exe External component has thrown an exception.
我正在使用 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();
}
}
您不能将 C++ std::string
用于非托管互操作,这是您的 DLL 抛出异常的原因。
改为使用指向空终止字符数组的指针在 C# 代码和非托管 C++ 代码之间传递字符串。
另一个错误是 C++ 代码使用 cdecl 调用约定,但 C# 代码采用 stdcall。需要让界面的两侧匹配,换一个匹配另一个。
我正在尝试从 C# 代码调用我用 C++ 构建的 dll。 但是,我收到以下错误:
Exception thrown: 'System.Runtime.InteropServices.SEHException' in dlltest_client.exe An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in dlltest_client.exe External component has thrown an exception.
我正在使用 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();
}
}
您不能将 C++ std::string
用于非托管互操作,这是您的 DLL 抛出异常的原因。
改为使用指向空终止字符数组的指针在 C# 代码和非托管 C++ 代码之间传递字符串。
另一个错误是 C++ 代码使用 cdecl 调用约定,但 C# 代码采用 stdcall。需要让界面的两侧匹配,换一个匹配另一个。