c++程序员修炼真经之五

我们开始一个项目时通常都会做些什么呢?

1.判断本程序是否已经有实例在运行

BOOL IsPigRun()
{
 CString strIn;
 strIn = "hcnet3_running";
 HANDLE  ghMutexExe =0;
 ghMutexExe = CreateMutex(NULL, TRUE , strIn);
 if (ghMutexExe == NULL || GetLastError() == ERROR_ALREADY_EXISTS )
 {
  CloseHandle(ghMutexExe);
  return TRUE;
 }
 else{
  CloseHandle(ghMutexExe);
  return FALSE;
 }

2.设置当前程序运行路径

 char path_config[MAX_PATH];

 *path_config= 0;
 GetModuleFileName(NULL,path_config, MAX_PATH);
 if (*path_config)
  strrchr(path_config,'//')[1] = 0;

 SetCurrentDirectory(path_config);

3.取得配置文件路径

char * getIniPath(char *pszInifFileFullPath)
{
 char path[_MAX_PATH];
 char drv[_MAX_DRIVE];
 char dir[_MAX_DIR];
 char fName[_MAX_PATH];

 GetModuleFileName(NULL, path, _MAX_PATH);
 _splitpath(path, drv, dir, fName, NULL);
 _makepath(path, drv, dir, fName,"ini");

 strcpy(pszInifFileFullPath, path);
 return pszInifFileFullPath;
}

4.转换目录分隔符

char normalise_delim(char c) {
 if ('//' == c) {
  return '/';
 }
 return c;
}
string determineBinaryLocation(const string& launchcmd) {
 string path(launchcmd);
 string binloc;
 transform(path.begin(), path.end(), path.begin(), normalise_delim);
 string::size_type delim = path.find_last_of('/');

 if (string::npos == delim) {
  return binloc = ".";
 }
 return binloc = path.substr(0, delim+1);
}

5.忽略大小写的字符串转换

void to_lower_tool(string &srcstr)
{
 transform(srcstr.begin(),srcstr.end(),srcstr.begin(),tolower);
}
void to_ouper_tool(string & srcStr)
{
 transform(srcStr.begin(),srcStr.end(),srcStr.begin(),::toupper);
}

6.产生另一个进程或执行内置CAB程序

HINSTANCE  hinst;

hinst = ShellExecute(NULL, "open", pExe, url, NULL, SW_SHOWMAXIMIZED  );
   if ((long)hinst <= 32)
   {
    char szUName[1024];
    STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi;
    sprintf(szUName,"%s %s",pExe,url);
    if(!CreateProcess(NULL, szUName , NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
    {
     AfxMessageBox("无法打开默认浏览器! ");
     exit(0);
    }
    return 1;
   }
   if (strstr(pExe,"iexplore.exe"))
   {
    return 1 ;
   }
   else
   {
    return 0;
   }

执行内置CAB程序

sprintf(szCmd , "makecab %s %s" ,_addcurrentfile[0],zip_addcurrentfile[0]);

WinExec(szCmd ,SW_HIDE);

你可能感兴趣的:(C++,String,浏览器,null,url,Path)