Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegisterClass and FindWindow for MDI applications

Status
Not open for further replies.

burgdavid

Programmer
Jun 22, 2000
70
DE
Hello,

Because the application I'm working on is locking IMAPI of Windows XP, it cannot start twice (the second fails be can't lock IMAPI). In case the user attempt to launch it twice, most probably because the application is very slow to initialize and people believe they don't double-click correctly. I need to activate the first launched process and exit.

This is a basic trouble, and the solution should be rather easy to implement, with mutex, RegisterClass and FindWindow... But of course I can't make it work with my MDI application, neither a blank MFC wizard MDI application.

Currently my code looks like this :

[...]
#define MY_CLASS "MY_PROGRAM Class"


int AFXAPI AfxWinMain(HINSTANCE h,HINSTANCE p,LPSTR c,int s);

extern "C" int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE prev,
LPSTR cmdline,int show)
{
signal(SIGINT,SIG_IGN);

// Register our application class so other process can found us.
WNDCLASS wc;
char szMYClass[] = MY_CLASS;

wc.style = CS_DBLCLKS;// | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
wc.lpfnWndProc = (WNDPROC) MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(IDR_MAINFRAME);
wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW + 1;
wc.lpszMenuName = NULL;
wc.lpszClassName = szMYClass; // replace this with your unique class name
ATOM atomRegisteredClass = ::RegisterClass(&wc); // Unregister is automatic at application exit and does not have to be done manually.
ASSERT(atomRegisteredClass!=NULL);

AfxWinMain(ourhandle,prev,cmdline,show);
};

[...]
BOOL CMyApp::InitInstance()
{
[...]
m_hMutex = ::CreateMutex(NULL,FALSE,"ImActiv");
BOOL bOtherInstanceExist = :):GetLastError() == ERROR_ALREADY_EXISTS);

if(!::DisableIMAPI())
{
if (!bOtherInstanceExist )
{
// Close the application, if we could
// not disable IMAPI:
::AfxMessageBox(IDS_CANTDISABLE_IMAPI);
return FALSE; // Don't start! }
else
{
HWND hWndMe = NULL;
hWndMe = ::FindWindow(MY_CLASS, NULL);
if (hWndMe)
{
if (IsIconic(hWndMe))
ShowWindow(hWndMe, SW_RESTORE);
SetForegroundWindow(hWndMe);
}
else
MessageBox(NULL,
TEXT("Another User is using this Application (?)"),
"OUCH ! Not founded !", MB_OK);
return FALSE;
}
} // if (We could not disable IMAPI)
[...]

I'm searching bug for several hours now and cannot understand what is wrong. There is no assertion failure when registering (neither later btw), but the spy++ FindWindow still found an Afx:400000.8.10011:etc... as class for my MDI application.

I'm out of idea and would appreciate any help or suggestion.

Best regards,

David Burg.
 
I founded it ! The MFC base class of MainFrame register its own class unless you overwritte like this :

BOOL CMainFrame::preCreateWindow(CREATESTRUCT& cs)
{
// Use the specific class name you established earlier.
cs.lpszClass = _T("MyNewClass");

// Change the following line to call.
// CFrameWnd::preCreateWindow(cs) if this is an SDI application.
return CMDIFrameWnd::preCreateWindow(cs);
}

Now it works. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top