2013-06-25 44 views

回答

1

如果你使用MFC(你没有用MFC标记你的问题,但是...),你可以使用AfxGetInstanceHandle。如果你没有使用MFC--即你有一个标准的Winapi实现),那么实例句柄作为参数传递给你的函数WinMain

创建了一个新的Win32 Windows应用程序的结果在下面的代码,从中你只需要存储hInstance方便的位置供以后

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, 
        _In_opt_ HINSTANCE hPrevInstance, 
        _In_ LPTSTR lpCmdLine, 
        _In_ int  nCmdShow) 
{ 
    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine); 

    // TODO: Place code here. 
    MSG msg; 
    HACCEL hAccelTable; 

    // Initialize global strings 
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_WIN32PROJECT4, szWindowClass, MAX_LOADSTRING); 
    MyRegisterClass(hInstance); 

    // Perform application initialization: 
    if (!InitInstance (hInstance, nCmdShow)) 
    { 
     return FALSE; 
    } 

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT4)); 

    // Main message loop: 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
    } 

    return (int) msg.wParam; 
} 

如果你不想做的样板代码,你想要你的exe文件的HINSTANCE而不是DLL,你也可以用GetModuleHandle来得到同样的东西。

HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL); 

如果这还不够,还有GetWindowLong

HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);