gpt4 book ai didi

c# - DLImport c# 控制台应用程序导入 c++ DLL

转载 作者:行者123 更新时间:2023-11-28 02:12:27 26 4
gpt4 key购买 nike

<分区>

我无法让它正常工作。我使用 c++ DLL 项目、c++ ConsoleApplication 和 c# ConsoleApplication 创建了一个测试解决方案 (VS2015)。两个控制台应用程序都在其主要方法中调用 DLL 函数并将结果打印到控制台。所有项目都构建到同一目录。

这在 C++ ConsoleApplication 中运行良好,因为我只是添加了对 DLL 项目的引用并包含了 header 。

在 c# ConsoleApplication 中,我无法让它工作。我知道它的非托管代码并且我需要使用 DLLImport。

当我尝试运行 c# 应用程序时,它会在调用 test() 时引发 System.EntryPointNotFoundException。并说找不到入口点“测试”。

那我做错了什么?

DLL.h
#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif

DLL_API int test(void);

DLL.cpp
#include "DLL.h"
// This is an example of an exported function.
DLL_API int test(void)
{
return 42;
}

编译为 DLL.dll。

Programm.cs
using System;
using System.Runtime.InteropServices;

namespace DLLTestCSharp
{

class Program
{

[DllImport("DLL.dll")]
public static extern int test();

static void Main(string[] args)
{

Console.WriteLine(test());

Console.Read();
}
}
}

编译为 DLLTestCSharp.exe。

All files are on the same path.

26 4 0
文章推荐: php - 将 Git 与 Wordpress 结合使用
文章推荐: javascript - 如何使 float HTML