Windows生成dllC++库
1、创建动态链接库项目
源文件编写函数
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"int sum(int a, int b) {return a + b;
}BOOL APIENTRY DllMain( HMODULE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{switch (ul_reason_for_call){case DLL_PROCESS_ATTACH:case DLL_THREAD_ATTACH:case DLL_THREAD_DETACH:case DLL_PROCESS_DETACH:break;}return TRUE;
}
头文件编写如下
// pch.h: 这是预编译标头文件。
// 下方列出的文件仅编译一次,提高了将来生成的生成性能。
// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。
// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。
// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。#ifndef PCH_H
#define PCH_H// 添加要在此处预编译的标头
#include "framework.h"#endif //PCH_H#define DLLAPI extern "C" _declspec(dllexport)
DLLAPI int sum(int a, int b);
Release生成dll
Linux生成C++.so文件
1、创建项目
2、编写方法生成so文件
extern "C" {int sumSo(int a, int b) {return a + b;}
}
C++项目调用
1、创建空项目,源文件编写如下
#include <iostream>
#include "windows.h"typedef int (*addfunction)(int ,int);
HMODULE dll = LoadLibrary(L"wuzhidll.dll");int main() {if (dll) {std::cout << "DLL Ok\n";addfunction sum_ = (addfunction)GetProcAddress(dll, "sum");if (sum_ != NULL) {std::cout << "HOOK set up success";std::cout << "调用sum函数" << sum_(300, 500) << std::endl;}else { std::cout << "HOOK is no"; }}else{std::cout << "DLL No\n";}return 0;
}
将dll拷贝在项目中,调试生成
C#调用dll
1、创建调用类
internal static class SumInfo{public const string DLL = "wuzhidll.dll";public const string So = "libConsoleSo.so";[DllImport(DLL)]public static extern int sum(int a,int b);[DllImport(So)]public static extern int sumSo(int a, int b);}
2、调试调用
// See https://aka.ms/new-console-template for more information
using ConsoleApp1;
int a = 24;
int b = 2000;
int c = SumInfo.sum(a, b);
Console.WriteLine(c);
Console.ReadLine();
3、调测
4、linux调试
Java调用C++dll和So
1、创建控制台项目、编写代码
package org.example;import com.sun.jna.Native;public class App
{public static void main( String[] args ){String os = System.getProperty("os.name");int sum=0;//判断操作系统if (os != null && os.toLowerCase().startsWith("windows")) {sum = MyLibrary.INSTANCE.sum(5, 6);} else if (os != null && os.toLowerCase().startsWith("linux")) {sum = MyLibraeySo.INSTANCESo.sum(5, 6);} else { //其它操作系统System.out.println(String.format("当前系统版本是:%s", os));}System.out.println( "调用C++函数获取求和结果:"+sum );}
}
package org.example;import com.sun.jna.Library;
import com.sun.jna.Native;public interface MyLibrary extends Library {String currentPath = System.getProperty("user.dir")+"\\wuzhidll.dll";MyLibrary INSTANCE = (MyLibrary) Native.loadLibrary(currentPath,MyLibrary.class);int sum(int a, int b);
}
package org.example;import com.sun.jna.Native;public interface MyLibraeySo {String currentPathSo = System.getProperty("user.dir")+"\\libConsoleSo.so";MyLibrary INSTANCESo = (MyLibrary) Native.loadLibrary(currentPathSo,MyLibrary.class);int sum(int a, int b);
}
注意pom文件添加jna的引用
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>com.sun.jna</groupId><artifactId>jna</artifactId><version>3.0.9</version></dependency></dependencies>
2、调测结果