Click to See Complete Forum and Search --> : ShellExecute - only one instance???


kuijer
May 17th, 1999, 04:09 AM
Hi,
I want to execute/view a program with shellexecute, but when there's already an instance of this program this instance should be activated (as in, restore/focus regain/etc).

The program im building is called 'open.exe'

When you run 'open test.txt', a shellexecute 'test.txt' is done, wich results in notepad beeing executed with test.txt as parameter (?).

when you run 'open test.txt' a second time, a new notepad instance will be created with test.txt as param.

I want to skip this new instance the second time , but only activate the first instance.

Ps. when a instance of 'test.txt - Notepad' is opened, a call like 'open test2.txt' should result in a new notepad instance with test2.txt as param.

I'm wrestiling with this problem for about 2 weeks now, and i'm getting very depressed... Please help...

scp
May 17th, 1999, 10:48 AM
Hi!

I would add the following code in the InitInstance of my program to achieve what you want. If you have any quetions, mail me... Lemme see how it goes. (This is not certainly the best way to do this.) Hope this helps!!

Santhosh




// Assuming that you have given the file to be opened
// in the command line (and nothing else)
CString strFile = (CString)m_lpCmdLine;

BOOL bRunning = FALSE;

char chWndName[50];
CString strWnd("");

HWND hParentWnd = ::GetDesktopWindow();
HWND hWnd = ::GetWindow(hParentWnd, GW_CHILD);
::GetWindowText(hWnd, chWndName, 50);

strWnd = CString(chWndName);

if(!strWnd.IsEmpty())
{
if(strWnd.Find("Notepad") != -1)
{
int nPos = strWnd.ReverseFind ('-');
CString strFileName = strWnd.Left (nPos - 1);

if (!strFile.CompareNoCase (strFileName))
{
SetForegroundWindow (hWnd);
bRunning = TRUE;
}
}
}

while(hWnd && !bRunning)
{
hWnd = ::GetWindow(hWnd, GW_HWNDNEXT);

if(hWnd)
{
::GetWindowText(hWnd, chWndName, 50);
strWnd = CString(chWndName);

if(!strWnd.IsEmpty())
{
if(strWnd.Find("Notepad") != -1)
{
int nPos = strWnd.ReverseFind ('-');
CString strFileName = strWnd.Left (nPos -1);

if (!strFile.CompareNoCase (strFileName))
{
SetForegroundWindow (hWnd);
bRunning = TRUE;
}
}
}
}
}


if (!bRunning)
{
ShellExecute (NULL,
"open",
"Notepad.exe",
strFile,
"C:\\",
SW_RESTORE);
}