天天看點

WindowsAPI每日一練(2) 使用應用程式句柄  LoadString問題: 聲明:參數:傳回值:說明系統要求

WindowsAPI每日一練系列 :        https://www.cnblogs.com/LexMoon/category/1246238.html               
  WindowsAPI每日一練(1) 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。

請看下面的例子:

1 #include<windows.h>
 2 
 3 int WINAPI wWinMain(
 4   HINSTANCE hInstance,      // handle to current instance
 5   HINSTANCE hPrevInstance,  // handle to previous instance
 6   LPSTR lpCmdLine,          // command line
 7   int nCmdShow              // show state
 8 )
 9 { 
10      UNREFERENCED_PARAMETER(hInstance);
11      UNREFERENCED_PARAMETER(hPrevInstance); 
12      UNREFERENCED_PARAMETER(lpCmdLine);
13      UNREFERENCED_PARAMETER(nCmdShow);    
14 
15      //使用應用程式句柄
16 
17      const int MAXSIZE_APPBUF = 256;
18      TCHAR wAppTitle[MAXSIZE_APPBUF];
19      LoadString(hInstance,IDS_APP_TITLE,wAppTitle,MAXSIZE_APPBUF);
20 
21 
22 
23     //獲得桌面句柄
24     HWND hWnd=GetDesktopWindow();
25 
26     //顯示第一行資訊
27 //    ::MessageBox(hWnd,"Hello Window","Hello Window",MB_YESNO);
28     MessageBox(hWnd,"Hello Windows",wAppTitle,MB_YESNO);
29     return 0;
30 }      

這個例子是在前面的基礎上修改的,主要添加了使用應用程式執行個體句柄。在第19行裡定義了一個儲存應用程式标題的緩沖區,然後在第20行裡調用函數LoadString從應用程式的資源裡加載字元串,它的第一個參數就使用到hInstance句柄。是以應用程式句柄是表示程式在資源上唯一的辨別符。

LoadString問題:

 作用:從 資源 裡加載字元串資源到

CString

對象裡。

聲明:

  函數LoadString聲明如下:

1 WINUSERAPI int WINAPI LoadStringA(
2 
3 __in_opt HINSTANCE hInstance,
4 
5 __in UINT uID,
6 
7 __out_ecount(cchBufferMax) LPSTR lpBuffer,
8 
9 __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

裡面的字元串讀出來到

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);

繼續閱讀