zoukankan      html  css  js  c++  java
  • Windows 编程中的字符串(2)

    (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 handle
    15             wType,                 // Event type
    16             0,                     // Event category
    17             0,                     // Event identifier
    18             NULL,                  // No security identifier
    19             2,                     // Size of lpszStrings array
    20             0,                     // No binary data
    21             lpszStrings,           // Array of strings
    22             NULL                   // No binary data
    23             );
    24 
    25         DeregisterEventSource(hEventSource);
    26     }
    27     //#else
    28     //#endif // DEBUG
    29 }

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

     

  • 相关阅读:
    新一代MQ apache pulsar的架构与核心概念
    Flutter使用fluwx实现微信分享
    BZOJ3622 已经没有什么好害怕的了 动态规划 容斥原理 组合数学
    NOIP2016提高组Day1T2 天天爱跑步 树链剖分 LCA 倍增 差分
    Codeforces 555C Case of Chocolate 其他
    NOIP2017提高组Day2T3 列队 洛谷P3960 线段树
    NOIP2017提高组Day2T2 宝藏 洛谷P3959 状压dp
    NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
    Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)
    Codeforces 873E Awards For Contestants ST表
  • 原文地址:https://www.cnblogs.com/meizixiong/p/4476761.html
Copyright © 2011-2022 走看看