[英]Unable to find an entry point named 'HelloFromCPP'
我是DLL的新手,我的任务是用几种语言编写一些DLL,并在它们之间创建依赖关系。 我已经设法创建一个C#DLL并从C#应用程序导入它,以及创建一个C DLL并从C#应用程序导入它。 但是现在我正在尝试2件事:
我对第二个问题一无所知。 我搜索了,但是找不到我理解的东西。 如果您能帮助我,我会很高兴。
关于第一个问题; 我编写了一个C ++ DLL和一个C#应用程序,根据一些指南,我发现它应该可以工作,但对我来说不起作用。 C ++ DLL文件:
#pragma once
namespace HelloCPPLibrary
{
class MyFunctions
{
public:
static __declspec(dllexport) char* HelloFromCPP();
};
}
#include "stdafx.h"
#include "HelloCPPLibrary.h"
using namespace std;
namespace HelloCPPLibrary
{
extern "C" {
char* HelloFromCPP() {
return "Hello from c++ dll";
}
}
}
using System;
using System.Runtime.InteropServices;
namespace TestDll
{
class Program
{
[DllImport(@"C:\Users\amitb\OneDrive\מסמכים\Visual Studio 2015\Projects\HELLODLL2\Debug\HELLODLL2")]
public static extern IntPtr HelloFromC();
[DllImport(@"C:\Users\amitb\OneDrive\מסמכים\Visual Studio 2015\Projects\HELLODLL3\Debug\HELLODLL3", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr HelloFromCPP();
static void Main(string[] args)
{
Console.WriteLine(Marshal.PtrToStringAnsi(HelloFromC()));
Console.WriteLine(Marshal.PtrToStringAnsi(HelloFromCPP()));
Console.ReadKey();
}
}
}
运行时,应用程序崩溃,并在标题中返回错误。
首先确保该函数实际上已导出:
在Visual Studio命令提示符中,使用dumpbin / exports what.dll
比EntryPoint =“?HelloFromCPP @ MyFunctions @ HelloCPPLibrary @@ SAPADXZ”
已在Visual Studio 2013中测试。
构建DLL。 您需要__declspec(dllexport)
来导出函数。 这使该函数对DLL的调用者可见。 extern "C"
去除了C ++名称处理。 名称修饰对于C ++区分具有相同名称但具有不同参数的函数是必需的。 并区分不同类别中的同名功能。
#include "stdafx.h"
using namespace std;
namespace HelloCPPLibrary
{
extern "C" {
__declspec(dllexport) char* HelloFromCPP() { return "Hello from c++ dll"; }
}
}
从C ++程序调用DLL
typedef char * (*fun_ptr) (); // fun_ptr is a pointer to a function which returns char * and takes no arguments
void main() {
HMODULE myDll = LoadLibrary("HelloCppLibrary.dll");
if (myDll != NULL) {
auto fun = (fun_ptr)GetProcAddress(myDll, "HelloFromCPP");
if (fun != NULL)
cout << fun() << endl;
else
cout << "Can't find HelloFromCpp" << endl;
FreeLibrary(myDll);
}
else {
cout << "Can't find HelloCppLibrary.dll" << endl;
cout << "GetLastError()=" << GetLastError() << endl;
}
}
从C#调用DLL
[DllImport("HelloCppLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr HelloFromCPP();
public static void Main(string[] args)
{
Console.WriteLine( Marshal.PtrToStringAnsi(HelloFromCPP()) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.