News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

command prompt command line ?

Started by Slugsnack, July 12, 2009, 09:30:26 PM

Previous topic - Next topic

Slugsnack

hi,

i'm making a program that can open up a command prompt to ping and to tracert a user defined IP. so my first way was to have an IP control and two buttons. one for trace and one for ping. then when each button is pressed use shellexecute to load an instance of cmd.exe. then use postmessage to post the 'string' that is the command. eg. 'ping 192.168.1.254'

i've already coded two functions for it as follows :

PostString proc uses ebx esi edi hWindow:DWORD, lpszStr:DWORD

xor ebx, ebx
mov esi, lpszStr
mov edi, len(esi)

    .WHILE ebx != edi

            invoke PostMessage, hWindow, WM_CHAR, byte ptr ds:[esi+ebx], NULL
        inc ebx

    .ENDW

    invoke MapVirtualKey, VK_RETURN, MAPVK_VK_TO_VSC
    invoke PostMessage, hWindow, WM_CHAR, VK_RETURN, eax

ret
PostString endp


ConvertIP proc uses ebx edi esi IPAddr:DWORD

mov ebx, IPAddr

shr ebx, 16
movzx edx, bh

mov edi, ustr$(edx)
mov eax, add$(edi, chr$("."))
movzx edx, bl
mov eax, add$(edi, ustr$(edx))
mov eax, add$(edi, chr$("."))
mov ebx, IPAddr
movzx edx, bh
mov eax, add$(edi, ustr$(edx))
mov eax, add$(edi, chr$("."))
movzx edx, bl
mov eax, add$(edi, ustr$(edx))

mov eax, edi

ret
ConvertIP endp


YES ! i know my ConvertIP function is very very inefficient : [ i might improve it once i get this thing working.

well anyway so i could actually do it that way.. but i wondered if there is a way that i can emulate typing into the 'run' dialog. without opening it up and posting a string there..

then assuming there is such a method to do this, is there then a way to keep the command prompt window open ? in terms of the tracert my user wants to be able to keep the information there to see.

if all else fails.. i will use my way of shellexecute/poststring but it seems a very poor method

sonic

You could use pipe (icztute 21 may help)

dedndave

i think CreateProcess could do what you want rather nicely
you have to find the proper location of cmd.exe, is all, which varies slightly from one OS to another (C:\WinNT, C:\Windows, etc.)
when you open cmd.exe, you can specify a child to run under it on the command line

Slugsnack

Quote from: sonic on July 13, 2009, 05:53:11 PM
You could use pipe (icztute 21 may help)

reading that now but i don't think it's what i want. it lets me run it command line. but for the tracert for example. running that command line instead of via command prompt causes the window to close immediately after the the tracert completes. i'll finish reading though tomorrow in my lunch break..

Quote from: dedndave on July 13, 2009, 07:43:10 PM
i think CreateProcess could do what you want rather nicely
you have to find the proper location of cmd.exe, is all, which varies slightly from one OS to another (C:\WinNT, C:\Windows, etc.)
when you open cmd.exe, you can specify a child to run under it on the command line

yeah what i am doing now is createprocess a cmd.exe. then enumwindows and finding the window that after i call getwindowthreadprocessid + openprocess it gets the same hprocess as what createprocess gave out in PROCESS_INFORMATION then using my poststring function on the hwnd

dedndave

i might be tempted to place a temporary in the registry so that you didn't have to enumerate (i.e. passing the PID)
might not save much in size, as code  for the registry is a bag of worms - lol
maybe it could be passed in the environment table - not sure how your parent would read that

Slugsnack

nono that isn't the problem. but you can't get hwnd directly from pid as far as i know. so easiest way i knew was to just pass handle then enumwindows till a window's process matched. uploaded current code ( i have not cleaned it yet !!!!!! ) just in case you're bored enough to help me find the bug. don't waste your time on it unless you got nothing better to do though cause it's probably a stupid mistake that i'll work out sooner or later. but so rushed for time now i got a job.. dammit. will look over again at lunch break tomorrow, gotta sleep soon : [

ps. i know my convertip function causes a buffer overflow. will fix that soon

[attachment deleted by admin]

dedndave

Quote            invoke PostMessage, hWindow, WM_CHAR, byte ptr ds:[esi+ebx], NULL

that's the first time i have seen a ds: segment override in a win32 program - lol
other than that, i don't see anything that hits me in the face
much of your code is kinda over my head, for now - lol
i'm learning, though  :P

Slugsnack

segments ?!?!??! i thought i was so fortunately born into an age where i don't have to care about segments !!!!!!! OMG panicking.. thought that is 16 bit stuff.. : |

Slugsnack

#8
also oops you can take out the getclassname in enumfunc that was something i put in there earlier and forgot to take out properly. fixed the buffer overflow problem..

( i think )

.data?

szIP BYTE 255 DUP (?)

.code

ConvertIP proc uses ebx edi esi IPAddr:DWORD

mov ebx, IPAddr

shr ebx, 16
movzx edx, bh

mov edi, ustr$(edx)
mov eax, add$(addr szIP, edi)
mov eax, add$(addr szIP, chr$("."))
movzx edx, bl
mov eax, add$(addr szIP, ustr$(edx))
mov eax, add$(addr szIP, chr$("."))
mov ebx, IPAddr
movzx edx, bh
mov eax, add$(addr szIP, ustr$(edx))
mov eax, add$(addr szIP, chr$("."))
movzx edx, bl
mov eax, add$(addr szIP, ustr$(edx))

mov eax, edi

ret
ConvertIP endp


will fix and optimise tomorrow at lunch break ^_^

night night

//edit : found another error just now ( lunch break ). at the newping there needs either to be a 'xor ebx, ebx' at the start or the createprocess ebxs need to be changed to NULLs. maybe that is causing hProcess to be incorrect or something.. dunno will test when i get home

donkey

The RunInConsole function from WinExplorer will open a console and run a command in it using CreateProcess for either 9x or NT based OS versions. The source is available on my website, I will not post GoAsm code in this forum as it seems to offend some people and don't much feel like translating it to MASM.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Slugsnack

Quote from: donkey on July 14, 2009, 02:11:07 PM
The RunInConsole function from WinExplorer will open a console and run a command in it using CreateProcess for either 9x or NT based OS versions. The source is available on my website, I will not post GoAsm code in this forum as it seems to offend some people and don't much feel like translating it to MASM.


RunInConsole FRAME pAppname
LOCAL sui :STARTUPINFO
LOCAL pi :PROCESS_INFORMATION
LOCAL SystemPath[MAX_PATH] :B
LOCAL TextBuffer[MAX_PATH] :B

mov D[TextBuffer],0

mov D[sui.cb],SIZEOF STARTUPINFO
invoke GetStartupInfo,OFFSET sui
mov eax,[f9x]
test eax,eax
jz >>
mov B[SystemPath],22h
invoke GetWindowsDirectory,OFFSET SystemPath,MAX_PATH
invoke szCat,OFFSET SystemPath,'\command.com /K "'
invoke szCat,OFFSET SystemPath,[pAppname]
invoke szCat,OFFSET SystemPath,'"'
invoke CreateProcess,NULL,OFFSET SystemPath,NULL,NULL,\
TRUE,NULL,NULL,NULL,OFFSET sui,OFFSET pi
jmp >>L1
:
invoke GetSystemDirectory,OFFSET SystemPath,MAX_PATH
invoke szCat,OFFSET SystemPath,"\cmd.exe"
invoke szCopy,OFFSET TextBuffer,'/K "'
invoke szCat,OFFSET TextBuffer,[pAppname]
invoke szCat,OFFSET TextBuffer,'"'
invoke CreateProcess,OFFSET SystemPath,OFFSET TextBuffer,NULL,NULL,\
TRUE,NULL,NULL,NULL,OFFSET sui,OFFSET pi
L1:

invoke CloseHandle,[pi.hProcess]
invoke CloseHandle,[pi.hThread]
RET

ENDF


i'm sure none object to me posting that function since otherwise nobody will be able to follow my comments regarding it haha

does that code not just run a console though ?! sorry i don't know goasm but that's what it seems to do but running different versions depending on the system

i found this nifty page :

http://www.ss64.com/nt/cmd.html

and i realised if i enter this into start >> run, it does what i want :

CMD /A /K "tracert www.google.com"

but how do you do that in code ? of course there is the stupid way of opening the run dialog then sending the command with PostString but i'm sure there is a better way to do it programatically

disintx

His code will do it. You would simply pass tracert www.google.com as pAppname.

invoke szCat,OFFSET SystemPath,"\cmd.exe"
invoke szCopy,OFFSET TextBuffer,'/K "'
invoke szCat,OFFSET TextBuffer,[pAppname]
invoke szCat,OFFSET TextBuffer,'"'


Seems to be doing exactly what you are saying here:
CMD /A /K "tracert www.google.com"

minus the actual command though, as it is passed..

Slugsnack

i'm an idiot who just wasted a whole day overcomplicating the situation

xor ebx, ebx

    invoke GetStartupInfo, addr StartupInfo
    invoke CreateProcess, ebx, chr$("C:\Windows\System32\cmd.exe /A /K tracert www.google.com"), ebx, ebx, FALSE, NORMAL_PRIORITY_CLASS, ebx, ebx, addr StartupInfo, addr ProcessInfo


    invoke CloseHandle, ProcessInfo.hProcess
    invoke ExitProcess, ebx


oh my god someone kill me please

thanks all  :boohoo:

donkey

You should also close the thread handle.

invoke CloseHandle, ProcessInfo.hThread
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Slugsnack