Telegram_2021-12-13T19

<< Click to Display Table of Contents >>

Navigation:  Telegram > 2021 > 12 > 13 >

Telegram_2021-12-13T19

Telegram link

 

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?

 


 

Telegram link

 

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;

 


 

Telegram link

 

2021-12-13T19:35:46

 

There are some tidbits I can try, thanks for sharing. I'll report back

 


 

Telegram link

 

2021-12-13T19:36:22

 

It works pretty well for me... I use it to create Firebird DBs on the fly

 


 

Telegram link

 

2021-12-13T19:36:48

 

cool

 


 

Telegram link

 

2021-12-13T19:36:59

 

I am sending an email using curl

 


 

Telegram link

 

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

 


 

Telegram link

 

2021-12-13T19:37:52

 

But it doesn't work in my IW app

 


 

Telegram link

 

2021-12-13T19:38:25

 

user permissions maybe?

 


 

Telegram link

 

2021-12-13T19:38:35

 

yes

 


 

Telegram link

 

2021-12-13T19:39:03

 

you may try to Impersonate a user that has permissions

 


 

Telegram link

 

2021-12-13T19:39:54

 

See this demo:

 

type: link https://github.com/Atozed/IntraWeb/tree/master/15/Delphi/CGIRunner

 


 

Telegram link

 

2021-12-13T19:40:37

 

I got past the permissions issue, so the final frontier literally is the HWND param

 


 

Telegram link

 

2021-12-13T19:40:43

 

This works on Windows:

 


 

Telegram link

 

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);

 


 

Telegram link

 

2021-12-13T19:40:47

 

he needs to execute something at OS level, which is different

 


 

Telegram link

 

2021-12-13T19:41:36

 

I tried sending 0 (zero) instead of Handle, but the email doesn't go out

 


 

Telegram link

 

2021-12-13T19:42:03

 

hum... and do you see any error log ?

 


 

Telegram link

 

2021-12-13T19:42:30

 

The error for \Handle\ is that my page doesn't have a parent window

 


 

Telegram link

 

2021-12-13T19:42:49

 

Using zero doesn't produce an error

 


 

Telegram link

 

2021-12-13T19:42:56

 

Let me check the actual result code

 


 

Telegram link

 

2021-12-13T19:43:29

 

CGIRunner also uses CreateProcess

 


 

Telegram link

 

2021-12-13T19:46:56

 

there is a IW demo (ISAPIRunProcess) that also uses CreateProcess

 


 

Telegram link

 

2021-12-13T19:47:21

 

I haven't seem a successful case of ShellExecute on ISAPI

 


 

Telegram link

 

2021-12-13T19:48:15

 

I'm trying to get it working in standalone first

 


 

Telegram link

 

2021-12-13T19:50:28

 

I get back a 42 from ShellExecute, which means that it worked

 


 

Telegram link

 

2021-12-13T19:50:35

 

and yet it didn't