The MASM Forum

General => The Campus => Topic started by: AssemblyChallenge on March 31, 2017, 09:07:53 AM

Title: Invoke external exe/command.
Post by: AssemblyChallenge on March 31, 2017, 09:07:53 AM
Hi good fellas :biggrin:

I'm reading Here. (http://win32assembly.programminghorizon.com/tut14.html)

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.
Title: Re: Invoke external exe/command.
Post by: P1 on March 31, 2017, 10:07:20 AM
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
Title: Re: Invoke external exe/command.
Post by: AssemblyChallenge 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:
Title: Re: Invoke external exe/command.
Post by: AssemblyChallenge on March 31, 2017, 04:48:49 PM
Note to self: Maybe this (https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx) could help.
Title: Re: Invoke external exe/command.
Post by: jj2007 on March 31, 2017, 04:52:28 PM
Launch$(): (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1047)

include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
  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)...
Title: Re: Invoke external exe/command.
Post by: Vortex on April 01, 2017, 07:26:38 AM
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
Title: Re: Invoke external exe/command.
Post by: jj2007 on April 01, 2017, 09:25:58 AM
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$(...) (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1047) does. And under the hood you will find more or less what MSDN suggests ;-)
Title: Re: Invoke external exe/command.
Post by: bomz on December 12, 2022, 02:40:36 AM
.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?
Title: Re: Invoke external exe/command.
Post by: C3 on December 12, 2022, 02:47:13 AM
Hey bomz,

I think you are looking for this: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable
Title: Re: Invoke external exe/command.
Post by: 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.
Title: Re: Invoke external exe/command.
Post by: jj2007 on December 12, 2022, 03:04:39 AM
https://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1069
Title: Re: Invoke external exe/command.
Post by: bomz on December 12, 2022, 03:34:45 AM
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
Title: Re: Invoke external exe/command.
Post by: 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
Title: Re: Invoke external exe/command.
Post by: bomz on December 14, 2022, 01:21:37 AM
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
Title: Re: Invoke external exe/command.
Post by: Vortex 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 :

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
Title: Re: Invoke external exe/command.
Post by: jj2007 on December 14, 2022, 07:07:57 AM
One more ;-)

include \masm32\MasmBasic\MasmBasic.inc
  Init
  Launch "Child.exe", passdata, FileRead$("\Masm32\include\Masm32rt.inc")
EndOfCode


include \masm32\MasmBasic\MasmBasic.inc
  Init
  PrintLine "Received from parent process:", CrLf$, ParentData$()
EndOfCode


There is a 64k limit: msvcrt.inc is ok, setupapi.inc is too long.
Title: Re: Invoke external exe/command.
Post by: bomz on December 14, 2022, 09:01:32 AM
I mean:
@echo off
setvar
echo %test1%
pause

I want
Quoteecho varvalue|setvar.exe varname
Title: Re: Invoke external exe/command.
Post by: jj2007 on December 14, 2022, 09:17:32 AM
Please be more verbose: try a full paragraph explaining what you want to do. Maybe I am just too stupid to understand what you want, but verbosity may help. If English is not your first language, try deepl.com (https://www.deepl.com/translator) :thup:
Title: Re: Invoke external exe/command.
Post by: bomz on December 14, 2022, 10:14:57 AM
Quote from: jj2007 on December 14, 2022, 09:17:32 AM
Please be more verbose: try a full paragraph explaining what you want to do. Maybe I am just too stupid to understand what you want, but verbosity may help. If English is not your first language, try deepl.com (https://www.deepl.com/translator) :thup:
I suppose this string make all clear : string2 db 'set "_str=" & echo !_str! & echo %_str%',0
Title: Re: Invoke external exe/command.
Post by: bomz on December 15, 2022, 03:06:45 AM
as I catch need to load application to CMD.EXE process context, create variable and go out
Title: Re: Invoke external exe/command.
Post by: jj2007 on December 15, 2022, 03:15:07 AM
Quote from: bomz on December 15, 2022, 03:06:45 AM
as I catch need to load application to CMD.EXE process context, create variable and go out

Doesn't look like English. What is your native language? Please formulate a complete, meaningful phrase and let deepl.com translate it, so that we have a chance to understand what you really want.

Quote from: bomz on December 14, 2022, 10:14:57 AM
Quote from: jj2007 on December 14, 2022, 09:17:32 AM
Please be more verbose: try a full paragraph explaining what you want to do. Maybe I am just too stupid to understand what you want, but verbosity may help. If English is not your first language, try deepl.com (https://www.deepl.com/translator) :thup:
I suppose this string make all clear : string2 db 'set "_str=" & echo !_str! & echo %_str%',0

No, "this string make all clear" НЕТ!
Title: Re: Invoke external exe/command.
Post by: hutch-- on December 15, 2022, 03:18:33 AM
bomz,

You can run ANY executable file with CreateProcess(). CMD.EXE is just a 64 bit executable file.
Title: Re: Invoke external exe/command.
Post by: bomz on December 15, 2022, 10:14:48 AM
Quote from: hutch-- on December 15, 2022, 03:18:33 AM
bomz,

You can run ANY executable file with CreateProcess(). CMD.EXE is just a 64 bit executable file.
and how create variable global to two console?
Title: Re: Invoke external exe/command.
Post by: hutch-- on December 15, 2022, 10:57:19 AM
I don't claim to uinderstand what you are after but creating the same named GLOBAL variable in two apps is easy. You need some method of inter application communication to connect them.

Two (2) techniques,
1. SendMessage using the HWND_BROADCAST message for signalling.

        rcall SendMessage,HWND_BROADCAST,PM_COMMAND,0,0

2. Memory mapped files for data.

If one is calling the other, the command line works OK.
Title: Re: Invoke external exe/command.
Post by: bomz on December 15, 2022, 11:19:17 AM
I am say about CMD variable. I want create variable inside batch file
masm32 language 100% clear

@echo off
setvar
echo %newvar%
pause


I am creating batch file now and have problem to create global variable standard methods because number of iterations to big inside cycles. So it is not so easy as may seem
Title: Re: Invoke external exe/command.
Post by: _japheth on December 15, 2022, 08:38:08 PM
Quote from: bomz on December 15, 2022, 11:19:17 AM
I am say about CMD variable. I want create variable inside batch file
masm32 language 100% clear

@echo off
setvar
echo %newvar%
pause


I am creating batch file now and have problem to create global variable standard methods because number of iterations to big inside cycles. So it is not so easy as may seem

What about using the /P option?

C:\>echo abc >tmpfile & set /P newvar= <tmpfile

should result in "newvar=abc"




Title: Re: Invoke external exe/command.
Post by: bomz on December 15, 2022, 10:07:18 PM
Quote from: _japheth on December 15, 2022, 08:38:08 PM
Quote from: bomz on December 15, 2022, 11:19:17 AM
I am say about CMD variable. I want create variable inside batch file
masm32 language 100% clear

@echo off
setvar
echo %newvar%
pause


I am creating batch file now and have problem to create global variable standard methods because number of iterations to big inside cycles. So it is not so easy as may seem

What about using the /P option?

C:\>echo abc >tmpfile & set /P newvar= <tmpfile

should result in "newvar=abc"
much better but how do it without TMP file
echo abc|set /p bewvar=
Title: Re: Invoke external exe/command.
Post by: TimoVJL on December 15, 2022, 11:20:00 PM
CMD.EXE have SETX command and can be problematic.


https://stackoverflow.com/questions/13222724/command-line-to-remove-an-environment-variable-from-the-os-level-configuration
Title: Re: Invoke external exe/command.
Post by: _japheth on December 16, 2022, 12:14:30 AM
Quote from: bomz on December 15, 2022, 10:07:18 PM
echo abc|set /p bewvar=

You're right, this "should" work, but obviously does not.

As a workaround, perhaps you might use FOR:

c:\>for /F %i in ('echo bomz') do set newvar=%i
Title: Re: Invoke external exe/command.
Post by: bomz on December 16, 2022, 12:17:48 AM
Quote from: _japheth on December 16, 2022, 12:14:30 AM
Quote from: bomz on December 15, 2022, 10:07:18 PM
echo abc|set /p bewvar=

You're right, this "should" work, but obviously does not.

As a workaround, perhaps you might use FOR:

c:\>for /F %i in ('echo bomz') do set newvar=%i
I am exactly want make utility which allow avoid FOR such cases
Title: Re: Invoke external exe/command.
Post by: bomz on December 16, 2022, 12:38:23 PM
.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
_si STARTUPINFO <0>
_pi PROCESS_INFORMATION <0>
cmd db "cmd.exe",0
EnvVar db 'test',0
EnvVarVal db 'Masm',0

.code
start:
invoke  SetEnvironmentVariable,addr EnvVar,addr EnvVarVal
mov _si.cb,size STARTUPINFO
invoke CreateProcess,0,addr cmd,0,0,TRUE,0,0,0,addr _si,addr _pi
invoke ExitProcess,NULL
end start


in child cmd console echo %test% work
parent cmd console destroy with echo %test%
something for beginning
Title: Re: Invoke external exe/command.
Post by: bomz on December 19, 2022, 01:09:49 PM
 :rolleyes:
STARTUPINFO any influence at all. restart cmd?
setlocal EnableDelayedExpansion
endlocal

.data
_si STARTUPINFO <sizeof STARTUPINFO,0,0,offset EnvVar,550,550,300,300,0,0,0,0,0,0,0,0,0,0>
_pi PROCESS_INFORMATION <0,0,0,0>
cmd db 'cmd.exe /c color 9f & echo %test% & pause',0
EnvVar db 'test',0
EnvVarVal db 'Masm',0

.code
start:
invoke  SetEnvironmentVariable,addr EnvVar,addr EnvVarVal
invoke CreateProcess,0,addr cmd,0,0,TRUE,0,0,0,addr _si,addr _pi
invoke ExitProcess,NULL
end start
Title: Re: Invoke external exe/command.
Post by: TimoVJL on December 19, 2022, 09:07:42 PM
It only put environment variable for running process, don't change CMD environment variables.

EDIT
https://devblogs.microsoft.com/oldnewthing/20150915-00/?p=91591
Title: Re: Invoke external exe/command.
Post by: bomz on December 20, 2022, 01:11:15 PM
Quote from: TimoVJL on December 19, 2022, 09:07:42 PM
It only put environment variable for running process, don't change CMD environment variables.

EDIT
https://devblogs.microsoft.com/oldnewthing/20150915-00/?p=91591

Quote.data
_si      STARTUPINFO <sizeof STARTUPINFO,0,0,offset EnvVar,0,0,0,0,0,0,0,0,0,0,0,0,0,0>
_pi      PROCESS_INFORMATION <0,0,0,0>
cmd      db 'cmd.exe /c color 9f & echo %test% & echo %test1% & pause',0
EnvVar      db 'test',0
EnvVarVal   db 'Masm',0

.code
start:
   invoke  SetEnvironmentVariable,addr EnvVar,addr EnvVarVal
   invoke CreateProcess,0,addr cmd,0,0,TRUE,0,0,0,addr _si,addr _pi
   invoke ExitProcess,NULL
end start

Quote@echo off
color 0A
cd /d %~dp0
set test1=test
noname
goto :eof
Title: Re: Invoke external exe/command.
Post by: bomz on December 23, 2022, 11:48:00 PM
Changing Environment Variables - Win32 apps - Microsoft Learn (https://learn.microsoft.com/en-us/windows/win32/procthread/changing-environment-variables)
Title: Re: Invoke external exe/command.
Post by: bomz on December 25, 2022, 05:59:58 PM
hard to understand why batch command 'set=', work with files and can't with console input. (console pipe eng.?)
only in real hand regimes. may be 13,10 as wrong bytes... hook cmd?

anybody know which keys need to be excluded?
;invoke crt__getch
;invoke ExitProcess,0

invoke GetStdHandle,STD_INPUT_HANDLE
mov ebx,eax
@@:
invoke ReadConsoleInput,ebx,addr Input,3136,addr _rb
cmp Input.KeyEvent.bKeyDown,1
je @B
cmp Input.MouseEvent.dwEventFlags,0
jne @B
cmp Input.MouseEvent.dwControlKeyState,0
je @B
movzx eax,Input.KeyEvent.wVirtualKeyCode
cmp eax,VK_SHIFT
jb @F
cmp eax,VK_CAPITAL
jbe @B
cmp eax,VK_SCROLL
je @B
@@:
invoke ExitProcess,0
end start