View Single Post
  #6 (permalink)  
Old 05-18-2009, 10:50 PM
theedon's Avatar
theedon
PPCGeeks Regular
Offline
 
Join Date: Jan 2007
Posts: 69
Reputation: 150
theedon is keeping up the good worktheedon is keeping up the good work
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Re: Running two instances of a program at the same time?

Quote:
Originally Posted by volDeus View Post
In most cases, when you see this behavior it's caused by the OS, not the application itself ... In my experience, the only check done by the OS to determine wether or not the application you are trying to execute is already running, is wether or not it's executable's filename matches one of the currently running processes ...

Although some more complicated (and more safely written) applications will be more "clone-aware" ... The majority of smaller WM apps will not, and can be "cloned" through the renaming method I posted above.

I have used this method many times in the past and it is effective... Unfortunately it does not work with TCPMP (I just tested it)...

Considering that, I agree with the earlier post suggesting that you use multiple applications...
get yourself a copy of ms visual studio 2008. instantiate an unmanaged code project (thats win32 C++). now compile and run. thats it... the default template has the check pre coded for you. when im back at my computer i'll post the actual lines of code. it requires the coder to specifically change the behavior. the template also hard codes the window name and the window class. the reason it does this is so that it can do a search with those two inputs to determine if the program is running. that is why changing the program name doesnt always work. the coder has to tell the program to allo such behavior. but, the default template does not allow that.

now since tcpmp is written in unmanaged code and it behaves as i have described... need i say more?


Promised code:

//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}

NOTE: The comments as well as the code are pulled directly from the windows mobile win32 C++ template.


I did some digging and here is the actual code from TCPMP

static bool_t FindRunning(const tchar_t* CmdLine)
{
HWND Wnd;
tchar_t ClassName[32];
int n = tcslen(ProgramName);
tcscpy_s(ClassName,TSIZEOF(ClassName),ProgramName) ;
tcscpy_s(ClassName+n,TSIZEOF(ClassName)-n,T("_Win"));
Wnd = FindWindow(ClassName, NULL);
if (Wnd)
{
HWND WndMain = Wnd;

while (!IsWindowEnabled(Wnd))
{
HWND Last = Wnd;
EnumWindows(EnumWindowsProc,(LPARAM)&Wnd);
if (Wnd == Last)
break;
}

SetForegroundWindow(Wnd);

if (CmdLine && CmdLine[0])
{
COPYDATASTRUCT Data;
Data.dwData = 0;
Data.cbData = (tcslen(CmdLine)+1)*sizeof(tchar_t);
Data.lpData = (PVOID)CmdLine;
SendMessage(WndMain,WM_COPYDATA,(WPARAM)WndMain,(L PARAM)&Data);
}

return 1;
}
return 0;
}

Last edited by theedon; 06-05-2009 at 04:57 PM.
Reply With Quote