#include <exception> #include <iostream> #include <sstream> using namespace std; void _declspec(dllexport) Login(){;} long _declspec(dllexport) LogRun(){cout << __FUNCTION__ << endl; return 0;} void _declspec(dllexport) Logout(){;} class _declspec(dllexport) SingletonHelper { protected: SingletonHelper(){ Logout(); } private: static SingletonHelper* instance; long m_uuid; public: ~SingletonHelper(){ Logout(); } static SingletonHelper* getInstance(){return instance;}; void run(){ m_uuid = LogRun(); } }; SingletonHelper* SingletonHelper::instance = new SingletonHelper; // int main(int argc, char* argv[]){ int main(int argc){ try { SingletonHelper* singletonHelper = SingletonHelper::getInstance(); singletonHelper->run(); if(IDCANCEL == MessageBox(NULL, CString("AA"), NULL, MB_YESNO)) return 1; throw 0; } catch (exception& e)// #include <exception> { stringstream ss; ss << e.what(); if(IDCANCEL == MessageBox(NULL, CString(ss.str().c_str()), NULL, MB_YESNO)); } catch (CException* e) // include <afx.h> before #inlude <windows.h> { CString cs; (void)e->GetErrorMessage(cs.GetBuffer(MAX_PATH), MAX_PATH); } catch (...){ if(IDCANCEL == MessageBox(NULL, CString("..."), NULL, MB_YESNO)); } return 0; } cl.exe SingletonHelper.exe dumpbin.exe -exports Helper.dll C和C++中动态链接库的创建和链接 1 创建MFC动态库with静态的 MFC dll 工程,代码函数头无需 _declspec(dllexport) 导出 2 设置环境 VC\vcvarsall.bat 变量 使用命令 dumpbin.exe -exports helloMfcHelp003.dll 3 创建MFC应用程序 (1)配置属性->VC++目录->在"包含目录"里添加头文件 Helper.h 所在的目录 (2)配置属性->VC++目录->在"库目录"里添加静态文件 Helper.lib 所在的目录