|
<< Click to Display Table of Contents >> Navigation: Telegram > 2021 > 12 > 13 > Telegram_2021-12-13T19 |
2021-12-13T19:28:14
Trying to use ShellExecute in my IW app, but passing HANDLE as the first param gives an error (because there is no window that you want to use to show error messages). The documentation for other languages states that you can send NULL in for the handle, but this doesn't work for Delphi. I tried sending in 0 (zero), but then nothing is executed. Anyone know how to solve this, or has successfully used ShellExecute in an IW app?
2021-12-13T19:34:15
Loren, this method I'm using uses CreateProcess. Not sure if it fits your needs
type: code function ExecuteCommandLine(CommandLine: string; Work: string = 'C:\\'): string;
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of AnsiChar;
BytesRead: Cardinal;
WorkDir: string;
Handle: Boolean;
begin
Result := '';
with SA do begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
WorkDir := Work;
Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
nil, nil, True, 0, nil,
PChar(WorkDir), SI, PI);
CloseHandle(StdOutPipeWrite);
if Handle then
try
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Result := Result + string(Buffer);
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(PI.hProcess, INFINITE);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
CloseHandle(StdOutPipeRead);
end;
end;
2021-12-13T19:35:46
There are some tidbits I can try, thanks for sharing. I'll report back
2021-12-13T19:36:22
It works pretty well for me... I use it to create Firebird DBs on the fly
2021-12-13T19:36:48
cool
2021-12-13T19:36:59
I am sending an email using curl
2021-12-13T19:37:33
It doesn't work with cmd, so I have to send the curl string to powershell, and then it works fine
2021-12-13T19:37:52
But it doesn't work in my IW app
2021-12-13T19:38:25
user permissions maybe?
2021-12-13T19:38:35
yes
2021-12-13T19:39:03
you may try to Impersonate a user that has permissions
2021-12-13T19:39:54
See this demo:
type: link https://github.com/Atozed/IntraWeb/tree/master/15/Delphi/CGIRunner
2021-12-13T19:40:37
I got past the permissions issue, so the final frontier literally is the HWND param
2021-12-13T19:40:43
This works on Windows:
2021-12-13T19:40:44
ShellExecute(Handle, 'open',
PChar('powershell.exe'),
PChar('\curl.exe -s --user ''api:xyz'' type: link https://api.mailgun.net/v3/abc.com/messages ' +
'-F from=''John Smith < type: email mailgun@abc.com >'' -F to='' type: email abc@abc.com '' -F subject=''Work Order 9876'' -F text=''The machine is not working''\'),
'', SW_HIDE);
2021-12-13T19:40:47
he needs to execute something at OS level, which is different
2021-12-13T19:41:36
I tried sending 0 (zero) instead of Handle, but the email doesn't go out
2021-12-13T19:42:03
hum... and do you see any error log ?
2021-12-13T19:42:30
The error for \Handle\ is that my page doesn't have a parent window
2021-12-13T19:42:49
Using zero doesn't produce an error
2021-12-13T19:42:56
Let me check the actual result code
2021-12-13T19:43:29
CGIRunner also uses CreateProcess
2021-12-13T19:46:56
there is a IW demo (ISAPIRunProcess) that also uses CreateProcess
2021-12-13T19:47:21
I haven't seem a successful case of ShellExecute on ISAPI
2021-12-13T19:48:15
I'm trying to get it working in standalone first
2021-12-13T19:50:28
I get back a 42 from ShellExecute, which means that it worked
2021-12-13T19:50:35
and yet it didn't