在C++ Builder应用程序中执行外部文件(50分)

  • 主题发起人 主题发起人 gtluo
  • 开始时间 开始时间
G

gtluo

Unregistered / Unconfirmed
GUEST, unregistred user!
在这儿WinExec("E://winexec//NTSQL.UDL",SW_SHOW);或者WinExec("NTSQL.UDL",SW_SHOW);都执行不成功,没有任何提示,请问为什么?
 
给你个例子
void __fastcall Tfrm_Main::RunCMD(AnsiString FullCMD)
{
int FBreak;
Screen->Cursor = crHourGlass;
// If NTdo
security stuff
SECURITY_DESCRIPTOR sd;
SECURITY_ATTRIBUTES sa;
LPSECURITY_ATTRIBUTES lpsa = NULL;
if (IsWindowsNT()) {
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, true, NULL, false);
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = &sd;
lpsa = &sa;
}
// Create the Pipe and get r/w handles
HANDLE hReadPipe;
HANDLE hWritePipe;
assert(CreatePipe(&hReadPipe, &hWritePipe, lpsa, 2500000));
// initialize STARTUPINFO struct
STARTUPINFO si;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW |STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hWritePipe;
si.hStdError = hWritePipe;
PROCESS_INFORMATION pi;
assert(hWritePipe);
// Run the cmdLine tool
mmo_Outputs->Lines->Add("Working.....");
Application->ProcessMessages();
if ( CreateProcess(NULL,
FullCMD.c_str(),
NULL,//security
NULL,// security
TRUE,//inherits handles
0,
0,
0,
&si,
&pi)) {
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, 90000);
// read from the pipe and put in memo
assert(hReadPipe);
DWORD BytesRead;
char dest[4000];
bool RdLoopDone = false;
mmo_Outputs->Lines->Clear();
FBreak = 1;
if (ExitCode) {
Screen->Cursor = crDefault;
}
while (!RdLoopDone) {
memset(dest, 0, 4000);
assert(ReadFile(hReadPipe, &dest, sizeof(dest), &BytesRead, NULL));
mmo_Outputs->Lines->Add(String(dest));
if (BytesRead < 4000) RdLoopDone = true;
if (FBreak > 150) RdLoopDone = true;
FBreak++;
}
mmo_Outputs->Lines->Add("Finished!");
} else
{//if CreateProcess
Screen->Cursor = crDefault;
mmo_Outputs->Lines->Add("Finished!");
MessageBox(this->Handle, "Unable to execute program./n/nPlease ensure /"sc.exe/" is in program directory!", "Error", MB_OK | MB_ICONERROR);
}
// Process cleanup
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
CloseHandle(pi.hProcess);
mmo_Outputs->Perform(EM_SETSEL,0,0);
Screen->Cursor = crDefault;
}
 
应该是路径有问题了,你在dos下运行一下E:/winexec/NTSQL.UDL看看,
或者判断winexec的返回值,具体看help吧。
Return Values
If the function succeeds, the return value is greater than 31.
If the function fails, the return value is one of the following error values:
 
我按照以上两位所说的方法去测试了一下,还是不行,我安装的操作系统是Windows2000 profession.请再指教.
 
WinExec("E:/winexec/NTSQL.UDL",SW_SHOW);
試一試這樣
 
WinExec("E://winexec//NTSQL.UDL",SW_SHOW);当然不行了。
它不会根据文件后缀来选择程序打开文件的。你可以用
ShellExecute(Handle,
'Open',
PChar(GetCurrentDir+'/操作演示/'+Name+'.avi'),
nil,
nil,
SW_SHOWNORMAL);
这个函数才可以自动按照后缀选择程序打开。当然,后缀要注册过的。
上面代码是在DELPHI里面实现的。你自己该为C++BUILDER的吧。
 
WinExec() 执行exe, bat.
eg: sprintf(str,"ftp -s:send.
txt");
WinExec(str,SW_SHOW);

ShellExecute(): 执行文件。
 
多人接受答案了。
 
后退
顶部