WindowsAPI每日一练系列 :https://www.cnblogs.com/LexMoon/category/1246238.html
  WindowsAPI每日一练() WinMain
  WindowsAPI每日一练(2) 使用应用程序句柄
从上面这段程序就可以看到,_tWinMain是应用程序的入口函数,这里是使用它的宏,定义在tchar.h头文件里,为什么要这样作宏定义的呢?由于Windows的应用程序要适应UNICODE和以前单字符的应用程序,由于Windows这两个API的定义是不一样的,如下:
UNICODE的定义:
 #define _tWinMain   wWinMain
单字符的定义:
 #define _tWinMain   WinMain
只要经过这样的宏定义后,就可以适应不同字符宽度的函数接口了。由于我是采用UNICODE编译的,所以这里使用wWinMain函数定义,下面再把它的定义找出来,如下:
int
WINAPI
wWinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine,
    int nShowCmd
);
这里要详细地解释一下函数wWinMain的参数,它有四个参数。
hInstance是当前应用程序的实例句柄,一般用来区分不同的资源使用。
hPrevInstance是以前Win98使用的句柄,在Win2000以后的操作系统里都是空值NULL。
lpCmdLine是命令行参数,比如你在Windows开始菜单里运行一个程序,并添加参数在后面,就会传递给应用程序,后面再详细讨论。
nShowCmd是窗口的显示方式,比如最大化显示,最小化显示,还是正常显示。
 
Windows运行程序时,是通过运行库里的启动代码来调用wWinMain函数,它是在启动文件里如下调用:
#ifdef WPRFLAG
            mainret = wWinMain(
#else /* WPRFLAG */
            mainret = WinMain(
#endif /* WPRFLAG */
                       (HINSTANCE)&__ImageBase,
                       NULL,
                       lpszCommandLine,
                       StartupInfo.dwFlags & STARTF_USESHOWWINDOW
                        ? StartupInfo.wShowWindow
                        : SW_SHOWDEFAULT
                      );
这就是操作系统传递给应用程序的值,现在就来演示使用第一个参数hInstance。
请看下面的例子:
 #include<windows.h>

 int WINAPI wWinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow); //使用应用程序句柄 const int MAXSIZE_APPBUF = ;
TCHAR wAppTitle[MAXSIZE_APPBUF];
LoadString(hInstance,IDS_APP_TITLE,wAppTitle,MAXSIZE_APPBUF); //获得桌面句柄
HWND hWnd=GetDesktopWindow(); //显示第一行信息
// ::MessageBox(hWnd,"Hello Window","Hello Window",MB_YESNO);
MessageBox(hWnd,"Hello Windows",wAppTitle,MB_YESNO);
return ;
}

这个例子是在前面的基础上修改的,主要添加了使用应用程序实例句柄。在第19行里定义了一个保存应用程序标题的缓冲区,然后在第20行里调用函数LoadString从应用程序的资源里加载字符串,它的第一个参数就使用到hInstance句柄。因此应用程序句柄是表示程序在资源上唯一的标识符。


LoadString问题:

 作用:从 资源 里加载字符串资源到CString对象里。

声明:

  函数LoadString声明如下:
 WINUSERAPI int WINAPI LoadStringA(

 __in_opt HINSTANCE hInstance,

 __in UINT uID,

 __out_ecount(cchBufferMax) LPSTR lpBuffer,

 __in int nBufferMax);
参数1: hInstance是应用程序实例句柄。
参数2: uID是资源中的字符串编号。
参数3: lpBuffer是接收从资源里拷贝字符串出来的缓冲区。
参数4: nBufferMax是指明缓冲的大小。

参数:

hInstance [in]:
Handle to an instance of the module whose executable file contains the string resource. To get the handle to the application itself, use GetModuleHandle(NULL).
uID [in] :
Specifies the integer identifier of the string to be loaded.
lpBuffer [out]:
Pointer to the buffer to receive the string.
nBufferMax [in]:
Specifies the size of the buffer, in TCHARs. This refers to bytes for ANSI versions of the function or WCHARs for Unicode versions. The string is truncated and NULL terminated if it is longer than the number of characters specified.

返回值:

If the function succeeds, the return value is the number of TCHARs copied into the buffer, not including the terminating NULL character, or zero if the string resource does not exist. To get extended error information, call GetLastError.

说明

Security Alert
Using this function incorrectly can compromise the security of your application. Incorrect use includes specifying the wrong size in the nBufferMax parameter. For example, if lpBuffer points to a buffer szBuffer which is declared as TCHAR szBuffer[100], then sizeof(szBuffer) gives the size of the buffer in bytes, which could lead to a buffer overflow for the Unicode version of the function. Buffer overflow situations are the cause of many security problems in applications. In this case, using sizeof(szBuffer)/sizeof(TCHAR) or sizeof(szBuffer)/sizeof(szBuffer[0]) would give the proper size of the buffer.
Windows 95/98/Me: LoadStringW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

系统要求

Minimum DLL Version: user32.dll
Header: Declared in Winuser.h, include Windows.h
Import library: User32.lib
Minimum operating systems: Windows 95, Windows NT 3.1
Unicode: Implemented as ANSI and Unicode versions.
看完上面的资料可能大家都蒙了,不知道具体是在做什么,
其实这个成员函数是在调用String Table里面定义的信息,
也就是说,把String Table中Caption里面的字符串读出来到CString对象里。
String Table中的ID号及其对应的字符串都是可以自己定义的。
String Table在ResourceView窗口中,也就是ClassView右边那个窗口。
双击之后就能看到,在最下面的选项中追加新的ID信息。
如果还是理解不清楚的话,请自己尝试多读msdn里的英文及例子就明白了。

int LoadString(HINSTANCE hInstance,//应用程序实例句柄
UINT uID,//资源ID
LPTSTR lpBuffer,//存放字符串的缓冲区
int nBufferMax//缓冲区大小

作用:
先在资源中加入字符串资源(不管是字符串还是窗口还是按钮),都有一个名字,比如IDC_BUTTON1
然后用这个函数把这个名字作为参数,就可以取出资源中的字符串了

如:一、在resource.h中,#define FIREWALL_ALREADY_START           61452
二、在*.rc文件中:
STRINGTABLE DISCARDABLE
BEGIN
        FIREWALL_ALREADY_START "防火墙已经启动"
END

三、
CString str;
    str.LoadString(FIREWALL_ALREADY_START);

WindowsAPI每日一练(2) 使用应用程序句柄的更多相关文章

  1. WindowsAPI每日一练(1) MessageBoxA

    WindowsAPI每日一练系列 :https://www.cnblogs.com/LexMoon/category/1246238.html WindowsAPI每日一练(1) WinMain 要跟 ...

  2. 每日一小练——Armstrong数

    上得厅堂.下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:Armstrong数 内容: 在三位的正整数中,比如abc.有一些能够满足a^3+b^3+c^3=abc的条件,也就是说各 ...

  3. python每日一练:0007题

    第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. # -*- coding:utf-8 -*- import os def count ...

  4. [python每日一练]--0012:敏感词过滤 type2

    题目链接:https://github.com/Show-Me-the-Code/show-me-the-code代码github链接:https://github.com/wjsaya/python ...

  5. Python 每日一练 | Flask 实现半成品留言板

    留言板Flask实现 引言 看了几天网上的代码,终于写出来一个半成品的Flask的留言板项目,为什么说是半成品呢?因为没能实现留言板那种及时评论刷新的效果,可能还是在重定向上有问题 或者渲染写的存在问 ...

  6. Python 每日一练(4)

    引言 今天继续是python每日一练的几个专题,主要涵盖简单的敏感词识别以及图片爬虫 敏感词识别 这个敏感词的识别写的感觉比较简单,总的概括之后感觉功能可以简略成if filter_words in ...

  7. Python 每日一练(2)

    引言 我又双叒叕的来啦,新博客的第二篇文章,这次是继之前公众号上每日一练的第二个,这次是专题实对于文件的一些处理的练习 主要有以下几类: 1.实现英文文章字频统计 2.统一剪裁某一指定目录下的所有图片 ...

  8. CSS3每日一练之内容处理-嵌套编号

    出处:http://www.w3cfuns.com/thread-5592229-1-17.html 1.大标题一   1.子标题   2.子标题   3.子标题2.大标题二   1.子标题   2. ...

  9. CSS3每日一练之选择器-结构性伪类选择器

    <!DOCTYPE HTML> <html> <head> <meta charset="gb2312"> <title> ...

随机推荐

  1. git使用——推送本地文件到远程仓库

    捣鼓了一下午之后总结如下:   1.首先可以照着这个链接里面博主给出的详细方法进行操作和配置: http://www.open-open.com/lib/view/open1454507333214. ...

  2. 案例源码解读及思路:RabbitMQ在springboot中的配置

    程序员的高级之处不是什么都会,而是对自己不会的进行抽象,然后完成自己的工作.比如对于RabbitMQ,按照字面理解,就将其看成Message Queue,也就是用来容纳对象的集合.很多功能都拆分给一个 ...

  3. O - Muddy Fields

    来源poj2226 Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R & ...

  4. css 获取从第n个开始,之后的所有元素

    <div id="box"> <div></div> <div>等待获取</div> <div>等待获取&l ...

  5. Vue.js——60分钟快速入门(转载)

    Vue.js介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的.相比于Angular.js,Vue.js提供了更加简洁.更易于理解的API,使得我们 ...

  6. 浅析 Hinton 最近提出的 Capsule 计划

    [原文]    浅析 Hinton 最近提出的 Capsule 计划     关于最新的 Hinton 的论文 Dynamic Routing Between Capsules,参见 https:// ...

  7. C++/C面试题(2)

    (1)单向链表操作  1)在链表尾部插入一个节点 void addNewNodeTail(ListNode **HeadNode, int value)//在链表尾部插入一个节点{ ListNode* ...

  8. BeeHive小思考

    事件分发和事件处理 将所有行为注册为不同的时间类型,配置Module,让他们在事件发生时,响应这些事件(除了系统的事件,还可以注册自定义事件,触发自定义事件) Module注册之后应当会生成单例对象, ...

  9. InnoDB中锁的查看

    Ⅰ. show engine innodb status\G 1.1 实力分析一波 锁介绍的那篇中已经提到了这个命令,现在我们开一个参数,更细致的分析一下这个命令 (root@localhost) [ ...

  10. 关于mysql中like查询是否通过索引的测试

    测试mysql的like语句是否通过索引时得到结果如下: 图片1: 图片2: 图片3: 通过上述3组图片我想大家很容易愤青我使用的'%8888888%','%8888888'和'8888888%'3中 ...