#include <shlwapi.h>#pragma comment(lib, "shlwapi.lib")//檢測需要的檔案是否存在//並且對需要註冊的檔案進行註冊//輸入參數用於指示檔案所在的路徑//返回TRUE表示成功BOOL Check_Environment(LPCTSTR lpszAppPath) //傳入檔案路徑{ //列出所需要的檔案清單 typedef struct _FILE_CELL { LPCTSTR szFileName; //檔案名稱 BOOL bNeedRegister; //需要註冊? }FILE_CELL; const FILE_CELL Support_FileList[] = { {_T("aaa.ocx"), TRUE}, {_T("bbb.ocx"), TRUE}, {_T("ccc.ocx"), TRUE}, {_T("fff.dll"), TRUE}, {_T("other.sys")}, //other need file 0, //Don't remove this line }; BOOL bRet = 0; TCHAR e_mess[4096], mess[256]; memset(e_mess, 0, sizeof(e_mess)); int uIndex = 0; while(Support_FileList[uIndex].szFileName) { TCHAR fileName[MAX_PATH + 1]; _tcscpy_s(fileName, lpszAppPath); PathAddBackslash(fileName); _tcscat_s(fileName, Support_FileList[uIndex].szFileName); if(PathFileExists(fileName)) { //if(_tcsicmp(PathFindExtension(fileName),_T(".ocx"))==0) //是需要註冊的OCX檔案 if(Support_FileList[uIndex].bNeedRegister) //需要註冊 { //使用短路徑名主要是為相容Win98 TCHAR shortfileName[MAX_PATH + 1]; GetShortPathName(fileName, shortfileName, MAX_PATH); // Path to OLE Control in shortfileName HMODULE hModule = LoadLibrary(shortfileName); if(hModule) { typedef HRESULT (STDAPICALLTYPE *CTLREGPROC)() ; // Requires olectl.h CTLREGPROC DLLRegisterServer = (CTLREGPROC)GetProcAddress(hModule,"DllRegisterServer" ) ; if(DLLRegisterServer) { if(DLLRegisterServer() != S_OK) { _stprintf_s(mess, _T("註冊模組失敗 %s\r\n"), fileName); _tcscat_s(e_mess, mess); } } else { _stprintf_s(mess, _T("模組%s找不到指定註冊入口\r\n"), fileName); _tcscat_s(e_mess, mess); } FreeLibrary(hModule) ; } else { _stprintf_s(mess, _T("載入模組失敗 %s\r\n"), fileName); _tcscat_s(e_mess, mess); } } } else //提示缺少必要的支援檔案 { _stprintf_s(mess, _T("缺少必要的檔案 %s\r\n"), fileName); _tcscat_s(e_mess, mess); } uIndex++; } if(_tcslen(e_mess) > 0) //檢測出錯提示 { MessageBox(NULL, e_mess, _T("Error"), MB_OK ); } else { bRet = true; } return(bRet);}