Jump to content
Geule

OpenSSL Commands

Recommended Posts

Hello!
I can run openssl commands directly from the Windows prompt and it runs normally. But how can I do this directly in Delphi. MS-DOS commands run normally in Delphi. But openssl commands don't. Anyone who can help me with this?

A simple example that I'm trying to run: "openssl version"

Share this post


Link to post

And what happens, if you try to run it? Are there any error messages? How do you try to run it?

Share this post


Link to post
3 hours ago, Geule said:

how can I do this directly in Delphi.

Use the Win32 CreateProcess() function.  Of course, a better option might be to NOT shell out to openssl.exe at all, but to use the OpenSSL API directly in your own code instead.  It really depends on what you are trying to accomplish exactly.

3 hours ago, Geule said:

MS-DOS commands run normally in Delphi.

Only if you execute them as parameters to cmd.exe.

3 hours ago, Geule said:

But openssl commands don't.

Then you are doing something wrong, but we can't see what you are actually doing. Please show your actual code you are having trouble with.

3 hours ago, Geule said:

Anyone who can help me with this?

A simple example that I'm trying to run: "openssl version"

For example:

uses
  ..., Windows;
  
procedure RunCommandLine(CmdLine: String);
var
  si: TStartupInfo;
  pi: TProcessInformation;
begin
  {$IFDEF UNICODE}UniqueString(CmdLine);{$ENDIF}
  ZeroMemory(@si, SizeOf(si));
  si.cb := SizeOf(si);
  if not CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0, nil, nil, si, pi) then
    RaiseLastOSError;
  ...
  CloseHandle(pi.hThread);
  CloseHandle(pi.hProcess);
end;

...

RunCommandLine('C:\path\openssl.exe version');

Just note that openssl.exe is a console app, so if you run this code in a non-console program, it will create a new console that will close when openssl.exe exits.  If you actually want to see the console window to read the output, or even to capture the output into your own code, requires more work than this code shows.

Edited by Remy Lebeau

Share this post


Link to post
2 hours ago, dummzeuch said:

And what happens, if you try to run it? Are there any error messages? How do you try to run it?

Prompt open and close quickly...

Share this post


Link to post
2 hours ago, Remy Lebeau said:

Use the Win32 CreateProcess() function.  Of course, a better option might be to NOT shell out to openssl.exe at all, but to use the OpenSSL API directly in your own code instead.  It really depends on what you are trying to accomplish exactly.

Only if you execute them as parameters to cmd.exe.

Then you are doing something wrong, but we can't see what you are actually doing. Please show your actual code you are having trouble with.

For example:


uses
  ..., Windows;
  
procedure RunCommandLine(CmdLine: String);
var
  si: TStartupInfo;
  pi: TProcessInformation;
begin
  ZeroMemory(@si, SizeOf(si));
  si.cb := SizeOf(si);
  if not CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0, nil, nil, si, pi) then
    RaiseLastOSError;
  ...
  CloseHandle(pi.hThread);
  CloseHandle(pi.hProcess);
end;

...

RunCommandLine('C:\path\openssl.exe version');

Just note that openssl.exe is a console app, so if you run this code in a non-console program, it will create a new console that will close when openssl.exe exits.  If you actually want to see the console window to read the output, or even to capture the output into your own code, requires more work than this code shows.

I got this error Access violation KERNELBASE.dll

OpenSSL version installed 3.2.0 23 Nov 2023

Share this post


Link to post
13 minutes ago, Geule said:

I got this error Access violation KERNELBASE.dll

I updated my earlier example.  Make sure CmdLine is not pointing at a string literal when passing it to CreateProcess(), as the 2nd parameter of the Unicode version of CreateProcess() (CreateProcessW) is not allowed to point at read-only memory.

Edited by Remy Lebeau

Share this post


Link to post
19 minutes ago, Remy Lebeau said:

I updated my earlier example.  Make sure CmdLine is not pointing at a string literal when passing it to CreateProcess(), as the 2nd parameter of the Unicode version of CreateProcess() (CreateProcessW) is not allowed to point at read-only memory.

With the Unicode directive it worked just fine! And best of all, it worked in my own code! Thank you very much!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×