Windows 编程中的字符串(2)

(1)windows写日志系统

 void writeDebugEventLog(TCHAR* pszMessage, WORD wType)
{
//#ifdef _DEBUG HANDLE hEventSource = NULL;
const TCHAR* lpszStrings[] = { NULL, NULL }; hEventSource = RegisterEventSourceW(NULL, L"DeviceMonitorService");
if (hEventSource)
{
lpszStrings[] = _T("DeviceMonitorService");
lpszStrings[] = pszMessage; ReportEvent(hEventSource, // Event log handle
wType, // Event type
, // Event category
, // Event identifier
NULL, // No security identifier
, // Size of lpszStrings array
, // No binary data
lpszStrings, // Array of strings
NULL // No binary data
); DeregisterEventSource(hEventSource);
}
//#else
//#endif // DEBUG
}

(2)将字符串写入数组

 TCHAR szMessage[];
ZeroMemory(szMessage, ARRAYSIZE(szMessage));
StringCchPrintf(szMessage, ARRAYSIZE(szMessage),
_T("[Win32Project1 ]monitorId %s , attached DisplayDevice.DeviceID : %s IsLocalMonitor %d"), monitorId, aDisplayDevice.DeviceID, IsLocalMonitor);
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 (<stdio.h>)

(3)TCHAR与string转换(std::string为char类型的字符集合)

 string TCHAR2STRING(TCHAR* STR){
int iLen = WideCharToMultiByte(CP_ACP, , STR, -, NULL, , NULL, NULL);
char* chRtn = new char[iLen*sizeof(char)];
WideCharToMultiByte(CP_ACP, , STR, -, chRtn, iLen, NULL, NULL);
std::string str(chRtn);
return str;
}

注:

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 字符串

 LPWSTR pwszOut = NULL;
if (value != NULL)
{
// // Double NULL Termination
int nOutputStrLen = MultiByteToWideChar(CP_ACP, , value, -, NULL, );//获取char字符串的长度,包括空串的长度
pwszOut = new TCHAR[nOutputStrLen]; memset(pwszOut, , nOutputStrLen);
MultiByteToWideChar(CP_ACP, , value, -, pwszOut, nOutputStrLen);
}

(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.0

Sets 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编程中的应用----菜单


下一篇:Java:类与继承