如何把VBA语句转化为delphi update语句命令

如何用Delphi运行DOS命令,把结果显示在MEMO里【转】
前几日遇到的问题在各位的帮助下已经圆满解决,现在吧这段代码写出来,可能会有一点用处。
procedure & CheckResult(b: &
& & if & not
& b & then
& & Raise &
Exception.Create(SysErrorMessage(GetLastError));
function & RunDOS(const & Prog,
& CommandLine,Dir: & Svar
& ExitCode:DWORD): &
& & HRead,HWrite:TH
& & StartInfo:TStartupI
ProceInfo:TProcessI
& & sa:TSecurityA
& & inS:THandleS
& & sRet:TS
& & Result & :=
FillChar(sa,sizeof(sa),0);
//设置允许继承,否则在NT和2000下无法取得输出结果
& & sa.nLength &
:= & sizeof(sa);
& & sa.bInheritHandle
& & sa.lpSecurityDescriptor
& & b & :=
& CreatePipe(HRead,HWrite,@sa,0);
& & CheckResult(b);
FillChar(StartInfo,SizeOf(StartInfo),0);
& & StartInfo.cb
& := & SizeOf(StartInfo);
& & StartInfo.wShowWindow
& := & SW_HIDE;
//使用指定的句柄作为标准输入输出的文件句柄,使用指定的显示方式
& & StartInfo.dwFlags
STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW;
& & StartInfo.hStdError
& & StartInfo.hStdInput
& GetStdHandle(STD_INPUT_HANDLE);//HR
& & StartInfo.hStdOutput
& & b & :=
& CreateProcess(PChar(Prog),//lpApplicationName:
& PChar & &
PChar(CommandLine), & &
& & //lpCommandLine:
& & & nil,
& //lpProcessAttributes: &
PSecurityAttributes
& & & nil,
& //lpThreadAttributes: &
PSecurityAttributes
& & & True,
& //bInheritHandles: & BOOL
CREATE_NEW_CONSOLE,
& & & nil,
PChar(Dir),
StartInfo,
& & & ProceInfo
& & CheckResult(b);
WaitForSingleObject(ProceInfo.hProcess,INFINITE);
GetExitCodeProcess(ProceInfo.hProcess,ExitCode);
& & inS & :=
& THandleStream.Create(HRead);
inS.Size& 0 & then
& sRet & := &
TStringList.C
& sRet.LoadFromStream(inS);
& Result & := &
& & CloseHandle(HRead);
& & CloseHandle(HWrite);
*******************
function & GetDosOutput(const &
CommandLine:string): &
TSecurityA
& SI: & TStartupI
& StdOutPipeRead, &
StdOutPipeWrite: & TH
& WasOK: & B
& Buffer: & array[0..255]
& BytesRead: & C
& WorkDir, & Line:
& Application.ProcessM
& with & SA &
& & & nLength
& := & SizeOf(SA);
bInheritHandle & := & T
lpSecurityDescriptor & := &
& // & create &
pipe & for & standard
& output & redirection
& CreatePipe(StdOutPipeRead, &
& // & read &
StdOutPipeWrite, & // & write
& & & @SA,
& // & security
& attributes
& // & number &
of & bytes & reserved
& for & pipe & -
& 0 & default
& Make & child &
process & use & StdOutPipeWrite
& as & standard
& and & make &
sure & it & does
& not & show &
on & screen.
& & & with
& & FillChar(SI,
& SizeOf(SI), & 0);
& & cb & :=
& SizeOf(SI);
& & dwFlags & :=
& STARTF_USESHOWWINDOW & or
& STARTF_USESTDHANDLES;
& & wShowWindow
& := & SW_HIDE;
& & hStdInput &
:= & GetStdHandle(STD_INPUT_HANDLE);
& // & don 't &
redirect & stdinput
& & hStdOutput &
:= & StdOutPipeW
& & hStdError &
:= & StdOutPipeW
& launch & the &
command & line & compiler
& & & WorkDir
ExtractFilePath(CommandLine);
& & & WasOK
& := & CreateProcess(nil,
& PChar(CommandLine), & nil,
& nil, & True, &
& & PChar(WorkDir),
& SI, & PI);
& Now & that &
the & handle & has
& been & inherited,
& close & write
& to & be &
& We & don 't &
want & to & read
& or & write &
to & it & accidentally.
CloseHandle(StdOutPipeWrite);
& if & process &
could & be & created
& then & handle
& its & output
& not & WasOK &
& & raise &
Exception.Create( 'Could & not &
execute & command & line!
& & & else
& // & get & all
& output & until
& dos & app &
& Line & := & '
& read & block &
of & characters & (might
& contain & carriage
& returns & and
& line & feeds)
& & & WasOK
& := & ReadFile(StdOutPipeRead,
& Buffer, & 255,
& BytesRead, & nil);
& has & anything
& been & read?
& BytesRead & &
& 0 & then
& & // & finish
& buffer & to &
& & Buffer[BytesRead]
& := & #0;
& & // & combine
& the & buffer &
with & the & rest
& of & the &
last & run
& & Line & :=
& Line & + &
& until & not &
WasOK & or & (BytesRead
& // & wait &
for & console & app
& to & finish &
(should & be & already
& at & this &
& WaitForSingleObject(PI.hProcess,
& INFINITE);
& & finally
& // & Close &
all & remaining & handles
& CloseHandle(PI.hThread);
& CloseHandle(PI.hProcess);
& & result:=L
CloseHandle(StdOutPipeRead);
**************
unit & Unit1;
& & Windows, &
Messages, & SysUtils, & Classes,
& Graphics, & Controls,
& Forms, & Dialogs,
& & TForm1 & =
& class(TForm)
& Memo1: & TM
& OpenDialog1: &
& btnRUn: & TB
& btnOpenFIle: & TB
& btnEditFile: & TB
& editfilename: & TE
& procedure &
btnOpenfileClick(Sender: & TObject);
& procedure & btnRunClick(Sender:
& TObject);
& & private
& { & Private &
declarations & }
& & public
& { & Public &
declarations & }
& & Form1: &
implementation
{$R & *.DFM}
procedure & TForm1.btnOpenfileClick(Sender:
& TObject);
opendialog1.Execute & then &
editfilename.Text & := &
opendialog1.FileN
procedure & TForm1.btnRunClick(Sender:
& TObject);
& & hReadPipe, &
hWritePipe: & TH
STARTUPINFO;
& & lsa: &
SECURITY_ATTRIBUTES;
PROCESS_INFORMATION;
& & mDosScreen:
& & cchReadBuffer:
& & fname: &
& & i, & j:
& & fname & :=
& allocmem(255);
& & ph & :=
& AllocMem(5000);
& & lsa.nLength
sizeof(SECURITY_ATTRIBUTES);
& & lsa.lpSecurityDescriptor
& & lsa.bInheritHandle
CreatePipe(hReadPipe, & hWritePipe,
& @lsa, & 0) & =
& false & then
& ShowMessage( 'Can & not
& create & pipe! ');
& & fillchar(si,
& sizeof(STARTUPINFO), &
& & si.cb & :=
& sizeof(STARTUPINFO);
& & si.dwFlags &
:= & (STARTF_USESTDHANDLES & or
& STARTF_USESHOWWINDOW);
& & si.wShowWindow
& := & SW_SHOW;
& & si.hStdOutput
& := & hWriteP
& & StrPCopy(fname,
& EditFilename.text);
CreateProcess(nil, & fname, &
nil, & nil, & true,
& 0, & nil, &
nil, & si, & pi)
& = & False &
& ShowMessage( 'can & not
& create & process ');
& FreeMem(ph);
& FreeMem(fname);
& & while &
(true) & do
& if & not &
PeekNamedPipe(hReadPipe, & ph, &
1, & @cchReadBuffer, & nil,
& nil) & then &
& if & cchReadBuffer
& 0 & then
& ReadFile(hReadPipe, & ph^,
& 4096, & cchReadBuffer,
& nil) & = &
false & then &
ph[cchReadbuffer] & := &
Memo1.Lines.Add(ph);
& else & if &
(WaitForSingleObject(pi.hProcess, & 0)
& = & WAIT_OBJECT_0)
& Sleep(100);
& & ph[cchReadBuffer]
& := & chr(0);
& & Memo1.Lines.Add(ph);
& & CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);
& & FreeMem(ph);
& & FreeMem(fname);
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。查看: 10204|回复: 11
如何用vba打开cmd 并执行命令
阅读权限20
在线时间 小时
如何用vba打开cmd并依次运行以下语句:
2. cd work
3. dir /s /q &c:test.txt
在线等,谢谢各位!!!
阅读权限50
在线时间 小时
shell &c:\windows\system32\cmd.exe&
阅读权限50
在线时间 小时
后面的不会,呵呵::L
阅读权限30
在线时间 小时
事先在dos下面写个批处理
然后vba调用cmd先,在调用这个批处理就行了
shell &c:\windows\system32\cmd.exe&
shell &c:\1.bat&
假设这个批处理是1.bat
阅读权限20
在线时间 小时
不能在vba里面一下子做完吗?我知道怎样调出cmd,但不知道怎么输入后面的命令阿
阅读权限20
在线时间 小时
那个批处理怎么写啊?
阅读权限30
在线时间 小时
参考这个帖子:
里面强行拍杀部分使用了FSO生成BAT文件,并用shell调用
如果只是一个指令的cmd,可以用:
Shell &cmd.exe /c&&命令&&&
阅读权限95
在线时间 小时
& & & & & & & &
Shell &c:\windows\system32\cmd.exe&
Application.Wait Now + TimeValue(&0:00:02&)
SendKeys &l:~cd work~ dir /s /q &c:test.txt~&
[ 本帖最后由 cflood 于
21:42 编辑 ]
阅读权限95
在线时间 小时
shell &cmd /c d:\xxx.bat&
阅读权限70
在线时间 小时
shell &cmd.exe /c dir l:\work\*.*/s/q&c:\test.txt&
试一下这个吧
你是想取得L:\work目录下所有文件的所有者列表是吧
[ 本帖最后由 kevinchengcw 于
18:49 编辑 ]
最新热点 /1
人人都要过六一,当当网ExcelHome部分图书4.9折封顶,莫失良机!
玩命加载中,请稍候
玩命加载中,请稍候
Powered by
本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任! & & 本站特聘法律顾问:徐怀玉律师 李志群律师}

我要回帖

更多关于 delphi goto语句 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信