Strings in Windows programming (2)

Source: Internet
Author: User
Tags sprintf

(1) Windows write log system

1 voidWritedebugeventlog (tchar*pszmessage, WORD wtype)2 {3     //#ifdef _DEBUG4 5HANDLE Heventsource =NULL;6     Consttchar* lpszstrings[2] ={null, null};7 8Heventsource = Registereventsourcew (NULL, L"Devicemonitorservice");9     if(Heventsource)Ten     { Onelpszstrings[0] = _t ("Devicemonitorservice"); Alpszstrings[1] =Pszmessage; -  -ReportEvent (Heventsource,//Event Log Handle theWtype,//Event Type -             0,//Event category -             0,//Event identifier -Null//No Security Identifier +             2,//Size of lpszstrings array -             0,//No binary Data +Lpszstrings,//Array of Strings ANull//No binary Data at             ); -  - Deregistereventsource (heventsource); -     } -     //#else -     //#endif//DEBUG in}

(2) writing a string to an array

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);

Note:

stringcchprintf function (Strsafe.h)

 

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

stringcchprintf is a replacement for the following functions:

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

sprintf (<stdio.h>)

(3) TCHAR and String conversions (Std::string is a char-type character Set)

1 stringTchar2string (tchar*STR) {2     intIlen = WideCharToMultiByte (CP_ACP,0, STR,-1Null0, NULL, NULL);3     Char* Chrtn =New Char[ilen*sizeof(Char)];4WideCharToMultiByte (CP_ACP,0, STR,-1, CHRTN, Ilen, NULL, and NULL);5STD::stringstr (CHRTN);6     returnstr;7}

Note:

String

Header: <string>--c++ Standard library header file

Namespace:std

String

char type will basic_string The specialization of the template class is described as string . ">a type that describes A specialization of the template class  basic_string  with elements of Ty pe  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 string converted to Unicode string

1LPWSTR pwszout =NULL;2 if(Value! =NULL)3 {4     //    //Double NULL Termination5     intNoutputstrlen = MultiByteToWideChar (CP_ACP,0, Value,-1Null0);//Gets the length of the char string, including the length of the empty string6Pwszout =NewTchar[noutputstrlen];7 8memset (Pwszout,0, Noutputstrlen);9MultiByteToWideChar (CP_ACP,0, Value,-1, Pwszout, Noutputstrlen);Ten}

(4) Various data structures

typedef _null_terminated_ CONST WCHAR *lpcwstr, *pcwstr;

typedef LPCWSTR PCTSTR, LPCTSTR; --winnt.h

(5) Various types of string functions

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.

WarningDo not use. Consider using StringCchCatinstead.       See Security considerations.       Lstrcat (cmd, _t ("***.exe")); lstrcpy (ID, buff); (6) Other functions 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-up, win NT

Strings in Windows programming (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.