#pragma once #ifdef DLL_IMPLEMENT #define DLL_API __declspec(dllexport) #else #define DLL_API __declspec(dllimport) #endif namespace MyDLL { //导出类 class DLL_API MyDLL { public: MyDLL(); ~MyDLL(); int add(int x, int y); //简单方法 } ; } extern "C" DLL_API int add(int x, int y);
#include <iostream> #include <Windows.h> using namespace std; void main() { typedef int (*pAdd)(int x, int y); HINSTANCE instance = NULL; pAdd add =NULL; instance = ::LoadLibrary(/*(LPCTSTR)*/"MyDLL.dll"); if (instance == NULL) { MessageBox(NULL,"加载失败","温馨提示",0); return ; } else { add =(pAdd)GetProcAddress(instance,"add"); if (add != NULL) { int result = add(1000,1000); cout << result <<endl;//输出 2000 } else { cout << "没有函数" << endl; } } system("pause"); ::FreeLibrary(instance); return ; }
#define DLL_IMPLEMENT #include "MyDLL.h" namespace MyDLL { MyDLL::MyDLL() { } MyDLL::~MyDLL() { } int MyDLL::add(int x, int y) { return x+y; } } int add(int x, int y) { return (x+y); }