|
|
I would not do it by setting a value in the registry. Imagine this situation:
- Instance1 starts and sets the Value "running=1" in the registry.
- Instance2 starts and reads the value in the registry -> "running=1" -> quit.
- Instance1 exits and resets the value "running=0"
- Instance3 starts. "running=0" -> runs and sets "running=1".
Everything fine for now. But lets go further:
- Instance3 crashes or is killed by a user. Registry key is not reset. "running=1"!!!
- Instance4 starts. checks for registry key -> "running=1" -> quits!!!
So now the application cannot be started again until someone manually resets the registry key. Not nice!
Better solutions:
use a named mutexenumerate all running processes and look if your's is already runningSearch for something like Single Instance Application VC++ or Singletion Application VC++ here on CodeProject or on Google and you'll find several examples how to solve this.
Claudio
Claudio's Website
Hommingberger Gepardenforelle
|
|
|
|
|
You mean doing something like this
TCHAR Buff[255];
HANDLE hMutexOneInstance= CreateMutex(NULL,TRUE,_T("PreventSecondInstance"));
GetPrivateProfileString(_T("INSTANCE"),_T("DESIGNCLASS"),_T(""),Buff,79,_T("MYAPP.INI"));
HWND Wnd = ::FindWindow (Buff,NULL);
if (Wnd) {
::SetFocus(Wnd);
::ShowWindow(Wnd,SW_SHOWMAXIMIZED);
}
if(hMutexOneInstance)
ReleaseMutex(hMutexOneInstance);
And if No Instance is already running. Write this to ini file. Obviously registry can be used for this as well.
GetClassName(m_pMainWnd->m_hWnd,Buff,80);
WritePrivateProfileString(_T("INSTANCE"),_T("DESIGNCLASS"),Buff,_T("MYAPP.INI"));
And yes, do it in InitInstance Method.
"I would luv to change the world, but they wont give me the source code"
My Articles
|
|
|
|
|
No, I don't mean something like this (well to be honest, I don't see from your example what your writing to the ini file). Because in this situation you still have the same problem: who deletes the information in the ini-file or in the registry, when your application crashes? After a crash, your information of "application is running" stays in the ini-file/registry forever and you won't be able to start your app again because it still thinks it is running.
The idea with the mutex is the following:
When you start your application, you first create a named mutex using something like CreateMutex(null, true, "anyuniquestring"); . If this succeeds you get a handle to the mutex. Then you call GetLastError . If your application is not already running (so it's the first instance creating the named mutex) it will not return an error. But if there is already an instance running GetLastError will return ERROR_ALREADY_EXISTS . So this way you know, there is already an instance of your application running.
Claudio
Claudio's Website
Hommingberger Gepardenforelle
|
|
|
|
|
Sorry, I didnt describe it in detail. I just picked this code from one of my projects and pasted it here without much editing. In that application, I am writing the clsid (which is unique for my app) in the ini file.
You are right though, mutex can be used in a better way as u described.
"I would luv to change the world, but they wont give me the source code"
My Articles
|
|
|
|
|
See here.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
|
Hi everybody,
I have created an PopUp Menu in a CDialog...it works OK.
Now I want to create a PopUp Menu inside an ActiveX every time I click Button RIGHT of the mouse...
I have copied the code inside the function WM_RBUTTONDOWN...The problem is that it shows the Menu but it is DISABLED (it is shown but I can not select an option).
How can I do it to solve it...to make it enable?Someone has told me that it doesn't work 'cause I need to call a function for each Item of the Menu...but
Thanks
Here the code :
HMENU hMenu = ::CreatePopupMenu();
if (NULL != hMenu)
{
// add a few test items
::AppendMenu(hMenu, MF_ENABLED | MF_STRING , 1, "Change Color");
::AppendMenu(hMenu, MF_ENABLED | MF_STRING , 2, "Hide");
::AppendMenu(hMenu, MF_ENABLED | MF_STRING , 3, "Item 3-");
ClientToScreen(&point);
int sel = ::TrackPopupMenuEx(hMenu,
TPM_CENTERALIGN | TPM_RETURNCMD,
point.x,
point.y,
m_hWnd,
NULL);
CString s;
s.Format("The selected item is %d", sel);
TRACE(s);
::DestroyMenu(hMenu);
|
|
|
|
|
create a menu in your ressources
create a wm_command message handler for your menu items with the class wizard
alternatively, you can handle the wm_contextmenu message:
void CCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
CMenu* submenu;
menu.LoadMenu(IDR_POPUPMENU);
submenu = menu.GetSubMenu(0);
submenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, pWnd);
}
that's it
thomas
|
|
|
|
|
I have a problem with multiple tcp (http) connections on Windows. When my program sent many (says, 300 http-connections. The critical number of http-connections depends on speed of Internet connection) http-requests, part of them are stuck. That means, request is sent, but response is either received partly or not received at all. At that, a stuck connection isn’t dropped by web server.
This problem is present only in the Internet. There is no such problem on local machine. Based on experiment, the problem doesn’t depend on either my application sends all 300 requests to single web server or my app sends requests, says, to 6 servers by 50 requests to each server. The result is the same: some of connections are stuck for a long time.
I made a small program to investigate this problem. The program sends http-requests and receives http-response. The program creates the thread for each connection. There is opportunity to set timeout between creating connections. By default, there is no timeout and all connections are created at the same time. When I was playing with this app, I found that if I set the timeout (10-100 millisecond), http-requests worked more effectively. Sometimes, if to choose the right timeout, there are no stuck and the app uses almost all available bandwidth.
Can somebody write what’s the problem? Why are requests are stuck? What’s the cause? I could send the test app with source codes (c++) by request.
Thanks!
--
Vadim
|
|
|
|
|
The HTTP protocol requires that a client has no more than 2 open connections to the same server. Windows enforces this at the API level. There are registry entries you can change to modify this behavior. See this[^] link.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
I'm sorry for the late response.
I don't use WinInet API. I use Windows Sockets, no such limitations should be there. Plus all is working perfectly with local web server. There is the problem only through Internet.
|
|
|
|
|
meremortal wrote:
I use Windows Sockets, no such limitations should be there.
Have you confirmed this by modifying the registry settings?
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
|
No. I'm using Win2k and all requests are sending to valid IP addresses.
|
|
|
|
|
Well it doesn't matter if it is to valid or invalid address. But anyway, you don't use XP
Because as you said, if used locally it works perfectly, I would say that there will be a problem in IP stack - either on your machine or on the peer - if you open hundreds of connections simultaneously, it can have reach some SYN flood protection on the peer side or something like that. I think some tool like TCPView from sysinternals can help revealing what's up.
Anyway what's the ping time between the machines?
|
|
|
|
|
I am trying to build a Java launcher for a Java Product. Executing a Java application with a Batch file looks akward. I want a write code so that a native application written in VC++ can launch the Java application. The batch file of the application looks like this
---------------------------------------------------------------------------------------------------
set path=%PATH%;.\java\j2re1.4.2_07\bin;.\java\j2re1.4.2_07\lib;.\JMF2.1.1e\bin;
set classpath=.\JMF2.1.1e\lib\sound.jar;.\JMF2.1.1e\lib\jmf.jar;
java -classpath Sample.jar;%CLASSPATH%;%JMFHOME% -Djava.library.path=./Samplelib NrthSample.SampleMainApplication
---------------------------------------------------------------------------------------------------
Is there any way to lauch the Java application straight for the native application without the batch file? Can someone tell me which functions to use??
Regards.
|
|
|
|
|
Why don't you just ShellExecute java.exe with all the environment variables specified as command line arguments?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
iz there any restriction regarding the amount of heap memory that can be used by a program?does windows put a restriction to that?
"faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
|
|
|
|
|
I think it is about 4GB and the reason is that we are using 32bit system. Therefore, the amount of memory used by an application must not exceed 4GB.
Cheers...
|
|
|
|
|
Well actually, Windows reserves 2GB for internal use (other DLL's and such). Substract from the 2 GB the amount required by your program (total stack size of all your threads, etc..) and than the remaining memory can be used as the heap.
But if you really need to extend the amount of heap memory, you can easely write yourself a heap manager and a pointer wrapper for a file or whatever that simulates 'real' memory. And there you go, per instance of the class, you have 4GB of extra heap. (Maybe I'll write an article about it one day)
I also got the blogging virus..[^]
|
|
|
|
|
Hi Bob, I will be very interested to read your article on this. Do you mind to let me know if you have published your article on this area? My email address is ryu_thomas@hotmail.com.
Thank you...
|
|
|
|
|
|
|
No, I use some other technique which creates some files that are used as a heap.. This allows me to allocate virtual unlimited memory! Confused? Let me elaborate:
I have a class, which uses a file where you can allocate data in. You don't get an ordinary file pointer, or just an adress, you get a struct, which has a heap id (the object which handles the file) and a block id ( a ID from the index ). This allows me to move all the memory I want without affecting anything I don't want.
How do I access the memory like anything else you ask? Well, I use a heap manager that manages the heap objects (the files) and a cache. When you request a certain data block, the heap manager loads the block into the cache and you get a pointer to it. When you write to the address, it's marked as dirty, and when the cache is cleaned up, it's written back to the file.
I've almost finished the design of the little library and if you watch my blog once in a while, you should notice it when I post the first of three articles. Maybe you find them interesting.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|