Windows 編程中的字串(2)

來源:互聯網
上載者:User

標籤:

(1)windows寫日誌系統

 1 void writeDebugEventLog(TCHAR* pszMessage, WORD wType) 2 { 3     //#ifdef _DEBUG 4  5     HANDLE hEventSource = NULL; 6     const TCHAR* lpszStrings[2] = { NULL, NULL }; 7  8     hEventSource = RegisterEventSourceW(NULL, L"DeviceMonitorService"); 9     if (hEventSource)10     {11         lpszStrings[0] = _T("DeviceMonitorService");12         lpszStrings[1] = pszMessage;13 14         ReportEvent(hEventSource,  // Event log handle15             wType,                 // Event type16             0,                     // Event category17             0,                     // Event identifier18             NULL,                  // No security identifier19             2,                     // Size of lpszStrings array20             0,                     // No binary data21             lpszStrings,           // Array of strings22             NULL                   // No binary data23             );24 25         DeregisterEventSource(hEventSource);26     }27     //#else28     //#endif // DEBUG29 }

(2)將字串寫入數組

1 TCHAR szMessage[260];2 ZeroMemory(szMessage, ARRAYSIZE(szMessage));3 StringCchPrintf(szMessage, ARRAYSIZE(szMessage),4                 _T("[Win32Project1  ]monitorId  %s  ,   attached DisplayDevice.DeviceID : %s   IsLocalMonitor %d"), monitorId, aDisplayDevice.DeviceID, IsLocalMonitor);5 writeDebugEventLog(szMessage, EVENTLOG_ERROR_TYPE);

  註:

StringCchPrintf function (Strsafe.h)

 

Writes formatted data to the specified string. The size of the destination buffer is provided to the function to ensure that it does not write past the end of this buffer.

 

StringCchPrintf is a replacement for the following functions:

  • sprintf, swprintf, _stprintf
  • wsprintf
  • wnsprintf
  • _snprintf, _snwprintf, _sntprintf

sprintf (<stdio.h>)

 

 

(3)TCHAR與string轉換(std::string為char類型的字元集合)

1 string TCHAR2STRING(TCHAR* STR){2     int iLen = WideCharToMultiByte(CP_ACP, 0, STR, -1, NULL, 0, NULL, NULL);3     char* chRtn = new char[iLen*sizeof(char)];4     WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);5     std::string str(chRtn);6     return str;7 }

註:

   string 

   Header: <string> ——c++ 標準庫標頭檔

   Namespace: std

   

string

A type that describes a specialization of the template class basic_string with elements of type char as a string.

wstring

A type that describes a specialization of the template class basic_string with elements of type wchar_t as a wstring.

 

 

 

 

 

 

char字串轉換成Unicode 字串

 1 LPWSTR pwszOut = NULL; 2 if (value != NULL) 3 { 4     //    // Double NULL Termination 5     int nOutputStrLen = MultiByteToWideChar(CP_ACP, 0, value, -1, NULL, 0);//擷取char字串的長度,包括空串的長度 6     pwszOut = new TCHAR[nOutputStrLen]; 7  8     memset(pwszOut, 0, nOutputStrLen); 9     MultiByteToWideChar(CP_ACP, 0, value, -1, pwszOut, nOutputStrLen);10 }

 

(4)各類資料結構 

 

     typedef _Null_terminated_ CONST WCHAR *LPCWSTR, *PCWSTR;

 

      typedef LPCWSTR PCTSTR, LPCTSTR;  ——winnt.h

 

 

 

(5)各類字串函數

      

 

  strrchr, wcsrchr,_tcsrchr——

Header: stdio.h, string.h.

     Scan a string for the last occurrence of a character.

 

     example:  (_tcsrchr(cmd, _T(‘\\‘)))[1] = 0;

 

 

     lstrcat—— include Windows.h

  Appends one string to another.

  Warning  Do not use. Consider using  StringCchCat instead. See Security Considerations.       lstrcat(cmd, _T("***.exe"));       lstrcpy(id, buff); (6)其它函數    ZeroMemory macro—— (include Windows.h)memset  ——  #include <memory.h>  #include <stdio.h>

 

Visual Studio 6.0Sets buffers to a specified character.

void *memset( void *dest, int c, size_t count );

Routine Required Header Compatibility
memset <memory.h> or <string.h> ANSI, Win 95, Win NT

 

Windows 編程中的字串(2)

聯繫我們

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