静态方法调用dll函数及类

来源:互联网 发布:mac下搭建lamp环境 编辑:程序博客网 时间:2024/06/06 17:52

与动态调用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;

就可以了


0 0
原创粉丝点击