如何在cb中调用外部程序

flingdragon 2002-08-15 02:05:15
我想在cb中,启动一个其他程序,例如:notepad(记事本),不知道该如何操作?
...全文
42 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
钛哥 2002-08-15
  • 打赏
  • 举报
回复
mark
flingdragon 2002-08-15
  • 打赏
  • 举报
回复
谢谢各位大虾!
sjd163 2002-08-15
  • 打赏
  • 举报
回复
收藏
kingcaiyao 2002-08-15
  • 打赏
  • 举报
回复
有三种方法:
1.WinExec("c:\\winnt\\system32\\Notepad.exe",SW_SHOW);
2.ShellExecute(Handle,"Open","c:\\winnt\\system32\\Notepad.exe",NULL,NULL,SW_SHOW);
3.CreatProcess
void __fastcall TForm1::Button1Click(TObject *Sender)
{
STARTUPINFO StartInfo;
PROCESS_INFORMATION ProcessInfo;
StartInfo.cb=sizeof(STARTUPINFO);
StartInfo.lpReserved=0;
StartInfo.lpDesktop=NULL;
StartInfo.lpTitle=NULL;
StartInfo.dwX=0;
StartInfo.dwY=0;
StartInfo.dwXSize=90;
StartInfo.dwYSize=90;
StartInfo.dwXCountChars=0;
StartInfo.dwXCountChars=0;
StartInfo.dwFillAttribute=0;
StartInfo.dwFlags=STARTF_USEPOSITION|STARTF_USESIZE|STARTF_USESHOWWINDOW;
StartInfo.wShowWindow=SW_SHOW;
StartInfo.cbReserved2=0;
StartInfo.lpReserved2=NULL;
StartInfo.hStdInput=0;
StartInfo.hStdOutput=0;
StartInfo.hStdError=0;
if(!::CreateProcess(NULL,(LPCTSTR)"c:\\winnt\\system32\\calc.exe",NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&StartInfo,&ProcessInfo))
{
Application->MessageBox("Failed to execute external program!","Error",MB_OK+MB_ICONERROR);
return;
}

hProcess=ProcessInfo.hProcess;
::WaitForSingleObject(hProcess,INFINITE);
Application->MessageBox("The process has been terminated!","Error",MB_OK+MB_ICONERROR);
}

flingdragon 2002-08-15
  • 打赏
  • 举报
回复
能详细一点吗。初学者..
JamesJiang 2002-08-15
  • 打赏
  • 举报
回复
使用WinExec或者CreateProcess两个API都行。
UINT WinExec(

LPCSTR lpCmdLine, // address of command line
UINT uCmdShow // window style for new application
);
BOOL CreateProcess(

LPCTSTR lpApplicationName, // pointer to name of executable module
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // pointer to process security attributes
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes
BOOL bInheritHandles, // handle inheritance flag
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // pointer to new environment block
LPCTSTR lpCurrentDirectory, // pointer to current directory name
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
);
keenleung 2002-08-15
  • 打赏
  • 举报
回复
API函数ShellExecute

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧