One-day Windows API training (44) wsprintf Function

Source: Internet
Author: User
One-day Windows API training (44) wsprintf Function

Next, we will continue to implement more powerful thread classes. From the above C ++ class, we can see that to use class members in static functions, we need to obtain the this pointer, that is, the this pointer of the class is transmitted through the createthread function, in this way, the parameter lpparameter in the threadproc function is the this pointer. Therefore, you can convert the lpparameter parameter to a cthread class pointer so that you can use the class members. In this example, the wsprintf function is used to format the thread ID output. The following is a detailed analysis example.

The wsprintf function is declared as follows:

Winuserapi
Int
Winapiv
Wsprintfa (
_ Out lpstr,
_ In _ format_string lpcstr,
...);
Winuserapi
Int
Winapiv
WsprintfW (
_ Out lpwstr,
_ In _ format_string lpcwstr,
...);
# Ifdef Unicode
# Define wsprintf wsprintfW
# Else
# Define wsprintf wsprintfa
# Endif //! Unicode

Lpwstr is the output buffer after the lattice.
Lpcwstr is a formatted string.
... Is a variable parameter.

An example of calling this function is as follows:
#001 # pragma once
#002
#003 // Thread class.
#004 // Cai junsheng 2007/09/23
#005 class cthread
#006 {
#007 public:
#008
#009 cthread (void)
#010 {
#011 m_hthread = NULL;
#012}
#013
#014 virtual ~ Cthread (void)
#015 {
#016 if (m_hthread)
#017 {
#018 // Delete the thread resource.
#019: closehandle (m_hthread );
#020}
#021
#022}
#023
#024 // create a thread
#025 handle createthread (void)
#026 {
#027 // create a thread.
#028 m_hthread =: createthread (
#029 null, // use the default security attribute.
#030 0, // The stack size of the thread.
#031 threadproc, // address of the thread running function.
#032 this, // The parameter passed to the thread function.
#033 0, // create a flag.
#034 & m_dwthreadid); // ID of the created thread.
#035
#036 return m_hthread;
#037}
#038
#039 // wait for the thread to end.
#040 void waitfor (DWORD dwmilliseconds = infinite)
#041 {
#042 // wait for the thread to end.
#043: waitforsingleobject (m_hthread, dwmilliseconds );
#044}
#045
#046 protected:
#047 //
#048 // run the function in a thread.
#049 // Cai junsheng 2007/09/21
#050 //
#051 static DWORD winapi threadproc (lpvoid lpparameter)
#052 {
#053 // convert the input parameters.
#054 cthread * pthread = reinterpret_cast <cthread *> (lpparameter );
#055 if (pthread)
#056 {
#057 // The thread return code.
#058 // call the thread processing function of the class.
#059 return pthread-> Run ();
#060}
#061
#062 //
#063 return-1;
#064}
#065
#066 // run the function in a thread.
#067 // here, you can use the members in the class or make the derived class more powerful.
#068 // Cai junsheng 2007/09/24
#069 virtual DWORD run (void)
#070 {
#071 // output to the debugging window.
#072: outputdebugstring (_ T ("Run () thread function run/R/N "));
#073
#074 tchar chtemp [1, 128];
#075 wsprintf (chtemp, _ T ("threadid = % d/R/N"), m_dwthreadid );
#076: outputdebugstring (chtemp );
#077
#078 return 0;
#079}
#080
#081 protected:
#082 handle m_hthread; // thread handle.
#083 DWORD m_dwthreadid; // thread ID.
#084
#085 };
#086

In this example, the pointer of the class is obtained in Row 3, and then the member function run () of the running class is called in row 59 (). After such a call, the code of all threads can be written in the class cthread, so as to write simpler code and reuse it. Therefore, the run function is designed as a virtual function, let the derived class rewrite the function of this class.

Related Article

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.