Article View: borland.public.cppbuilder.nativeapi
Article #17350Re: ShellExecute
From: nicolasr
Date: Fri, 04 Apr 2003 11:23
Date: Fri, 04 Apr 2003 11:23
99 lines
3896 bytes
3896 bytes
Hi RaynorPao,
this is what the uptodate online help at MSDN says about the return value of
ShellExecute:
<quote>
Returns a value greater than 32 if successful, or an error value that is
less than or equal to 32 otherwise. The following table lists the error
values. The return value is cast as an HINSTANCE for backward compatibility
with 16-bit Windows applications. It is not a true HINSTANCE, however. The
only thing that can be done with the returned HINSTANCE is to cast it to an
int and compare it with the value 32 or one of the error codes below.
</quote>
Here is a sketch for an alternative approach that uses CreateProcess(), as
Remy suggested. It uses FindExecutable() to find the associated application
for "test.txt" in the Registry.
hope this helps,
Nick
HWND ghwnd;
BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam);
BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam)
{
DWORD targetID = lParam;
DWORD curID = GetWindowThreadProcessId(hWnd, NULL);
if(curID==targetID)
{
ghwnd=hWnd;
return FALSE;
}
return TRUE;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
const char* fileName = "test.txt";
//adjust as needed
//################
const char* filePath = "c:\\cppbuilder\\test\\";
char exeName[MAX_PATH+1];
int err = reinterpret_cast<int>(FindExecutable(fileName,
filePath, exeName));
if(err <= 32)
{
ShowMessage(SysErrorMessage(err));
return;
}
STARTUPINFO si;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
err = CreateProcess(NULL,
(AnsiString(exeName) + " test.txt").c_str(),
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi
);
if(err == 0)
{
ShowMessage(SysErrorMessage(GetLastError()));
return;
}
//wait until the application is ready for input, otherwise the
//window may still not have been created and you don't find it.
WaitForInputIdle(pi.hProcess, 1000);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
if(!EnumWindows((WNDENUMPROC)MyEnumWndProc, pi.dwThreadId))
{
char szCaption[256]={0};
GetWindowText(ghwnd, szCaption, 256);
ShowMessage(szCaption);
}
}
Technical Details
Message-ID:
<3e8d6b26@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!38.144.126.75.MISMATCH!feed1.newsreader.com!newsreader.com!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3e8c0ea4@newsgroups.borland.com> <3e8c5793$1@newsgroups.borland.com> <3e8c5dce@newsgroups.borland.com> <3e8c7d44@newsgroups.borland.com> <3e8c8d8f$1@newsgroups.borland.com> <3e8c9481$1@newsgroups.borland.com> <3e8ca60e$2@newsgroups.borland.com> <3e8cb03c@newsgroups.borland.com> <3e8cfc38@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,bff1fa0e8e8777f2 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-04-04 03:29:56 PST From: "nicolasr" <nicolasrNOSPAMATSIGNgmx.net> Newsgroups: borland.public.cppbuilder.nativeapi References: <3e8c0ea4@newsgroups.borland.com> <3e8c5793$1@newsgroups.borland.com> <3e8c5dce@newsgroups.borland.com> <3e8c7d44@newsgroups.borland.com> <3e8c8d8f$1@newsgroups.borland.com> <3e8c9481$1@newsgroups.borland.com> <3e8ca60e$2@newsgroups.borland.com> <3e8cb03c@newsgroups.borland.com> <3e8cfc38@newsgroups.borland.com> Subject: Re: ShellExecute Date: Fri, 4 Apr 2003 13:23:34 +0200 Lines: 103 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2720.3000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 80.133.161.1 Message-ID: <3e8d6b26@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1049455398 80.133.161.1 (4 Apr 2003 03:23:18 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!38.144.126.75.MISMATCH!feed1.newsreader.com!newsreader.com!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail