AutoHotkey Community

It is currently May 26th, 2012, 11:20 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 164 posts ]  Go to page 1, 2, 3, 4, 5 ... 11  Next
Author Message
PostPosted: May 22nd, 2005, 7:41 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
Image 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:
Image 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 ;) .

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 December 14th, 2008, 11:27 pm, edited 18 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 22nd, 2005, 7:58 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 1:22 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Nice! It's getting better and better and someday soon your work should finally get added as a built-in feature.

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 2:09 am 
Offline

Joined: September 2nd, 2004, 1:08 am
Posts: 124
Location: Sunnyvale
Very good and very handy indeed! Keep up the good work. :D

_________________
I am he of whom he speaks!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 4:28 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
Thanks :) . Please keep in mind that this is a beta. Any feedback regarding code that works/doesn't work as expected would be greatly appreciated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 4:39 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Thanks.
PostPosted: May 23rd, 2005, 6:57 pm 
Offline

Joined: November 16th, 2004, 6:38 am
Posts: 153
Location: New York
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 7:03 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
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 May 23rd, 2005, 7:13 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 7:07 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 7:23 pm 
Offline

Joined: March 28th, 2004, 3:53 pm
Posts: 1870
its a nice util, thanx! (just curious) why is dir cmd not supported?

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 7:54 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
Rajat wrote:
its a nice util, thanx! (just curious) why is dir cmd not supported?
Thanks :) . 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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 8:02 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
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")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 23rd, 2005, 8:20 pm 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
corrupt wrote:
Rajat wrote:
its a nice util, thanx! (just curious) why is dir cmd not supported?
Thanks :) . 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...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 24th, 2005, 1:39 am 
Offline

Joined: March 28th, 2004, 3:53 pm
Posts: 1870
even better!

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 24th, 2005, 8:51 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2542
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%
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 164 posts ]  Go to page 1, 2, 3, 4, 5 ... 11  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: SKAN and 4 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group