月度归档: 2016 年 9 月

  • VC生成隐藏 受保护的系统目录 

    VC生成隐藏 受保护的系统目录

    /*  add by laowu 20160904   */
    TCHAR szPath[MAX_PATH];
    ZeroMemory(szPath, MAX_PATH);
    SHGetSpecialFolderPath(NULL, szPath, CSIDL_APPDATA, FALSE);
    strcat(szPath, "\\Winsys");
    
    if (!PathIsDirectory(szPath))
    {
           ::CreateDirectory(szPath, NULL);
           SetFileAttributes(szPath, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM );  // 主要是这句,设置文件夹的属性
    }
  • vc常用到的一些函数 

    vc常用到的一些函数 

    #include <Shlwapi.h>
    #pragma comment (lib , "shlwapi.lib" )
    
    #define nullptr NULL
    
    inline BYTE toHex(const BYTE &x)
    {
           return x > 9 ? x + 55: x + 48;
    }
    
    inline CString UrlEncode(CString sIn)
    {
           CString sOut;
           const int nLen = sIn.GetLength() + 1;
           register LPBYTE pOutTmp = NULL;
           LPBYTE pOutBuf = NULL;
           register LPBYTE pInTmp = NULL;
           LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
           BYTE b = 0;
    
           //alloc out buffer
           pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3 - 2);//new BYTE [nLen * 3];
    
           if(pOutBuf)
           {
                  pInTmp = pInBuf;
                  pOutTmp = pOutBuf;
    
                  // do encoding
                  while (*pInTmp)
                  {
                         if(isalnum(*pInTmp))
                               *pOutTmp++ = *pInTmp;
                         else
                               if(isspace(*pInTmp))
                                      *pOutTmp++ = '+';
                               else
                               {
                                      *pOutTmp++ = '%';
                                      *pOutTmp++ = toHex(*pInTmp>>4);
                                      *pOutTmp++ = toHex(*pInTmp%16);
                               }
                               pInTmp++;
                  }
                  *pOutTmp = '\0';
                  //sOut=pOutBuf;
                  //delete [] pOutBuf;
                  sOut.ReleaseBuffer();
           }
           sIn.ReleaseBuffer();
           return sOut;
    }
    
    inline int SplitString(CString & str, TCHAR cTok, CStringArray& aryItem)
    {
           TCHAR* p = str.GetBuffer(0);
           TCHAR* e = p;
           TCHAR cEnd = *e;
           int nCount = 0;
           while (cEnd)
           {
                  if (*e == _T('\0'))
                         cEnd = *e;
                  else if (*e == cTok)
                         *e = _T('\0');
    
                  if (*e)
                         e++;
                  else
                  {
                         if (*p != _T('\0'))
                         {
                               aryItem.Add(p);
                               nCount++;
                         }
                         p = ++e;
                  }
           }
           return nCount;
    }
    
    inline void MakeSureCreateDir(CString basePath, CString path)
    {
           CStringArray arr;
           SplitString(path, '\\', arr);
           for (int i = 0; i < arr.GetCount(); i++)
           {
                  basePath += L"\\" + arr[i];
    
                  if (!PathFileExists(basePath))
                         ::CreateDirectory(basePath, NULL);
           }
    }