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. asp.net mvc 路由检测工具

    初学mvc,路由搞不清楚,可以通过一款插件 查看匹配的路由. 工具名<RouteDebugger> 可以在nuget中查询RouteDebugger后,安装.或者在控制台进行安装: pm& ...

  2. UnicodeEncodeError: 'latin-1' codec can't encode characters in position 41-50: ordinal not in range(256)

    在处理标题或网址为中文的文件或网页的时候,报UnicodeEncodeError: 'latin-1' codec can't encode characters in position 41-50: ...

  3. re:从零开始的数位dp

    起源:唔,,前几天打cf,edu50那场被C题虐了,决定学学数位dp.(此文持续更新至9.19) ps:我也什么都不会遇到一些胡话大家不要喷我啊... 数位dp问题:就是求在区间l到r上满足规定条件的 ...

  4. SVN版本管理系统的使用(CentOS+Subversion+Apache+Jsvnadmin+TortoiseSVN)

    1.服务器: 192.168.4.221root 用 户操作安装 装 apache# yum install httpd httpd-devel# service httpd start# chkco ...

  5. react material-ui 添加jss插件

    jss.config.js import { create } from "jss"; import preset from "jss-preset-default&qu ...

  6. package.json文件配置信息

    1.概述 每个项目的根目录下面,一般都有一个package.json文件,定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称.版本.许可证等元数据).npm install命令根据这个配置文 ...

  7. laravel5.4+vue+element简单搭建(gulp+laravel Elixir)(转)

    如今laravel来到5.4版本,更方便引入vue了,具体步骤如下: 下图为我动到的文件 1.下载laravel5.4 2.命令行(laravel5.4目录下):composer install 3. ...

  8. mac 远程连接 云服务器

    之前mac 命令行连接云端服务器,一直失败,今天问题突然间解决了,如果遇到类似的问题,按照方法解决不了,可以在下面留言,共同探讨. 首先,在云端先判断一下云端服务器是否安装了    ssh服务器:op ...

  9. [redis] redis cli的学习记录

    文档: https://redis.io/topics/rediscli help命令: The command can be used in two forms: . help @<categ ...

  10. 【Python基础】zip函数的使用

    zip函数的使用 描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同, ...