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. 给新手学习Java的建议

    有很多的朋友都在问我一个同样的问题:新手应该如何能学好Java.我做了一个简单的总结,分享给大家: 1-信念:无论你是选择JAVA,C,C#,C++....还是其他的语言编程,信念是第一位,只有相信自 ...

  2. 在php cli下可以使用 STDIN 来实现标准输入

    简单的例子:  echo "请输入一个数字:";  $num = trim(fgets(STDIN)); echo "请再输入一个数字:";  $num1 =  ...

  3. Centos 为Nginx 搭建https

    Youtube Nginx 扩展模块 certbot文档 yum -y install epel-release yum -y install certbot certbot certonly --s ...

  4. 清理孤儿文件 clearing up outdated orphans

    pacman -Rns $(pacman -Qtdq) It lists all packages installed as dependencies but no longer required b ...

  5. js字符串转数字(小数),数字转字符串

    将字符串转化为小数并加法计算,然后保留两位小数 (parseFloat(that.data.pay_price) + parseFloat(that.data.qiandao)).toFixed(2) ...

  6. 嵌入式常用技术概览之IIC(I2C)

    一.先决知识             (1)模电基础知识(用以理解IIC如何通信) 二.IIC概览               I2C是80年代飞利浦(Philips->NXP->高通)研 ...

  7. vim快捷键与vi

    vim与程序员 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体 ...

  8. 删除64位ODBC数据源DNS

    1.按照打开管理工具-打开数据源(ODBC),进入如下界面,选择用户DSN删除,发现报错一直删除不了. 2.成功删除:进入如下图路径,打开ODBC数据源管理工具,选择要删除的DSN就可以成功删除啦.

  9. 记录常用的adb命令

    1.启动adb服务 adb start-server 2.关闭服务 adb kill-server 3.进入shell环境 adb shell 4.安装应用 adb install -r xxx.ap ...

  10. HTTP Get Post究竟有哪些区别

    get在浏览器回退时是无害的,而post会再次提交请求. get产生的url地址可以被bookmark,而post不可以. get请求会被浏览器主动cache,而post不会,除非手动设置. get请 ...