Solved

returning the result of a dos application when finished

Posted on 2003-11-04
22
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I need help embedding an image as HTML in my vb.net application 3 84
Advice in Xamarin 21 118
Excel Automation VBA 19 115
IF ELSE Statement in Excel Macro VBA 16 98
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 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