`
huozheleisi
  • 浏览: 1235594 次
文章分类
社区版块
存档分类
最新评论

HINSTANCE

 
阅读更多

在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

    知道了一个程序的主窗口hwnd,怎么得到这个进程的hInstance

    使用API写windows程序

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)  {   MessageBox (NULL, TEXT ("你好,欢迎来到VC之路"), TEXT ("欢迎"), 0) ;   ...

    Windows程序设计第五版

    int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0); return 0 ; }

    MFC的程序框架剖析

    _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // call shared/exported WinMain return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); } 其中#...

    贪吃蛇C++版(转)

    #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:沉阳某程序员的简单开源directui

    #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 =...

    C++做菜单和对话框

    ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); #define MAX_LOADSTRING 100 HINSTANCE hInst; // 当前...

    gertt点阵图读取器(opengl)

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

    用鼠标画矩形的win32代码

    ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int ...

    opengl画图程序附带源代码

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

    Win7下绕过UAC代码

    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { g_hInstance = hInstance; UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER...

    HGE_系列教材(1-9)

    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { pHge = hgeCreate(HGE_VERSION); // ... pHge->Release(); return 0; } 通过HGE 指针,我们才可以访问HGE ...

    Windows编程基础--窗体篇.rar_WinMain_Windows编程_pcm_winapi_winbase.h

    其中第一个参数(HINSTANCE hInstance)是程序的实例句柄,这个实例句柄是程序的唯一标识.第二个参数(HINSTANCE hPrevInstance)是用做检查是否有多个程序实例运行,但由于Windows9X和NT(包括更高版本)是32位操作...

    VC6.0 Win32 数字时钟

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

    VC++实现MFC程序方程求根的程序以及源代码

    int WINAPI WinMain(HINSTANCE hInstance, //当前应用程序实例的句柄 HINSTANCE hPrevInstance, //前一个应用程序实例的句柄 LPSTR lpCmdLine, //指向本程序命令行的指针 int nCmdShow) //决定...

    soil测试程序,可以正常运行

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

    c++实现自动抓图的源码

    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char BitmapNameOut[] = "b.bmp"; /*HDC hdc; hdc = CreateDC("display",NULL,NULL,NULL); ...

Global site tag (gtag.js) - Google Analytics