Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the shell Program of VB.NET

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use VB.NET 's shell program, the article is very detailed, has a certain reference value, interested friends must read it!

VB.NET after a long period of development, many users are very familiar with VB.NET, here I would like to publish a personal understanding, and discuss with you. The so-called shell programs in Windows environment are dos command line programs, such as VC's CL.exe command line compiler, JDK's javac compiler, and the java.exe used to start java programs are all standard shell programs. Intercept the output of a shell program is very useful, for example, you can write an IDE (integrated development environment), when the user issues a compilation instruction, you can start the shell call compiler in the background and intercept their output, analyze the output information and display it in a more friendly user interface. For convenience, we use VB as the demonstration language for this article.

Usually, when the system starts the Shell program, it is given three O channels by default, standard input (stdin), standard output stdout, and standard error output stderr. The distinction is made because of some limitations in early computer systems such as PDP-11. There was no GUI at that time, and dividing the output into stdout,stderr prevented the program's debugging information from being mixed with the normal output information. Typically, shell programs write their output to the standard output pipeline (stdout) and error information to the standard error pipeline (stderr). By default, the system sends the pipe output directly to the screen so that we can see the results of the application running. To capture the output of a standard console application, we must redirect the standOutput and standError pipeline output to our custom pipeline.

The following code starts a VB.NET shell program and intercepts its output. Execute and return the standard output and standard error output of a command line program (shell program). Usually all the output of a command line program is sent directly to the screen

PrivateFunctionExecuteApp (sCmdlineAsString) AsStringDimprocAsPROCESS_INFORMATION, retAsLongDimstartAsSTARTUPINFO DimsaAsSECURITY_ATTRIBUTESDimhReadPipeAsLong' is responsible for reading pipe DimhWritePipeAsLong' is responsible for Shell program standard output and standard error output pipeline DimsOutputAsString' puts the returned data DimlngBytesReadAsLong, sBufferAsString*256sa.nLength=Len (sa) sa.bInheritHandle=Trueret=CreatePipe (hReadPipe, hWritePipe,sa,0) Ifret=0ThenMsgBox "CreatePipefailed.Error:" & Err.LastDllErrorExitFunction EndIfstart.cb=Len (start) start.dwFlags=STARTF_USESTDHANDLESOrSTARTF_USESHOWWINDOW' redirects standard output and standard error output to the same pipe. Start.hStdOutput=hWritePipestart.hStdError=hWritePipestart.wShowWindow=SW_HIDE' implied shell program window 'starts the shell program, and sCmdLine indicates the path of execution ret=CreateProcessA (0,000pmdlinejigma sa, True,NORMAL_PRIORITY_CLASS,_0&,0&,start,proc) Ifret=0ThenMsgBox "can not establish a new process, error code:" & Err.LastDllErrorExitFunctionEndIf' in this case does not need to send information to the shell program, so you can first close the hWritePipeCloseHandlehWritePipe' loop to read the output of the shell program, each time read 256 bytes. Doret=ReadFile (hReadPipe,sBuffer,256,lngBytesRead,0&) sOutputsOutput=sOutput&Left$ (sBuffer,lngBytesRead) LoopWhileret0' if ret=0 means no more information needs to be read 'release related resources CloseHandleproc.hProcessCloseHandleproc.hThreadCloseHandlehReadPipeExecuteApp=sOutput' output result EndFunction

I'll give some explanation to this program.

Ret=CreatePipe (hReadPipe,hWritePipe,sa,0)

Then the standard output and standard error output of the VB.NET shell program are directed to our pre-built pipeline.

The code is as follows:

Start.dwFlags=STARTF_USESTDHANDLESOrSTARTF_USESHOWWINDOW start.hStdOutput=hWritePipe start.hStdError=hWritePipe

Okay, now you can call the function that creates the new process:

Ret=CreateProcessA (0,000pr sCmdlinerect Sajre Sajj Truejour NORMALLING PRIORITYCLASS0C 0forth recorder 0nd star proc)

Then, loop through the data in the pipeline until there is no data to read.

Do ret=ReadFile (hReadPipe,sBuffer,256,lngBytesRead,0&) '256byte sOutputsOutput=sOutput&Left$ (sBuffer,lngBytesRead)' is fed into a string LoopWhileret0' if ret=0 indicates that there is no data waiting to be read.

Then, release unused resources.

The use of the VB.NET shell program is simple: for example:

MsgBoxExecuteApp ("c:\ windows\ command\ mem.exe) above is all the content of this article" how to use VB.NET 's shell program ", thank you for reading! hope to share the content is helpful to you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report