News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Invoke external exe/command.

Started by AssemblyChallenge, March 31, 2017, 09:07:53 AM

Previous topic - Next topic

AssemblyChallenge

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

Welcome to the Most Awesome Programming in the world of computing !!!


;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

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


jj2007

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:
Fri 31.03.17
07:51
C:\Program Files (x86)...

Vortex

C library function - system() :

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

.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

Quote from: AssemblyChallenge on March 31, 2017, 09:07:53 AMstore 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

.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

Hey bomz,

I think you are looking for this: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable

zedd151

bomz, what variable? Could you explain a little better? From your code, I cannot understand what it is that you want to do.

jj2007

https://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1069

bomz

Quote from: zedd151 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.
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

Hi bomz,

Here is an example for you :

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

Quote from: Vortex on December 12, 2022, 04:01:06 AM
Hi bomz,

Here is an example for you :

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

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 :

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