问题遇到的现象和发生背景
C#调用C++编写的DLL失败
编译器 MSVC 2022
问题相关代码,请勿粘贴截图
CmakeLists
cmake_minimum_required (VERSION 3.8)
project(DLLVersion1)
add_library(TMP_dll SHARED TMP_dll.cpp)
c++ 编写的DLL代码,文件名为TMP_dll.cpp
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
extern "C" __declspec(dllexport) void TestCode(unsigned long long data)
{
cout << data << endl;
}
c# 编写的调用代码
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace test1
{
class Program
{
[DllImport("C:\\Users\\26327\\Desktop\\CTPDLL\\out\\build\\x64-Debug\\TMP_dll.dll")]
public static extern void TestCode(UInt64 data);
static void Main(string[] args)
{
TestCode(5125518666);
}
}
}
运行结果及报错内容
System.DllNotFoundException: 'Unable to load DLL 'TMP_dll.dll' or one of its dependencies: 找不到指定的模块。