Win32+API 知識積累

來源:互聯網
上載者:User

3.分析"...,...,..."類型的句子:
char *next_token,*token;
char seps[]   = " ,/t/n";
char string1[] ="A string/tof ,,tokens/nand some  more tokens";
char *token1=strtok_s( string1, seps, &next_token1);
while ((token1 != NULL))
{// Get next token:
 if (token1 != NULL)
        {
            printf( " %s/n", token1 );
            token1 = strtok_s( NULL, seps, &next_token1);
        }
}
4."c://123//a.exe"
wchar_t* pEnd=wcsrchr(wcFileName,wcDot);
if(pEnd!=NULL) *pEnd=wcNull;

5.a.匯出到硬碟中
void CLDRCapUI::OutputToDisk(CString strRoot)
{
 DWORD dwAttrs = GetFileAttributes(strRoot);
 if (dwAttrs==INVALID_FILE_ATTRIBUTES)
 {
  AfxMessageBox(_T("沒有找到I盤"));
  return;
 }

 char cMovie[256]={0};
 char cPhoto[256]={0};
 char cTo[256]={0};
 ::sprintf(cMovie,"%s",thePrefs.GetMovieHome());
 ::sprintf(cPhoto,"%s",thePrefs.GetPhotoHome());
 ::sprintf(cTo,"%sLRCap",strRoot);
 char  strTitle[] = "檔案正在拷貝...";    //進度條標題
 //如果沒有發現該檔案夾則建立此檔案夾
 CFileFind finder;
 BOOL bFileFind= finder.FindFile(cTo);
 if(!bFileFind)
  CreateDirectory(cTo,NULL); 

 //將圖片資訊從資料庫下到硬碟中
 int iRepositoryType=thePrefs.GetRepositoryType();
 if(0==iRepositoryType)//資料庫儲存
  ExportProcessedRecord(cPhoto);
 
 SHFILEOPSTRUCT   sfo;  
 sfo.hwnd   =   NULL;  
 sfo.wFunc   =   FO_COPY;
 sfo.pFrom   =cMovie;
 sfo.pTo   = cTo; 
 sfo.fFlags   =   /*FOF_SILENT*/   FOF_ALLOWUNDO|   FOF_NOCONFIRMATION   |   FOF_NOCONFIRMMKDIR;  
 sfo.hNameMappings = NULL;
    sfo.lpszProgressTitle = strTitle;

 int hr   =   SHFileOperation(&sfo); 
 if(hr!=0)
 {
  AfxMessageBox(_T("匯出影片失敗"));
  return;
 }

 sfo.pFrom   =cPhoto;
 hr   =   SHFileOperation(&sfo); 
 if(hr!=0)
 {
  AfxMessageBox(_T("匯出影片成功,但是匯出圖片失敗"));
  return;
 }
 AfxMessageBox(_T("匯出成功"));
}
b.
拷貝多個
比如在C盤上我有三個目錄和三個檔案,能否  
  用該函數一下子copy到d盤上去,是否還有其他  
  方法?

char     pFrom[1024];  
  char   *pNextBuf;  
  memset(pFrom,   0x00,   1024);  
  pNextBuf   =   pFrom  
  strcpy(pNextBuf,   "C://autoexec.bat");   //   第一個檔案  
  pNextBuf   +=   strlen(pNextBuf)+1;   //   空出一個字元'/0'  
  strcpy(pNextBuf,   "c://winnt//hh.exe");   //   下一個檔案  
  pNextBuf   +=   strlen(pNextBuf)+1;   //   空出一個字元'/0'  
  因為pFrom中的所有字元在開始時已全置為0,所以它肯定是以兩個'/0'字元結尾的
6.一次只運行一個程式執行個體
下列兩種方式都可以實現,建議採用第二種方式:
1、 if( FindWindow(NULL,"程式標題"))
         exit(0);
2、BOOL CDemoTBarEApp::InstanceIsRun()
{
 HANDLE m_hMutex;
 m_hMutex = ::CreateMutex(NULL, TRUE, _T("YourApplication"));
 ASSERT(m_hMutex);
 if (GetLastError() == ERROR_ALREADY_EXISTS)
 {
  m_hMutex = NULL;
  return TRUE;//執行個體已經運行
 }
 return FALSE;//執行個體未運行
}
7.怎樣刪除檔案到資源回收筒中
 要刪除檔案到資源回收筒,很簡單。只要用SHFileOperation函數就行了,下面的代碼我將為你示範了這一個函數的用法。當然你可以直接拷貝到你的項目中。
//刪除檔案到資源回收筒中
//pszPath  : 待刪除的全路徑檔案名稱
//bDelete  : TRUE 刪除,不移到資源回收筒,FALSE:移到資源回收筒
一、 //返回    : TRUE 刪除成功     FALSE 刪除失敗
BOOL CDelFileToRecycleDlg::Recycle(LPCTSTR pszPath, BOOL bDelete/*=FALSE*/)
{
 SHFILEOPSTRUCT  shDelFile;
 memset(&shDelFile,0,sizeof(SHFILEOPSTRUCT));
 shDelFile.fFlags |= FOF_SILENT;      // don't report progress
 shDelFile.fFlags |= FOF_NOERRORUI;     // don't report errors
 shDelFile.fFlags |= FOF_NOCONFIRMATION;    // don't confirm delete
 // Copy pathname to double-NULL-terminated string.
 //
 TCHAR buf[_MAX_PATH + 1]; // allow one more character
 _tcscpy(buf, pszPath);   // copy caller's pathname
 buf[_tcslen(buf)+1]=0;   // need two NULLs at end

 // Set SHFILEOPSTRUCT params for delete operation
 shDelFile.wFunc = FO_DELETE;       // REQUIRED: delete operation
 shDelFile.pFrom = buf;         // REQUIRED: which file(s)
 shDelFile.pTo = NULL;          // MUST be NULL
 if (bDelete)
 {         // if delete requested..
  shDelFile.fFlags &= ~FOF_ALLOWUNDO;    // ..don't use Recycle Bin
 }
 else
 {           // otherwise..
  shDelFile.fFlags |= FOF_ALLOWUNDO;    // ..send to Recycle Bin
 }
     return SHFileOperation(&shDelFile);    // do it!
}

8.、記憶體流失檢查
    也許你已經知道,在C++和C語言中指標問題也就是記憶體申請與釋放是一個令人頭疼的事情,假如你申請了記憶體,但沒有釋放,並且你的程式需要長時間地運行,那麼,系統的資源將逐漸減少,當系統的資源全部被用完時,系統將會崩潰。所以在開發程式的過程中一定要保證資源的完全釋放。下面我們來介紹記憶體漏洞的檢查。
樣本如下:
// do your memory allocations and deallocations...
 CString s = "This is a frame variable";
#ifdef _DEBUG
 CMemoryState oldMemState, newMemState, diffMemState;
 oldMemState.Checkpoint();
#endif
 // the next object is a heap object
 CString* p = new CString( "Smith  Alan  581_0215" );
 delete p;
 p=NULL;
#ifdef _DEBUG
 newMemState.Checkpoint();
 BOOL b=diffMemState.Difference(oldMemState, newMemState);
 if (b)
 {
  AfxMessageBox( "Memory leaked!/n" );
 }
#endif
    根據實驗,由於我們無法釋放掉象int CString char 申請的變數。只能釋放指標型的變數。而檢測記憶體時,照樣會出現記憶體流失現象。所以,這種記憶體檢測方式局限性還是很大。因為我們無法釋放非指標型變數。

 

擷取浮點型的值ceil(a)>=afloor(a)<=a 
9、將string類型轉化為浮點型
double wcstod( const wchar_t *nptr, wchar_t **endptr );
將string類型轉化為浮點型
long strtol( const char *nptr, char **endptr, int base );
轉化char類型為long,返回nptr的資料若越界則取邊界值:如int則為:[-2147483648,2147483647] 10、擷取浮點型的整數值ceil(a)>=afloor(a)<=a

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.