應用程式檢查必要的檔案和自動註冊控制項

來源:互聯網
上載者:User

 

#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);}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.