Solved

returning the result of a dos application when finished

Posted on 2003-11-04
22
239 Views
Last Modified: 2010-05-01
hi i use this code to get the result of a dos application but the problem with this is that i cant get all the text back, only the amout that can be cept in a long is returned and i cant seem to make it work as either single or double.

Option Explicit

      Private Declare Function CreatePipe Lib "kernel32" ( _
          phReadPipe As Long, _
          phWritePipe As Long, _
          lpPipeAttributes As Any, _
          ByVal nSize As Long) As Long

      Private Declare Function ReadFile Lib "kernel32" ( _
          ByVal hFile As Long, _
          ByVal lpBuffer As Long, _
          ByVal nNumberOfBytesToRead As Long, _
          lpNumberOfBytesRead As Long, _
          ByVal lpOverlapped As Any) As Long

      Private Type SECURITY_ATTRIBUTES
          nLength As Long
          lpSecurityDescriptor As Long
          bInheritHandle As Long
      End Type

      Private Type STARTUPINFO
         cb As Long
         lpReserved As Long
         lpDesktop As Long
         lpTitle As Long
         dwX As Long
         dwY As Long
         dwXSize As Long
         dwYSize As Long
         dwXCountChars As Long
         dwYCountChars As Long
         dwFillAttribute As Long
         dwFlags As Long
         wShowWindow As Integer
         cbReserved2 As Integer
         lpReserved2 As Long
         hStdInput As Long
         hStdOutput As Long
         hStdError As Long
      End Type

      Private Type PROCESS_INFORMATION
         hProcess As Long
         hThread As Long
         dwProcessID As Long
         dwThreadID As Long
      End Type

      Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
         lpApplicationName As Long, ByVal lpCommandLine As String, _
         lpProcessAttributes As Any, lpThreadAttributes As Any, _
         ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
         ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
         lpStartupInfo As Any, lpProcessInformation As Any) As Long

      Private Declare Function CloseHandle Lib "kernel32" (ByVal _
         hObject As Long) As Long

      Private Const NORMAL_PRIORITY_CLASS = &H20&
      Private Const STARTF_USESTDHANDLES = &H100&

      Private Sub ExecCmd(cmdline$)
          Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long
          Dim start As STARTUPINFO
          Dim sa As SECURITY_ATTRIBUTES, hReadPipe As Long, hWritePipe _
          As Long
          Dim bytesread As Long, mybuff As String
          Dim i As Integer

          mybuff = String(256, Chr$(65))

          sa.nLength = Len(sa)
          sa.bInheritHandle = 1&
          sa.lpSecurityDescriptor = 0&

          ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
          If ret = 0 Then
              MsgBox "CreatePipe failed. Error: " & Err.LastDllError
              Exit Sub
          End If

       start.cb = Len(start)
       start.dwFlags = STARTF_USESTDHANDLES
       start.hStdOutput = hWritePipe

       ' Start the shelled application:
       ret& = CreateProcessA(0&, cmdline$, sa, sa, 1&, _
       NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
       If ret <> 1 Then
           MsgBox "CreateProcess failed. Error: " & Err.LastDllError
       End If

       bSuccess = ReadFile(hReadPipe, mybuff, 100000, bytesread, 0&)
       If bSuccess = 1 Then
           Text1.Text = Left(mybuff, bytesread)
       Else
           MsgBox "ReadFile failed. Error: " & Err.LastDllError
       End If

          ret& = CloseHandle(proc.hProcess)
          ret& = CloseHandle(proc.hThread)
          ret& = CloseHandle(hReadPipe)
          ret& = CloseHandle(hWritePipe)
      End Sub
0
Comment
Question by:Tai-San
22 Comments
 
LVL 2

Author Comment

by:Tai-San
ID: 9681408
ups, made a little mistake

ByVal lpBuffer As Long. _
was supposted to be:
ByVal lpBuffer As String
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9681493
Why don't you send the result to a text file and then have VB read that?

Leon
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9681514
would that do any difference?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9681582
Sure, the problem you are having is passing a memory space reference from DOS to VB.  By dealing with a text file you avoid the entire situation all together.  You can start you app in VB do the the shell command to launch your DOS what ever, then when it is done access the text file, get the required info and delete the file. Much cleaner and less code, I bet.

Leon
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9681602
could you give an example?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9681657
What is the output of your DOS application?
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9681698
its:

Mo'PaQ 2000 build 2210 Copyright (c) 2000 Justin Olbrantz(Quantam)
The official client of the MPQ API Library v2.00 by Andrey Lelikov and Quantam
For the most recent version, go to www.campaigncreations.com/starcraft/mpq2k/

        Usage:
    Add files: mpq2k a <archive> <*src_file> [int_name] [/c] [/wav] [/r] [/new]
Extract files: mpq2k e <archive> <*int_file> [target_dir] [/fp] [/r]
 Rename files: mpq2k r <archive> <int_file> <new_name>
   Move files: mpq2k m <archive> <*src_file> <target_dir> [/r]
 Delete files: mpq2k d <archive> <*int_file> [/r]
Flush archive: mpq2k f <archive>
   List files: mpq2k l <archive> [filter] [out_file] [/lf] [/p]
  Run scripts: mpq2k s <script>
 Command help: mpq2k h <command>
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9681749
The link you specified redirects me to http://www.bestinfo.com/index.html  

Leon
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9681761
dont follow the link
the output of the dos program is excactly as said above including the link (though its not a link in dos)
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9681778
Oh, ok.  Sorry, can you have the DOS program output this to a file?

Leon
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9681808
its not my program, i dont have the source. i am creating a GUI for it but i cant get my code to return enough of the text
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 29

Expert Comment

by:leonstryker
ID: 9681818
I see, does the output normally go the command prompt?

Leon
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9681823
yes it does
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9681841
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9681918
could you create an example out of this cause im having many problems with it
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9681944
Ok, but you will have to wait till tomorrow, since I am on my way home.

Leon
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9682011
okey
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9682366
BTW are you only getting 256 characters?  You only dimension mybuff to 256 characters.
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9682511
i solved that one but i found another problem, the program says that it cant get access to listfile.dat
0
 
LVL 5

Accepted Solution

by:
fantasy1001 earned 75 total points
ID: 9683571
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9687069
i got it to work this far so that the program works with that code ms provided but im having trouple with the batch file
i use this code in the batch file:

mpq2k.exe > c:\LIST.DAT

but it just wont write more than the first lines, then comes a double new line and it doesnt write any further
0
 
LVL 2

Author Comment

by:Tai-San
ID: 9688431
i guess ive got what i need to do now:

1: copy my application to a specific path so i can specify all of it
2: make specific batch file
3: delete all, now, unnecesarry files and copy whats been changed

ill return
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now