C++生成动态库,C++和C#以及Java在windows和linux调用

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、调测结果

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值