c++动态调用

  • 编写动态库

libAAA.h

#pragma once

#define WIN32_LEAN_AND_MEAN             
#include <windows.h>

#if defined(WIN32) || defined(_WIN32)
#define LIB_EXPORT _declspec(dllexport)
#else
#define LIB_EXPORT __attribute__((visibility("default")))
#endif

extern "C"  LIB_EXPORT int fnlibAAA_test(void);
extern "C"  LIB_EXPORT int fnlibAAA_sum(int a,int b);

 

libAAA.cpp

#include "libAAA.h"



LIB_EXPORT int fnlibAAA_test(void)
{
    return 55;
}

LIB_EXPORT int fnlibAAA_sum(int a, int b)
{
	return (a + b);
}
  • 调用方法 

main.cpp

#include <iostream>
#include <windows.h>

typedef int(*MYtestN)(void);
typedef int(*MYsumN)(int a,int b);

int main()
{
	int ret = 0;
	HMODULE dlllHandle = NULL;
	dlllHandle = ::LoadLibrary(L"D:\\myStudy\\c++Demo\\fnlibAAA\\x64\\Release\\fnlibAAA.dll");
	if (dlllHandle)
	{
		MYtestN testApi = (MYtestN)::GetProcAddress(dlllHandle, "fnlibAAA_test");
		if (!testApi)
		{
			std::cout << "GetProcAddress testApi fail\n";
		}
		else
		{
			ret= testApi();
			std::cout << "testApi result: " << ret << "\n";
		}

		MYsumN sumApi = (MYsumN)::GetProcAddress(dlllHandle, "fnlibAAA_sum");
		if (!sumApi)
		{
			std::cout << "GetProcAddress  sumApi fail\n";
		}
		else
		{
			ret = sumApi(10,20);
			std::cout << "sumApi result: " << ret << "\n";
		}
	}
	else
	{
		std::cout << "load lib fail\n";
	}

	std::cout << "game over !!! \n";
	system("pause");
	return 0;
}

 

  • 结果 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值