`
阿尔萨斯
  • 浏览: 4183900 次
社区版块
存档分类
最新评论

Windows API一日一练(44)wsprintf函数

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
接着上面,再继续实现更加强大的线程类。从上面的C++类里可以看到,要在静态函数里使用类的成员就需要获取this指针,也就是通过CreateThread函数里把类的this指针传送进来的,这样在函数ThreadProc里的参数lpParameter就是this指针了。因此把参数lpParameter转换为CThread类指针,这样就可以使用类的成员。在这个例子里使用wsprintf函数来格式化线程ID输出来,下面就来详细地分析例子吧。
函数wsprintf声明如下:
WINUSERAPI
int
WINAPIV
wsprintfA(
__out LPSTR,
__in __format_string LPCSTR,
...);
WINUSERAPI
int
WINAPIV
wsprintfW(
__out LPWSTR,
__in __format_string LPCWSTR,
...);
#ifdef UNICODE
#define wsprintfwsprintfW
#else
#define wsprintfwsprintfA
#endif // !UNICODE
LPWSTR是格化后输出的缓冲区。
LPCWSTR是输入格式化字符串。
...是可变参数。
调用这个函数的例子如下:
#001#pragma once
#002
#003//线程类。
#004//蔡军生 2007/09/23
#005class CThread
#006{
#007public:
#008
#009CThread(void)
#010{
#011 m_hThread = NULL;
#012}
#013
#014virtual ~CThread(void)
#015{
#016 if (m_hThread)
#017 {
#018 //删除的线程资源。
#019 ::CloseHandle(m_hThread);
#020 }
#021
#022}
#023
#024//创建线程
#025HANDLE CreateThread(void)
#026{
#027 //创建线程。
#028 m_hThread = ::CreateThread(
#029 NULL, //安全属性使用缺省。
#030 0, //线程的堆栈大小。
#031 ThreadProc, //线程运行函数地址。
#032 this, //传给线程函数的参数。
#033 0, //创建标志。
#034 &m_dwThreadID); //成功创建后的线程标识码。
#035
#036 return m_hThread;
#037}
#038
#039//等待线程结束。
#040void WaitFor(DWORD dwMilliseconds = INFINITE)
#041{
#042 //等待线程结束。
#043 ::WaitForSingleObject(m_hThread,dwMilliseconds);
#044}
#045
#046protected:
#047//
#048//线程运行函数。
#049//蔡军生 2007/09/21
#050//
#051static DWORD WINAPI ThreadProc(LPVOID lpParameter)
#052{
#053 //转换传送入来的参数。
#054 CThread* pThread = reinterpret_cast<cthread>(lpParameter);</cthread>
#055 if (pThread)
#056 {
#057 //线程返回码。
#058 //调用类的线程处理函数。
#059 return pThread->Run();
#060 }
#061
#062 //
#063 return -1;
#064}
#065
#066//线程运行函数。
#067//在这里可以使用类里的成员,也可以让派生类实现更强大的功能。
#068//蔡军生 2007/09/24
#069virtual DWORD Run(void)
#070{
#071 //输出到调试窗口。
#072 ::OutputDebugString(_T("Run()线程函数运行/r/n"));
#073
#074 TCHAR chTemp[128];
#075 wsprintf(chTemp,_T("ThreadID=%d/r/n"),m_dwThreadID);
#076 ::OutputDebugString(chTemp);
#077
#078 return 0;
#079}
#080
#081protected:
#082HANDLE m_hThread; //线程句柄。
#083DWORD m_dwThreadID; //线程ID
#084
#085};
#086
在这个例子里主要在第54行获取类的指针,然后在59行调用运行类的成员函数Run()。通过这样的调用后,所有线程的代码都可以写在类CThread里面了,这样就达到编写更简单的代码和复用的目的,因此Run函数设计为虚函数,让派生类可以改写这个类的功能。



分享到:
评论

相关推荐

    windows控制台wsprintf打印utf8字符串例子

    windows控制台 wsprintf 打印 utf8 字符串例子,用VS2015编译。 通过一个 hpp 头文件定义了utf8转utf16的功能函数,将 utf8 字符串转为utf16后传递给 wsprintf 打印 出来。

    VB API实现简单的程序窗口.rar

    VB 基于API技术实现简单的程序窗口,源码文件说明:用VB的模块模仿写的几个小例子(不用窗体)[源码] ...主要是用到了wsprintf函数,对应于Win32API函数wprintf(Win32API函数wprintf在VB中不能去调用它,调用会出错。)

    windows用户称拦截api

    Windows用户层下拦截api的原理与实现(附源码) (2008-03-29 16:15:07)转载▼ 标签: computer 杂谈 声明:本页所发布的技术文章及其附件,供自由技术传播,拒绝商业使用。本页文章及其附件的所有权归属本文作者...

    VB.win32函数教程

    VB中win32函数,非常不错,顶

    MFC类型转换与回调函数

    格式化字符串与回调函数  与其用 sprintf() 函数或 wsprintf() 函数来格式化一个字符串,还不如用 CString 对象的Format()方法:

    在win32窗口下输出杨辉三角

    静态窗口输出通过建立Win32项目实现,在预编译好的窗口文件里,只需要添加绘制的信息即可。...利用二维数组和for循环排列出杨辉三角,再利用wsprintf函数把整形转化为字符型,再利用TextOut函数打印出杨辉三角。

    获得USB的信息vc++

    一、枚举USB设备  通过枚举USB控制器-&gt;枚举此控制器上的USB HUB-&gt;枚举HUB的各个端口-&gt;获得设备信息。 枚举控制器: wsprintf(HCName, "\\\\.\\HCD%d", HCNum); hHCDev = CreateFile(HCName, GENERIC_WRITE, ...

    定时关机程序

    #include &lt;windows.h&gt; #include &lt;windowsx.h&gt; #include "resource.h" #include "MainDlg.h" BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { HANDLE_MSG(hWnd, WM...

    键盘消息源代码

    #include &lt;windows.h&gt; #include //全局变量 RECT rc; //记录滚屏的矩形区域 int xChar, yChar; //文本输入点坐标 WNDCLASSEX wnd; //窗口类结构变量 char szAppName[] = "键盘...

    常用功能汇编语言代码汇总,例如:ASCII转换,浮点操作,网络,字符串,排序等

    MP3 信息 [比特率/时间/...],OneZeroMany flags 案例,WinIO 原型,一年中的某一天,GetLastError Handling,64 bit to dec ASCIIZ - 2 with MMX,将 64 位无符号整数转换为十进制 ASCIIZ 字符串,无 MMX 指令集的...

    LPK专杀C语言源码

    该函数可以分析出一个文件的路径。 PathAppend(szTmpRARPath, L"rar.exe"); GetFileAttributes(szTmpRARPath);// 获取到压缩包rar.exe的路径 TCHAR seps[] = L"\""; TCHAR *token = NULL; TCHAR *next_token =...

    简易播放器

    #include "stdafx.h" #include &lt;windows.h&gt; #include &lt;windowsx.h&gt; #include "resource.h" #include "MainDlg.h" #include "mmsystem.h" #include "commdlg.h" ... wsprintf(strTime,"%2d:%2d:%2d",mscH,mscM,mscS);

    内存监测VC源代码

    王艳萍的内存监测,代码蛮好的,代码摘录如下:   char szBuff[128]; MEMORYSTATUS ms; ... wsprintf(szBuff, "\n 物理内存总量: %-5d MB", ms.dwTotalPhys/(1024*1024));... wsprintf(szBuff, "\n

    PostSend类

    wsprintf(pName,L"%s?filename=%s",m_strFtpSite,strDestName); AfxOutInfoToFile("===上传文件名:",strSourceName); AfxOutInfoToFile("===上传地址:",pName); ASSERT(m_strFtpSite != "" && strSourceName != ...

    PETools源码

    wsprintf(cBuff,"lX",ImpDescriptor.OriginalFirstThunk); this-&gt;m_ListCtrl1.SetItemText(n,1,cBuff); wsprintf(cBuff,"lX",ImpDescriptor.TimeDateStamp); this-&gt;m_ListCtrl1.SetItemText(n,2,cBuff); ...

    易语言日志打印

    易语言日志打印源码,日志打印,创建日志框,写日志,CreateFontA,GetDC,GetClientRect,SelectObject,SetTextColor,DrawText,DeleteObject,ReleaseDC,DefWindowProc,RedrawWindow,Data_wsprintf1,SetWindowLongA

    简单债务管理(32位汇编)

    对日常债务进行数据库操作 .586 .model flat,stdcall option casemap:none include pay.inc include p_Func.asm include p_const.asm include p_struct.asm include p_data.asm include macro.mac ...

Global site tag (gtag.js) - Google Analytics