VC如何执行*.bat文件?

时间:2022-09-30 12:16:48
我写了程序如下:
try
    { 
CString strCmd;
strCmd = _T("F:\\MFC_MDI1\\OraBin\\debug\\run.bat");        
PROCESS_INFORMATION pInfo;
STARTUPINFO         sInfo;
sInfo.cb              = sizeof(STARTUPINFO);
sInfo.lpReserved      = NULL;
sInfo.lpReserved2     = NULL;
sInfo.cbReserved2     = 0;
sInfo.lpDesktop       = "";
sInfo.lpTitle         = NULL;
sInfo.dwFlags         = 0;
sInfo.dwX             = 0;
sInfo.dwY             = 0;
sInfo.dwFillAttribute = 0;
sInfo.wShowWindow     = SW_SHOW;

BOOL bRet = CreateProcess(NULL,
(LPSTR)(LPCTSTR)strCmd,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&sInfo,
&pInfo);
        
        if(bRet)
        {
            CloseHandle(pInfo.hThread);
            CloseHandle(pInfo.hProcess);
        }

}
catch(...)
{
}
run.bat中的内容如下:
java -jar Security.jar 2 8 1
run.bat可以在DOS下执行,我试过。
问题是我运行程序时,界面只闪了一下,并没有执行run.bat,为什么?怎样才能执行run.bat,请指教。

9 个解决方案

#1


WinExec(_T("F:\\MFC_MDI1\\OraBin\\debug\\run.bat"),SW_SHOW);

#2


不太懂得你的要求.
先用ShellExecute(this->GetSafeHwnd(),_T("Open"),_T("F:\\MFC_MDI1\\OraBin\\debug\\run.bat"),NULL,NULL,SW_SHOW );顶一下吧

#3


ShellExecute

#4


ShellExecute
最简单了。

#5


ShellExecute

#6


可以使用_spawn(),_exec(),system(),WinExec(),ShellExecute(),ShellExecuteEx(),CreateProcess.

system()可移植性最好.
WinExec()、ShellExecute(),ShellExecuteEx和CreateProcess()是Windows特有的函数。后面这四个函数允许你在后台加载应用

WinExec()是最容易使用的一个,只有两个参数。但对于32位程序,微软建议使用CreateProcess,而不是WinExec()

ShellExecute()比CreateProcess更容易使用,且比WinExec()有更多的选项。但是,它也是一个遗留下来的函数,对于Win32应用,微软建议使用ShellExecuteEx().

#7


学习

#8


//执行批处理
STARTUPINFO StartupInfo;
PROCESS_INFORMATION pro_info;
GetStartupInfo(&StartupInfo);
StartupInfo.lpReserved=NULL;
StartupInfo.lpDesktop=NULL;
StartupInfo.lpTitle=NULL;
StartupInfo.dwX=0;
StartupInfo.dwY=0;
StartupInfo.dwXSize=200;
StartupInfo.dwYSize=300;
StartupInfo.dwXCountChars=500;
StartupInfo.dwYCountChars=500;
StartupInfo.dwFlags=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow=SW_HIDE; //后台执行
StartupInfo.cbReserved2=0;
StartupInfo.lpReserved2=NULL;
StartupInfo.hStdInput=stdin;
StartupInfo.hStdOutput=stdout;
StartupInfo.hStdError=stderr;
LPTSTR lpBuffer;
UINT uSize;
HANDLE hHeap;

uSize=(GetCurrentDirectory(0,NULL))*sizeof(TCHAR);
hHeap=GetProcessHeap();
lpBuffer=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,uSize);
GetCurrentDirectory(uSize,lpBuffer);

SetCurrentDirectory(srcDir);

if (CreateProcess(strBatFilePath,NULL,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&StartupInfo,&pro_info)) 
{
MSG Message;
SetTimer(1,100,NULL);
BOOL Search = TRUE;
while(Search) {
if (::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) {
::TranslateMessage(&Message);
::DispatchMessage(&Message);
}
}

DWORD ExitCode;
if (!GetExitCodeProcess(pro_info.hProcess,&ExitCode))
AfxMessageBox("GetExitCodeProcess is Failed!");

if (!TerminateProcess(pro_info.hProcess,(UINT)ExitCode))
AfxMessageBox("TerminateProcess is Failed!");

if (!CloseHandle(pro_info.hProcess))
AfxMessageBox("CloseHandle is Failed!");

KillTimer(1);
}
else AfxMessageBox("Process Is Not Created!");

SetCurrentDirectory(lpBuffer);

#9


当然,最方便的还是system了,呵呵。
考虑一个删除文件夹的操作:system("rd /s /q path");
搞定。

#1


WinExec(_T("F:\\MFC_MDI1\\OraBin\\debug\\run.bat"),SW_SHOW);

#2


不太懂得你的要求.
先用ShellExecute(this->GetSafeHwnd(),_T("Open"),_T("F:\\MFC_MDI1\\OraBin\\debug\\run.bat"),NULL,NULL,SW_SHOW );顶一下吧

#3


ShellExecute

#4


ShellExecute
最简单了。

#5


ShellExecute

#6


可以使用_spawn(),_exec(),system(),WinExec(),ShellExecute(),ShellExecuteEx(),CreateProcess.

system()可移植性最好.
WinExec()、ShellExecute(),ShellExecuteEx和CreateProcess()是Windows特有的函数。后面这四个函数允许你在后台加载应用

WinExec()是最容易使用的一个,只有两个参数。但对于32位程序,微软建议使用CreateProcess,而不是WinExec()

ShellExecute()比CreateProcess更容易使用,且比WinExec()有更多的选项。但是,它也是一个遗留下来的函数,对于Win32应用,微软建议使用ShellExecuteEx().

#7


学习

#8


//执行批处理
STARTUPINFO StartupInfo;
PROCESS_INFORMATION pro_info;
GetStartupInfo(&StartupInfo);
StartupInfo.lpReserved=NULL;
StartupInfo.lpDesktop=NULL;
StartupInfo.lpTitle=NULL;
StartupInfo.dwX=0;
StartupInfo.dwY=0;
StartupInfo.dwXSize=200;
StartupInfo.dwYSize=300;
StartupInfo.dwXCountChars=500;
StartupInfo.dwYCountChars=500;
StartupInfo.dwFlags=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow=SW_HIDE; //后台执行
StartupInfo.cbReserved2=0;
StartupInfo.lpReserved2=NULL;
StartupInfo.hStdInput=stdin;
StartupInfo.hStdOutput=stdout;
StartupInfo.hStdError=stderr;
LPTSTR lpBuffer;
UINT uSize;
HANDLE hHeap;

uSize=(GetCurrentDirectory(0,NULL))*sizeof(TCHAR);
hHeap=GetProcessHeap();
lpBuffer=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,uSize);
GetCurrentDirectory(uSize,lpBuffer);

SetCurrentDirectory(srcDir);

if (CreateProcess(strBatFilePath,NULL,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&StartupInfo,&pro_info)) 
{
MSG Message;
SetTimer(1,100,NULL);
BOOL Search = TRUE;
while(Search) {
if (::PeekMessage(&Message,NULL,0,0,PM_REMOVE)) {
::TranslateMessage(&Message);
::DispatchMessage(&Message);
}
}

DWORD ExitCode;
if (!GetExitCodeProcess(pro_info.hProcess,&ExitCode))
AfxMessageBox("GetExitCodeProcess is Failed!");

if (!TerminateProcess(pro_info.hProcess,(UINT)ExitCode))
AfxMessageBox("TerminateProcess is Failed!");

if (!CloseHandle(pro_info.hProcess))
AfxMessageBox("CloseHandle is Failed!");

KillTimer(1);
}
else AfxMessageBox("Process Is Not Created!");

SetCurrentDirectory(lpBuffer);

#9


当然,最方便的还是system了,呵呵。
考虑一个删除文件夹的操作:system("rd /s /q path");
搞定。