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.
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
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:
Note to self: Maybe this (https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx) could help.
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)...
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
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 ;-)
.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?
Hey bomz,
I think you are looking for this: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable
bomz, what variable? Could you explain a little better? From your code, I cannot understand what it is that you want to do.
https://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1069
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
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
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
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
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.
I mean:
@echo off
setvar
echo %test1%
pause
I want
Quoteecho varvalue|setvar.exe varname
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:
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
as I catch need to load application to CMD.EXE process context, create variable and go out
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"
НЕТ!
bomz,
You can run ANY executable file with CreateProcess(). CMD.EXE is just a 64 bit executable file.
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?
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.
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
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"
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=
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
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
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
.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
: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
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 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
Changing Environment Variables - Win32 apps - Microsoft Learn (https://learn.microsoft.com/en-us/windows/win32/procthread/changing-environment-variables)
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