DLL
extern "C" __declspec(dllexport) void GetDefaultPrinterName(char* name); extern "C" __declspec(dllexport) void GetDefaultPrinterName(char* name) { // }
调用代码
#include <iostream> #include<windows.h> #include<string> using namespace std; #define TEST_DLL_PATH "D:\\Office-5.0-project\\c\\windows-printer\\Debug\\TPrinter.dll" #define TEST_FUNCTION_NAME "GetDefaultPrinterName" typedef void( *TestFunction)(char*); int main() { cout << "Start" << endl; HINSTANCE hDLL = LoadLibrary(TEST_DLL_PATH); if (hDLL == NULL) { cout << TEST_DLL_PATH << " ERROR: " << GetLastError() << endl; return -1; } cout << TEST_DLL_PATH << " " << hDLL << endl; TestFunction function=(TestFunction)GetProcAddress(hDLL,"GetDefaultPrinterName"); if (function == NULL) { cout << TEST_DLL_PATH << " ERROR:" << GetLastError() << endl; return -1; } cout << TEST_FUNCTION_NAME << " " << function << endl; char name[256] = {0}; function(name); cout<<"RESULT="<< name << endl; FreeLibrary(hDLL);//卸载dllTest.dll文件; cout << "End" << endl; return 0; }
测试结果
D:\Office-5.0-project\c\Debug>TaishanDllTest.exe Start D:\Office-5.0-project\c\windows-printer\Debug\TPrinter.dll 63B00000 GetDefaultPrinterName 63B012DF RESULT=Microsoft XPS Document Writer End