I just finished a cryption project written in c++ WinApi, in my project I also use the registry.
My problem is this- I assigned a registry value that is 1 when the program is running, and 0 when the program is terminated (normally)- the code for turning the registry value to 0 is in the
" case WM_CLOSE: {...} " in the switch(Msg).
Hoping u understand me, I dont know how else to say this..
Anyway when you try to open the program when the registry value is set to 1, it doesn't allow it and prints out an error message, and here lies the problem- when the program terminats abnormally, the registry stays on 1, and the program cannot run (needed to be changed manually from the registry)..
how can I fix this? I thought about getting another variable, purhaps with 3 possiable values (for before program running, while, and after), but cannot think of anything that can fix this and combine the two functionalities- knowing if the program is running or not, and if the program terminated abnormally or normally...
I had ran into the same problem before.
I was using atoms and mutexes to identify unique instances of the main window.
(I only wanted only one instance of my app to be alive at any one given time)
Funny that i had crashed my program purposly just to see what happens.
In the end i had just used FindWindow() with the main window classname.
There may be other ways to do it, but this way is the easiest i found.
I just finished a cryption project written in c++ WinApi, in my project I also use the registry.
My problem is this- I assigned a registry value that is 1 when the program is running, and 0 when the program is terminated (normally)- the code for turning the registry value to 0 is in the
" case WM_CLOSE: {...} " in the switch(Msg).
Hoping u understand me, I dont know how else to say this..
Anyway when you try to open the program when the registry value is set to 1, it doesn't allow it and prints out an error message, and here lies the problem- when the program terminats abnormally, the registry stays on 1, and the program cannot run (needed to be changed manually from the registry)..
how can I fix this? I thought about getting another variable, purhaps with 3 possiable values (for before program running, while, and after), but cannot think of anything that can fix this and combine the two functionalities- knowing if the program is running or not, and if the program terminated abnormally or normally...
Thanks for any help,
cheers!
The WM_CLOSE Message to an Application is posted by Windows on the Windows Closing event.
And, your RegSetValueEx() is set to be called after the Window is Closed. that means, the control never reaches the code because Window is ALREADY Destroyed
You need to call your RegSetValueEx() before the DestroyWindow() or PostQuitMessage() API function calls. You might have given either any of these function calls inside the WM_CLOSE message handling section.
Code:
case WM_CLOSE:
RegSetValueEx(....) //Call here
DestroyWindow(hWnd);
case WM_DESTROY:
PostQuitMessage(0);
break;
Hope you got the problem...
Last edited by CoolPG; May 2nd, 2009 at 03:39 AM.
"I studied everything but never topped. Today, toppers of the world's best universities are my employees"
CoolPG, that is what I did in my code, the RegSetValueEx() is in the "case WM_CLOSE:".. I think purhaps you didn't understand my problem... thanks anyway!
bitshifter420- your solution obviously worked! thanks!
but there is one problem left though.. WHILE the program is in "Not Responding" state (which I just forced it to see what happens), another 1 same program CAN open.. after that, the program recognize the second by the "FindWindow()" function and does not allow any more to open..
this is really a minor problem, but if anyone has any ideas about fixing it, that would be great.
Bookmarks