Author Topic: Invoke external exe/command.  (Read 4836 times)

AssemblyChallenge

  • Member
  • **
  • Posts: 110
Invoke external exe/command.
« on: March 31, 2017, 09:07:53 AM »
Hi good fellas :biggrin:

I'm reading Here.

What I'm trying to do is not reinventing the wheel. Had a couple of instances where using Windows commands is easier than implementing the whole stuff myself. Examples: WMIC, ipconfig, etc. The idea is to invoke the command internally, store the resulting string(s) in some sort of memory buffer and parse the results (which is easier IMHO). My project is command line based (not visual & forms). So far, Iczelion's example is the closest I found.

Any guide/suggestions please?

Regards.

P1

  • Global Moderator
  • Member
  • *****
  • Posts: 66
Re: Invoke external exe/command.
« Reply #1 on: March 31, 2017, 10:07:20 AM »
Welcome to the Most Awesome Programming in the world of computing !!!

Code: [Select]
;Run a DOS command
invoke GetEnvironmentVariable, ADDR szComSpec, ADDR szDOScommand, SIZEOF szDOScommand

;Look Ma, No Window !!!
invoke GetStartupInfo,ADDR l_StartupInfo
invoke CreateProcess,
       ADDR szDOScommand,
       ADDR szDOScommandTail,
       NULL,
       NULL,
       FALSE,
       CREATE_NO_WINDOW,
       NULL,
       NULL,
       ADDR l_StartupInfo,
       ADDR l_Process_Information
In szDOScommandTail, set the DOS command you want.  "Dir > Dir.txt",0

You need to set your own local variables for CreateProcess to work. 

Tweak it for yourself and enjoy !!!

Regards,  P1

AssemblyChallenge

  • Member
  • **
  • Posts: 110
Re: Invoke external exe/command.
« Reply #2 on: March 31, 2017, 04:36:08 PM »
Thank you P1  :t

Being reading MSDN about those functions and I like what I see. Seems a lot easier to implement, just couldn't find where to store the results after child execution, if there is a way to avoid the temp file and put it on memory directly, much better  :biggrin:

AssemblyChallenge

  • Member
  • **
  • Posts: 110
Re: Invoke external exe/command.
« Reply #3 on: March 31, 2017, 04:48:49 PM »
Note to self: Maybe this could help.

jj2007

  • Member
  • *****
  • Posts: 13932
  • Assembly is fun ;-)
    • MasmBasic
Re: Invoke external exe/command.
« Reply #4 on: March 31, 2017, 04:52:28 PM »
Launch$():

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  Let esi=Launch$("cmd.exe /C echo %path%")
  Print Launch$("cmd.exe /C date /T"), Launch$("cmd.exe /C time /T")
  Inkey esi
EndOfCode


Output:
Code: [Select]
Fri 31.03.17
07:51
C:\Program Files (x86)...

Vortex

  • Member
  • *****
  • Posts: 2787
Re: Invoke external exe/command.
« Reply #5 on: April 01, 2017, 07:26:38 AM »
C library function - system() :

https://www.tutorialspoint.com/c_standard_library/c_function_system.htm

Code: [Select]
.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\msvcrt.lib


.data

cmd1        db 'ver > version.txt',0
cmd2        db 'type test.txt',0


.code

start:

    invoke  crt_system,ADDR cmd1
    invoke  crt_system,ADDR cmd2
         
    invoke  ExitProcess,0

END start

jj2007

  • Member
  • *****
  • Posts: 13932
  • Assembly is fun ;-)
    • MasmBasic
Re: Invoke external exe/command.
« Reply #6 on: April 01, 2017, 09:25:58 AM »
store the resulting string(s) in some sort of memory buffer

This is indeed what Let esi=Launch$(...) does. And under the hood you will find more or less what MSDN suggests ;-)

bomz

  • Member
  • **
  • Posts: 60
Re: Invoke external exe/command.
« Reply #7 on: December 12, 2022, 02:40:36 AM »
Code: [Select]
.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\kernel32.inc
include \Masm32\INCLUDE\msvcrt.inc
includelib \MASM32\LIB\kernel32.lib
includelib \MASM32\LIB\msvcrt.lib

.data
string db 'cmd',0,'color 9f',0
string0 db 'color 9f',0
string1 db 'echo 444',0
string2 db 'set "_str=" & echo !_str! & echo %_str%',0
string3 db 'echo %_str%',0

.code
start:
INVOKE  crt_system, addr [string+4]
INVOKE  crt_system, addr string1
INVOKE  crt_system, addr string2
INVOKE  crt_system, addr string3
;INVOKE  crt_system, addr string
invoke ExitProcess,0
end start

how determine variable?

C3

  • Regular Member
  • *
  • Posts: 35
Re: Invoke external exe/command.
« Reply #8 on: December 12, 2022, 02:47:13 AM »

zedd151

  • Member
  • *****
  • Posts: 1942
Re: Invoke external exe/command.
« Reply #9 on: December 12, 2022, 03:00:52 AM »
bomz, what variable? Could you explain a little better? From your code, I cannot understand what it is that you want to do.
Regards, zedd.
:tongue:

jj2007

  • Member
  • *****
  • Posts: 13932
  • Assembly is fun ;-)
    • MasmBasic

bomz

  • Member
  • **
  • Posts: 60
Re: Invoke external exe/command.
« Reply #11 on: December 12, 2022, 03:34:45 AM »
bomz, what variable? Could you explain a little better? From your code, I cannot understand what it is that you want to do.
utilities may get stream and commandline, back stream and set variable(?)

Quote
.data
string      db 'cmd',0,'color 9f',0
string0      db 'color 9f',0
string1      db 'echo 444',0
string2      db 'cmd /c set _str=777 & echo !_str! & echo %_str%',0
string3      db 'echo %_str%',0

Quote
.data
string      db 'cmd',0,'color 9f',0
string0      db 'color 9f',0
string1      db 'echo 444',0
string2      db 'cmd /c set _str=777 & exit /b',0
string3      db 'echo %_str%',0

Vortex

  • Member
  • *****
  • Posts: 2787
Re: Invoke external exe/command.
« Reply #12 on: December 12, 2022, 04:01:06 AM »
Hi bomz,

Here is an example for you :

Code: [Select]
include     \masm32\include\masm32rt.inc

BUFFER_SIZE equ 32

.data

EnvVar      db 'test1',0
EnvVarVal   db 'Masm',0

.data?

buffer      db BUFFER_SIZE dup(?)

.code

start:

    invoke  SetEnvironmentVariable,\
            ADDR EnvVar,ADDR EnvVarVal

    invoke  GetEnvironmentVariable,\
            ADDR EnvVar,ADDR buffer,BUFFER_SIZE

    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start

bomz

  • Member
  • **
  • Posts: 60
Re: Invoke external exe/command.
« Reply #13 on: December 14, 2022, 01:21:37 AM »
Hi bomz,

Here is an example for you :

Code: [Select]
include     \masm32\include\masm32rt.inc

BUFFER_SIZE equ 32

.data

EnvVar      db 'test1',0
EnvVarVal   db 'Masm',0

.data?

buffer      db BUFFER_SIZE dup(?)

.code

start:

    invoke  SetEnvironmentVariable,\
            ADDR EnvVar,ADDR EnvVarVal

    invoke  GetEnvironmentVariable,\
            ADDR EnvVar,ADDR buffer,BUFFER_SIZE

    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start

It work only inside created console but not inherited with call console

Vortex

  • Member
  • *****
  • Posts: 2787
Re: Invoke external exe/command.
« Reply #14 on: December 14, 2022, 06:03:25 AM »
Hi bomz,
Quote
It work only inside created console but not inherited with call console

Not so sure. The environment variables can be inherited from the parent process. Here is an example for you :

Code: [Select]
include     \masm32\include\masm32rt.inc

.data

EnvVar      db 'test1',0
EnvVarVal   db 'Masm Assembler',0
app         db 'GetEnvVar.exe test1',0

.data?

Sh_st_info STARTUPINFO <?>
Sh_pr_info PROCESS_INFORMATION <?>

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC USES esi

    invoke  SetEnvironmentVariable,\
            ADDR EnvVar,ADDR EnvVarVal

    mov     esi,OFFSET Sh_st_info

    mov     STARTUPINFO.cb[esi],\
            SIZEOF STARTUPINFO

    invoke  GetStartupInfo,esi

    xor     eax,eax
    mov     STARTUPINFO.lpReserved[esi],eax

    invoke  CreateProcess,0,ADDR app,\
            eax,eax,eax,eax,eax,eax,esi,\
            ADDR Sh_pr_info

    invoke  WaitForSingleObject,\
            Sh_pr_info.hProcess,INFINITE

    invoke  CloseHandle,Sh_pr_info.hThread
    invoke  CloseHandle,Sh_pr_info.hProcess

    ret

main ENDP

END start

EnvVarSample.exe should output the value of test1, Masm Assembler