Click to See Complete Forum and Search --> : Please Help me to get an Application Instance!!


Kelvin
June 15th, 1999, 04:04 AM
I have 3 Question regarding my simple code below!


#include "afx.h"
#include "afxwin.h"
#include "resource.h"

int
CALLBACK
PasswordDlgProc(
HWND hDlg,
UINT Message,
WPARAM wParam,
LPARAM lParam)
{
.........

}

void main()
{
HINSTANCE hMainInstance = AfxGetInstanceHandle();
DialogBoxParam(hMainInstance, (LPTSTR) MAKEINTRESOURCE(IDD_PASSWORD_DIALOG),
NULL,
(DLGPROC)PasswordDlgProc,
0);
}




Q1. Why i need to change the program setting to "Using MFC as a shared Lib" in order to successfully
do the program linking?

Q2. Why is there an assertion error at AfxGetInstanceHandle?

Q3. What should i do now in order to display my Password dialog Box???? How to get the application Instance??

Jason Teagle
June 15th, 1999, 05:47 AM
AfxGetInstanceHandle() will only work for a CWinApp-derived application, and only once the app has started (because it comes from WinMain, which is wrapped in CWinApp - InitInstance() is the first valid place to obtain this. This is the reason for an assertion.

If you are trying to create a dialogue box, how come you aren't using App Wizard to create an MFC application?

To get an HMODULE / HINSTANCE (the same thing) from a console application (which your app HAS to be if it starts with main()?), use:

HMODULE hMainInstance = GetModuleHandle("TESTCO~1.EXE");

The parameter is the 8.3 format filename of your executable (or DLL) (no path required). Long filenames will NOT work!

You can now use hMainInstance as you did before, and it will work.

I do not know why you have to link with MFC statically - as a console app, with your code plus the mod above, it linked and ran quite happily as a shared MFC.

Does this help?

as_prabhu
June 15th, 1999, 05:58 AM
Hi,
for GUI based programs on windows, you need to implement WinMain instead of main. The signature of WinMain is as follows:


int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
);




The answers
1. AfxGetInstanceHandle is an MFC function and hence you need to link to the MFC library (static or shared).

2. You get an assertion since it is a non GUI app.

3. The hInstance input parameter to WinMain can be used to initalize the dialog box.

HTH,
Prabhu