静态方法调用dll函数及类

与动态调用dll类相比,静态方法就简单多了

先把dll的头文件和源文件贴出来

dll头文件.h

namespace FunName
{
	class Fun
	{
	public:
		__declspec(dllexport) int Add(int a,int b);
		__declspec(dllexport) int Sub(int a,int b);
	protected:
	private:
	};
}

extern "C" __declspec(dllexport) void show();  //这里时dll中的函数



dll源文件.cpp

#include "MyDll.h"
#include <stdexcept>
#include <iostream>

using namespace std;

namespace FunName
{
	__declspec(dllexport) int Fun::Add(int a,int b)
	{
		return a+b;
	}

	__declspec(dllexport) int Fun::Sub(int a,int b)
	{
		return a-b;
	}

	
}

extern "C" __declspec(dllexport) void show()
{
	cout<<"The show function is here!\n";
}



调用dll的源文件.cpp

// Test0.cpp : Defines the entry point for the console application.
//
//#pragma comment(lib,"MyDllTest.lib")

#include "stdafx.h"
#include "MyDll.h"
#pragma comment(lib,"MyDllTest.lib")
#include <iostream>

using namespace std;

int main()
{
	FunName::Fun fun;
	int i = fun.Add(5,8);
	int j = fun.Sub(9,5);
	cout<<"i = "<<i<<"\n";
	cout<<"j = "<<j<<"\n";

	show();

	return 0;
}




这里注意一下,如果不希望将类写在命名空间里面,除了在dll的头文件和源文件中去掉相应的部分,在调用时,将

FunName::Fun fun;

改写成:

Fun fun;

就可以了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值