在win32下与HMODULE是相同的东西,在Win32下还存在主要是因为win16
程序使用HINSTANCE来区别task。
区别:
Handle 是代表系统的内核对象,如文件句柄,线程句柄,进程句柄。
HMODULE 是代表应用程序载入的模块,win32系统下通常是被载入模块的线性地址。
HINSTANCE 在win32下与HMODULE是相同的东西,在Win32下还存在主要是因为win16
程序使用HINSTANCE来区别task。
HWND 是窗口句柄
熟悉C编程的人都知道main函数带有2个参数:arc和argv,完整的main函数定义是:int main(int argc, char *argv[])。argc指示程序启动时命令行参数的个数,argv则包含具体的参数字符串。
如果有程序叫“hello.exe”,直接启动时,argc=1, argv[0]=hello.exe。
如果以“hello.exe readme.txt”的形式启动,argc=2, argv[0]=hello.exe, argv[1]=readme.txt
通过这两个参数,程序可以获知自身在启动时的命令行信息。
而在WinMain函数中,带有4个参数,分别是:hInstance, hPrevInstance, lpCmdLine, nShowCmd。今天就探讨hInstance的含义。
hInstance是程序的当前实例的句柄。在Windows这样的多任务操作系统中,一个程序可以同时运行多个实例。不同的实例间需要彼此区别,句柄就是干这个的。
我们想办法把句柄指示的信息显示出来,用LoadString函数,格式为:
int LoadString(
HINSTANCE hInstance,
UINT uID,
LPTSTR lpBuffer,
int nBufferMax
);
其中第2个参数uID,表示装载(Load)的是第几个信息。下面的程序将把程序的名称(IDS_APP_TITLE)信息显示在消息窗口中。
// second.cpp : 定义应用程序的入口点。
//
//
//第2个例子
//吴新 2008/01/06
#include "stdafx.h"
#include "second.h"
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
const int BUFSIZE=256;
TCHAR wAppTitle[BUFSIZE];
//初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, wAppTitle, BUFSIZE);
//获取桌面句柄
HWND hWnd = GetDesktopWindow();
//将wAppTitle显示在消息窗口中
MessageBox(hWnd, w
AppTitle,_T("第2个例子"), MB_OK);
return 0;
}
程序运行后将显示如下的窗口:
补充:
hInstance是操作系统分配给实例的指针. 程序根据hInstance访问其相应的内存空间
hInstance是操作系统分配给程序自身实例的句柄.句柄是用来标识实例的,句柄是实例指针的索引. 通过句柄能找到实例的地址.
HINSTANCE hInstance;是应用程序的实例句柄
获取方法 HINSTANCE AfxGetInstanceHandle( );
或者AfxGetApp( );
得到一个CWINAPP类的指针,其中有该句柄的成员
nCmdShow,你有SDK经验就知道,它是主窗口的状态,也就是WinMain(...)的参数之一
分享到:
相关推荐
知道了一个程序的主窗口hwnd,怎么得到这个进程的hInstance
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MessageBox (NULL, TEXT ("你好,欢迎来到VC之路"), TEXT ("欢迎"), 0) ; ...
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0); return 0 ; }
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // call shared/exported WinMain return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); } 其中#...
#include <windows.h> #include <list> #include <cstdio> #include <ctime> using namespace std; class CMap { public: CMap();...m_bmpOfFood[3]= (HBITMAP)LoadImage(NULL, foodBmp[3...
#SY-UI v0.1(麻省理工学院)开始使用 SY-UI int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow){WCHAR buff[MAX_PATH];GetCurrentDirectory(MAX_PATH,buff);path =...
ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); #define MAX_LOADSTRING 100 HINSTANCE hInst; // 当前...
HINSTANCE hInstance; // Holds The Instance Of The Application int ax[10000]; bool keys[256]; // Array Used For The Keyboard Routine bool active=TRUE; // Window Active Flag Set To TRUE By Default ...
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { MSG Message; if(!InitWindowsClass(hInstance)) return FALSE; if(!InitWindows(hInstance,nCmdShow)) ...
ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int ...
HINSTANCE hInstance; // Holds The Instance Of The Application int x,y; int xx,yy; int chakela; int amd[200][200]; int rmd[200][200]; int gmd[200][200]; int bmd[200][200]; int yanser,yanseg,yanseb; ...
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { g_hInstance = hInstance; UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER...
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { pHge = hgeCreate(HGE_VERSION); // ... pHge->Release(); return 0; } 通过HGE 指针,我们才可以访问HGE ...
其中第一个参数(HINSTANCE hInstance)是程序的实例句柄,这个实例句柄是程序的唯一标识.第二个参数(HINSTANCE hPrevInstance)是用做检查是否有多个程序实例运行,但由于Windows9X和NT(包括更高版本)是32位操作...
VC6.0下通过,基于SDK的数字时钟int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; LoadString(hInstance, IDC_CLOCK, szWindowClass, MAX_...
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("SineWave") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; ...
int WINAPI WinMain(HINSTANCE hInstance, //当前应用程序实例的句柄 HINSTANCE hPrevInstance, //前一个应用程序实例的句柄 LPSTR lpCmdLine, //指向本程序命令行的指针 int nCmdShow) //决定...
#include #include #include ...int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; HWND hwnd; HDC hDC; HGLRC hRC
int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { int iTest=1; CHostAssist assist; Test( iTest ); int iStat= assist.GuardMemDll( hInstance); ...
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char BitmapNameOut[] = "b.bmp"; /*HDC hdc; hdc = CreateDC("display",NULL,NULL,NULL); ...