Discussion:
ShellExecute
(too old to reply)
Borland
2008-07-09 16:14:14 UTC
Permalink
Hi,

How to call the Windows calculator from an application ?

Thanks

Alain
Martin Nijhoff
2008-07-14 07:30:02 UTC
Permalink
Hi Alain,

Here's one way to do it:

STARTUPINFO Startup;
PROCESS_INFORMATION Process;
DWORD ExitCode;

ZeroMemory(&Startup, sizeof(Startup));
Startup.cb = sizeof(Startup);

if (CreateProcess("calc.exe", NULL, NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS, NULL, NULL, &Startup, &Process))
{
CloseHandle(Process.hThread);

do
{
// << You can do some background processing here if you want. >>

Application->ProcessMessages();

if (!GetExitCodeProcess(Process.hProcess, &ExitCode))
break;
} while (ExitCode == STILL_ACTIVE);

CloseHandle(Process.hProcess);
}
else
Application->MessageBox("Unable to launch Calculator.", "Error", MB_OK);

Hope this helps,

Martin
Clayton Arends
2008-07-14 16:53:09 UTC
Permalink
Post by Martin Nijhoff
if (CreateProcess("calc.exe", NULL, NULL, NULL, FALSE,
CreateProcess() doesn't use the search path. The path needs to be fully
qualified.
Post by Martin Nijhoff
do
{
// << You can do some background processing here if you want. >>
Application->ProcessMessages();
if (!GetExitCodeProcess(Process.hProcess, &ExitCode))
break;
} while (ExitCode == STILL_ACTIVE);
This wait routine will require near 100% CPU processing power. Use
WaitForSingleObject() instead. Eg:

while (WaitForSingleObject(Process.hProcess, 100) == WAIT_TIMEOUT)
{
if (GetCurrentThreadId() == MainThreadID)
Application->ProcessMessages();
}
Post by Martin Nijhoff
Application->MessageBox("Unable to launch Calculator.", "Error", MB_OK);
In addition to the error message you can add a call to GetLastError() to
determine what the cause of the failed launch was:

String msg = "Unable to launch Calculator. " +
SysErrorMessage(GetLastError());
Application->MessageBox(msg.c_str(), "Error", MB_OK);

Clayton
Remy Lebeau (TeamB)
2008-07-14 19:47:29 UTC
Permalink
Post by Clayton Arends
This wait routine will require near 100% CPU processing power. Use
while (WaitForSingleObject(Process.hProcess, 100) == WAIT_TIMEOUT)
{
if (GetCurrentThreadId() == MainThreadID)
Application->ProcessMessages();
}
For even more efficiency, use MsgWaitForMultipleObjects() instead, so you
can wait on the process handle and message queue at the same time, ie:

do
{
DWORD dwRet = MsgWaitForMultipleObjects(1, &Process.hProcess, FALSE,
INFINITE, QS_ALLINPUT);
if( dwRet == WAIT_OBJECT_0 )
break;

else if( dwRet == WAIT_OBJECT_0 + 1 )
Application->ProcessMessages();
}
while( true );


Gambit
Clayton Arends
2008-07-14 20:30:12 UTC
Permalink
Post by Remy Lebeau (TeamB)
For even more efficiency, use MsgWaitForMultipleObjects() instead, so you
Nice, thanks for the tip.

Clayton
alain
2008-07-23 10:01:42 UTC
Permalink
I found a simplest code

WinExec("c:\\windows\\system32\\calc.exe -f -s",1);

Clayton Arends
2008-07-14 16:58:16 UTC
Permalink
Post by Borland
Hi,
How to call the Windows calculator from an application ?
Thanks
Alain
You already had part of the correct answer in your subject line for
performing a quick application execution. The following can do the job for
you:

ShellExecute(NULL, NULL, "calc.exe", NULL, NULL, SW_NORMAL);

This might be your first time posting to these newsgroups. I am guessing
this because your "From" name is set to "Borland". Since I doubt you work
for Borland please change this to your name or something else to uniquely
identify yourself. Thanks.

HTH,
Clayton
Loading...