尝试将 c++ dll 导入 c-sharp 代码
Trying to import a c++ dll to a c-sharp code
我正在尝试做一个最简单的例子,以便我开始理解这是如何完成的。在网上搜索我只找到了我不太明白的例子。
这是 C++ 代码,在 C++ class 库项目中。
#include "stdafx.h"
#include <iostream>
#include "ClassLibrary1.h"
using namespace std;
extern "C" {
void CallMe()
{
cout << "I am the called function! Hooray!" << endl;
}
}
这是 C# 控制台应用程序代码:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("calling dll");
CallMe();
Console.ReadLine();
}
[DllImport("ClassLibrary1.dll")]
public static extern void CallMe();
}
}
我添加了 dll 作为对 C# 项目的引用,并将其放在与可执行文件相同的文件夹中。
我仍然得到“无法加载 DLL "ClassLibrary1.dll":找不到指定的模块(HRESULT 异常:0x8007007E)".
我错过了什么?
我尝试在 C++ 代码中的 CallMe 声明之前添加“__declspec(dllexport)”,但没有成功。
(代表OP发表).
解决方案分别是:
Still I get an "Unable to load DLL "ClassLibrary1.dll": the specified module could not be found (exception from HRESULT: 0x8007007E)".
错误 #1:dll 放置错误,抱歉。
Now I get a new error: "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"
错误 #2:c++ 代码被编译为 x86,而不是 x64,非常抱歉。
I now get the error: "Unable to find entery point named "CallMe" in dll"
错误 #3:当 declaring/defining 函数时,您必须放置“__declspec(dllexport)”。
我正在尝试做一个最简单的例子,以便我开始理解这是如何完成的。在网上搜索我只找到了我不太明白的例子。
这是 C++ 代码,在 C++ class 库项目中。
#include "stdafx.h"
#include <iostream>
#include "ClassLibrary1.h"
using namespace std;
extern "C" {
void CallMe()
{
cout << "I am the called function! Hooray!" << endl;
}
}
这是 C# 控制台应用程序代码:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("calling dll");
CallMe();
Console.ReadLine();
}
[DllImport("ClassLibrary1.dll")]
public static extern void CallMe();
}
}
我添加了 dll 作为对 C# 项目的引用,并将其放在与可执行文件相同的文件夹中。
我仍然得到“无法加载 DLL "ClassLibrary1.dll":找不到指定的模块(HRESULT 异常:0x8007007E)".
我错过了什么?
我尝试在 C++ 代码中的 CallMe 声明之前添加“__declspec(dllexport)”,但没有成功。
(代表OP发表).
解决方案分别是:
Still I get an "Unable to load DLL "ClassLibrary1.dll": the specified module could not be found (exception from HRESULT: 0x8007007E)".
错误 #1:dll 放置错误,抱歉。
Now I get a new error: "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"
错误 #2:c++ 代码被编译为 x86,而不是 x64,非常抱歉。
I now get the error: "Unable to find entery point named "CallMe" in dll"
错误 #3:当 declaring/defining 函数时,您必须放置“__declspec(dllexport)”。