AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CMDret - return output from console progs [DLL version]
Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Utilities & Resources
View previous topic :: View next topic  
Author Message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Sun May 22, 2005 6:41 pm    Post subject: CMDret - return output from console progs [DLL version] Reply with quote

CMDret
- A Dll file designed for retrieving output from console based applications and saving the output in a variable and/or redirecting to a control without displaying the console window or creating/using temporary files.

Latest release:
CMDret 4d Beta - Download (cmdret.dll, cmdstub.exe, several test scripts)
- updated all functions for Windows 2000 compatibility (hopefully)
- This version contains the following functions:
RunRedirect(CMDin)
RunInControl(CMDin, ControlHwnd)
RunReturn(CMDin, CMDout)
RunWEx(CMDin, WorkingDir, CMDin, CMDout, CMDerr)
RunWCB(CMDin, CallbackAddress) - callback - (PID, NumberOfCharsInOutput, Output, FutureUse)


Previous Releases:
CMDret - v3.2.1 (DLL version)

CMDret v3.x is a .DLL file (Freeware) that has been designed to return the output from a console program to a variable (rather than redirect to a textbox like the command line version). Version 3.1 (or greater) now includes a function that has the ability to output to a control and should be more stable than the command line version. Please keep in mind that CMDret is currently a beta realease. Any feedback would be greatly appreciated Wink .

Win9.x users
Note: only 32 bit console applications will currently work with the this dll version of CMDret (v3.1.2 or lower). Calls that require command.com will likely not produce any output and may crash. To avoid this I have included a file named "cmdstub.exe" with the download (in the Win9x folder in the zip file). This file should be used when calling 16 bit console applications to enable returning output. Example: cmdstub.exe command.com /C set


Updated: March 15, 2008 - version 3.2.1
- modified: the methods used in the functions to detect when a console program has finished executing to improve stability, reliability and efficiency (hopefully)
- modified required parameters for the RunInControl function
- Added: sample scripts

Updated: June 1, 2005 - version 3.1.2
- Added cmdstub.exe to the download to support 16 bit console applications

Updated: May 30, 2005 - version 3.1.2
- Tweaked the RunReturn function
- Added another function to the dll (RunWEx) to allow StdInput, StdOutput, StdError

Updated: May 24, 2005 - version 3.1.1
- Added another function to the dll (RunReturn) to allow returning output to a variable. This function does not require the additional LoadLibrary, lstrcpyA, and FreeLibrary calls.

Current functions:
RunReturn(CMD, StrOut)
- CMD - the console program/command to execute (str)
- StrOut - the variable used to store the output (str)
= If the function fails, the return value is zero

RunWEx(CMD, CMDdir, CMDin, CMDout, CMDerr)
- CMD - the console program/command to execute (str)
- CMDdir - Future Use (working directory - not currently functional) (str)
- CMDin - StdInput String (str)
- CMDout - the variable used to store StdOutput output (str)
- CMDerr - the variable used to store StdError output (str)
= If the function fails, the return value is zero

RunRedirect(CMD)
- Where CMD is the console program/command to execute (str)

RunInControl(CMD, WinHandle, CtrlClass, CtrlInstance)
- CMD should be the console program/command to execute (str)
- CtrlHWND should be the handle to the control to stream into
= If the function fails, the return value is zero

Download the latest version (DLL Version 3.2.1)

Download (DLL Version 3.1.2)
Alternate Download for version 3.1.2

BCX Source (version 3.1.2) - not currently available...

Notes:
- To check which version you currently have - right click on cmdret.dll, select Properties, and click the Version tab
- When trying to retrieve a directory listing be sure to specify a directory after a 'DIR' command. ( COMSPEC " /C Dir c:\" )

Sample Code:
Code:
; CMDret DLL version 3.1.1 (or greater) required

ret1 := CMDret(COMSPEC " /C set")
MsgBox, %ret1%

CMDret(CMD)
{
  VarSetCapacity(StrOut, 10000)
  RetVal := DllCall("cmdret.dll\RunReturn", "str", CMD, "str", StrOut)
  Return, %StrOut%
}


Code:
; CMDret (DLL version 3.0 or greater required) examples

CMD := COMSPEC " /C set"

  VarSetCapacity(StrOut, 1000)
  hModule := DllCall("LoadLibrary", "str", "cmdret.dll","UInt")
  DllCall("lstrcpyA", "str", StrOut, "int", DllCall("cmdret.dll\RunRedirect", "str", CMD))
  DllCall("FreeLibrary", "UInt", hModule)

  MsgBox %StrOut%



CMD := "ipconfig /all"

  VarSetCapacity(StrOut, 1000)
  hModule := DllCall("LoadLibrary", "str", "cmdret.dll","UInt")
  DllCall("lstrcpyA", "str", StrOut, "int", DllCall("cmdret.dll\RunRedirect", "str", CMD))
  DllCall("FreeLibrary", "UInt", hModule)

  MsgBox %StrOut%


Streaming Example:
Code:
; CMDret DLL version 3.1 (or greater) required

Gui, Add, Edit, x6 y10 w460 h360 +HScroll
Gui, Show, x398 y110 h377 w477, CMDret - DLL version - Streaming Test App

CMD = ping 127.0.0.1
OutputWindow := WinExist("CMDret - DLL version - Streaming Test App")

StrOut := DllCall("cmdret.dll\RunInControl", "str", CMD, "Uint", OutputWindow, "str", "Edit", "int", "1")
Return

GuiClose:
ExitApp


Last edited by corrupt on Sun Dec 14, 2008 10:27 pm; edited 18 times in total
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Sun May 22, 2005 6:58 pm    Post subject: Reply with quote

Same as above but only loads and unloads the dll once for speed:

Code:
; CMDret (DLL version) examples
hModule := DllCall("LoadLibrary", "str", "cmdret.dll","UInt")

CMD := COMSPEC " /C set"
  VarSetCapacity(StrOut, 1000)
  DllCall("lstrcpyA", "str", StrOut, "int", DllCall("cmdret.dll\RunRedirect", "str", CMD))
MsgBox %StrOut%

CMD := "ipconfig /all"
  VarSetCapacity(StrOut, 1000)
  DllCall("lstrcpyA", "str", StrOut, "int", DllCall("cmdret.dll\RunRedirect", "str", CMD))
MsgBox %StrOut%

DllCall("FreeLibrary", "UInt", hModule)
Back to top
View user's profile Send private message Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10667

PostPosted: Mon May 23, 2005 12:22 am    Post subject: Reply with quote

Nice! It's getting better and better and someday soon your work should finally get added as a built-in feature.

Thanks.
Back to top
View user's profile Send private message Send e-mail
Atomhrt



Joined: 02 Sep 2004
Posts: 124
Location: Sunnyvale

PostPosted: Mon May 23, 2005 1:09 am    Post subject: Reply with quote

Very good and very handy indeed! Keep up the good work. Very Happy
_________________
I am he of whom he speaks!
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Mon May 23, 2005 3:28 am    Post subject: Reply with quote

Thanks Smile . Please keep in mind that this is a beta. Any feedback regarding code that works/doesn't work as expected would be greatly appreciated.
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Mon May 23, 2005 3:39 am    Post subject: Reply with quote

Another example:
Code:
; Generated using SmartGUI Creator 3.4

Gui, Add, Edit, x12 y30 w310 h20 -WantReturn vCMD,  ;
Gui, Add, Button, x322 y30 w51 h20 gBrowse1, ...
Gui, Add, Text, x12 y10 w130 h20, Command:
Gui, Add, Text, x12 y90 w130 h20, Output:
Gui, Add, Edit, x12 y110 w410 h280 +HScroll vEditOut,  ;
Gui, Add, Button, x372 y30 w51 h20 Default, Go
Gui, Add, Checkbox, x12 y60 w190 h20 vChk1, Use COMSPEC
Gui, Show, h408 w436, CMDret - DLL version - Test App
Return

Browse1:
FileSelectFile, SelectedFile, 3, , Please select a console program
if SelectedFile
{
  Loop, %SelectedFile%
    Spath = %A_LoopFileShortPath%
  GuiControl,,Edit1,%Spath%
}
Return

buttonGo:
StrOut=
GuiControl, Text, Edit2,  ;
GuiControlGet, CMD,, Edit1
GuiControlGet, Chk1
if Chk1
  CMD := COMSPEC " /C " CMD
if CMD
  IfNotInString, CMD, dir
  {
    VarSetCapacity(StrOut, 1000)
    hModule := DllCall("LoadLibrary", "str", "cmdret.dll","UInt")
    DllCall("lstrcpyA", "str", StrOut, "int", DllCall("cmdret.dll\RunRedirect", "str", CMD))
    GuiControl, Text, Edit2, %StrOut%
    DllCall("FreeLibrary", "UInt", hModule)
  }
  else
  {
    msgbox, Sorry, 'Dir' command not currently allowed
  }
ControlFocus, Edit1
Return

GuiClose:
ExitApp
Back to top
View user's profile Send private message Visit poster's website
JBensimon



Joined: 16 Nov 2004
Posts: 149
Location: New York

PostPosted: Mon May 23, 2005 5:57 pm    Post subject: Thanks. Reply with quote

Looks very promising, corrupt. I'll play with it as soon as I can find the time. In practice, I'd probably wind up "wrapping it" in a user-defined function to keep the setup machinery out of sight.

Later,

Jacques.
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Mon May 23, 2005 6:03 pm    Post subject: Reply with quote

Added a function to allow streaming to a control. This function should be more stable than the command line version.

RunInControl(CMD, WinHandle, CtrlClass, CtrlInstance)
- CMD should be the console program/command to execute (str)
- WinHandle should be the handle to the window where the ouput control exists (Uint)
- CtrlClass should be the ClassName of the output control (str)
- CtrlInstance should be the instance number of the output control (int)
= If the function fails, the return value is zero

Code:
; CMDret DLL version 3.1 (or greater) required

Gui, Add, Edit, x6 y10 w460 h360 +HScroll
Gui, Show, x398 y110 h377 w477, CMDret - DLL version - Streaming Test App

CMD = ping 127.0.0.1
OutputWindow := WinExist("CMDret - DLL version - Streaming Test App")

StrOut := DllCall("cmdret.dll\RunInControl", "str", CMD, "Uint", OutputWindow, "str", "Edit", "int", "1")
Return

GuiClose:
ExitApp


Last edited by corrupt on Mon May 23, 2005 6:13 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Mon May 23, 2005 6:07 pm    Post subject: Reply with quote

Another streaming example
Code:
; CMDret DLL version 3.1 (or greater) required

Gui, Add, Edit, x12 y30 w310 h20 -WantReturn vCMD,  ;
Gui, Add, Button, x322 y30 w51 h20 gBrowse1, ...
Gui, Add, Text, x12 y10 w130 h20, Command:
Gui, Add, Text, x12 y90 w130 h20, Output:
Gui, Add, Edit, x12 y110 w410 h280 +HScroll vEditOut,  ;
Gui, Add, Button, x372 y30 w51 h20 Default, Go
Gui, Add, Checkbox, x12 y60 w190 h20 vChk1, Use COMSPEC
Gui, Show, h408 w436, CMDret - DLL version - Streaming Test App 2
Return

Browse1:
FileSelectFile, SelectedFile, 3, , Please select a console program
if SelectedFile
{
  Loop, %SelectedFile%
    Spath = %A_LoopFileShortPath%
  GuiControl,,Edit1,%Spath%
}
Return

buttonGo:
; Retrieve output window handle
OutputWindow := WinExist("CMDret - DLL version - Streaming Test App 2")
; Clear Edit control & wait for refresh
GuiControl, Text, Edit2, ;
sleep, 250
; Retrieve & process command info
GuiControlGet, CMD,, Edit1
GuiControlGet, Chk1
if Chk1
  CMD := COMSPEC " /C " CMD
if CMD
  IfNotInString, CMD, dir
  {
  ; one line call. loadlibrary not necessary to output to control
  StrOut := DllCall("cmdret.dll\RunInControl", "str", CMD, "Uint", OutputWindow, "str", "Edit", "int", "2")
  }
  else
  {
    msgbox, Sorry, 'Dir' command not currently supported
  }
ControlFocus, Edit1
Return

GuiClose:
ExitApp
Back to top
View user's profile Send private message Visit poster's website
Rajat



Joined: 28 Mar 2004
Posts: 1687

PostPosted: Mon May 23, 2005 6:23 pm    Post subject: Reply with quote

its a nice util, thanx! (just curious) why is dir cmd not supported?
_________________
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Mon May 23, 2005 6:54 pm    Post subject: Reply with quote

Rajat wrote:
its a nice util, thanx! (just curious) why is dir cmd not supported?
Thanks Smile . I haven't currently specified a working directory so the function fails and locks up when trying to do a directory listing. I should have a fix and/or option to allow a directory listing and related commands soon.
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Mon May 23, 2005 7:02 pm    Post subject: Reply with quote

Stream to Notepad:
Code:
; CMDret DLL version 3.1 (or greater) required

Run, Notepad,,, notepid
WinWait, ahk_pid %notepid%
WinWaitActive, ahk_pid %notepid%

CMD := "ping 127.0.0.1"
OutputWindow := WinExist(ahk_pid %notepid%)
if OutputWindow <> 0
  StrOut := DllCall("cmdret.dll\RunInControl", "str", CMD, "Uint", OutputWindow, "str", "Edit", "int", "1")
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Mon May 23, 2005 7:20 pm    Post subject: Reply with quote

corrupt wrote:
Rajat wrote:
its a nice util, thanx! (just curious) why is dir cmd not supported?
Thanks Smile . I haven't currently specified a working directory so the function fails and locks up when trying to do a directory listing. I should have a fix and/or option to allow a directory listing and related commands soon.

However, the following does seem to work ok:
Code:
; CMDret DLL version 3.0 (or greater) required

VarSetCapacity(StrOut, 10000) ; this number may need to be increased if your directory has many, many entries
hModule := DllCall("LoadLibrary", "str", "cmdret.dll","UInt")

CMD := COMSPEC " /C Dir c:\"
DllCall("lstrcpyA", "str", StrOut, "int", DllCall("cmdret.dll\RunRedirect", "str", CMD))
MsgBox, %StrOut%
DllCall("FreeLibrary", "UInt", hModule)


So it might be best to just make a note that a directory must be specified after a 'Dir' command. Opinions welcome...
Back to top
View user's profile Send private message Visit poster's website
Rajat



Joined: 28 Mar 2004
Posts: 1687

PostPosted: Tue May 24, 2005 12:39 am    Post subject: Reply with quote

even better!
_________________
Back to top
View user's profile Send private message
corrupt



Joined: 29 Dec 2004
Posts: 2446

PostPosted: Tue May 24, 2005 7:51 am    Post subject: Reply with quote

Updated to version 3.1.1

- Added another function to the dll (RunReturn) to allow returning output to a variable. This function does not require the additional LoadLibrary, lstrcpyA, and FreeLibrary calls.

RunReturn(CMD, StrOut)
- CMD - the console program/command to execute (str)
- StrOut - the variable used to store the output (str)
= If the function fails, the return value is zero

Code:
; CMDret DLL version 3.1.1 (or greater) required

ret1 := CMDret(COMSPEC " /C set")
MsgBox, %ret1%

CMDret(CMD)
{
  VarSetCapacity(StrOut, 10000)
  RetVal := DllCall("cmdret.dll\RunReturn", "str", CMD, "str", StrOut)
  Return, %StrOut%
}
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Utilities & Resources All times are GMT
Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
Page 1 of 10

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group