VC中执行命令行程序 各位大侠:请问在VC代码中怎样执行象在Windows 的“开始”->“run”-> "tux.exe"的效果啊?可以传递命令行参数吗?先谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看下这个 转贴的喔winexec(Pchar('StrCommand'),sw_Show);其中"StrCommand"代表以下命令之一(使用Windows中的运行不要加引号):"rundll32 shell32,Control_RunDLL" - 运行控制面板"rundll32 shell32,OpenAs_RunDLL" - 打开"打开方式"窗口"rundll32 shell32,ShellAboutA Info-Box" - 打开"关于"窗口"rundll32 shell32,Control_RunDLL desk.cpl" - 打开"显示属性"窗口"rundll32 user,cascadechildwindows" - 层叠全部窗口"rundll32 user,tilechildwindows" - 最小化所有的子窗口"rundll32 user,repaintscreen" - 刷新桌面"rundll32 shell,shellexecute Explorer" - 重新运行Windows Explorer"rundll32 keyboard,disable" - 锁写键盘"rundll32 mouse,disable" - 让鼠标失效"rundll32 user,swapmousebutton" - 交换鼠标按钮"rundll32 user,setcursorpos" - 设置鼠标位置为(0,0)"rundll32 user,wnetconnectdialog" - 打开"映射网络驱动器"窗口"rundll32 user,wnetdisconnectdialog" - 打开"断开网络驱动器"窗口"rundll32 user,disableoemlayer" - 显示BSOD窗口, (BSOD) = Blue Screen Of Death, 即蓝屏"rundll32 diskcopy,DiskCopyRunDll" - 打开磁盘复制窗口"rundll32 rnaui.dll,RnaWizard" - 运行"Internet连接向导", 如果加上参数"/1"则为silent模式"rundll32 shell32,SHFormatDrive" - 打开"格式化磁盘(A)"窗口"rundll32 shell32,SHExitWindowsEx -1" - 冷启动Windows Explorer"rundll32 shell32,SHExitWindowsEx 1" - 关机"rundll32 shell32,SHExitWindowsEx 0" - 退当前用户"rundll32 shell32,SHExitWindowsEx 2" Windows9x 快速重启"rundll32 krnl386.exe,exitkernel" - 强行退出Windows 9x(无确认)"rundll rnaui.dll,RnaDial "MyConnect" - 运行"网络连接"对话框"rundll32 msprint2.dll,RUNDLL_PrintTestPage" - 选择打印机和打印测试页"rundll32 user,setcaretblinktime" - 设置光标闪烁速度"rundll32 user, setdoubleclicktime" - 测试鼠标双击速度"rundll32 sysdm.cpl,InstallDevice_Rundll" - 搜索非PnP设备 控制面板中的各项功能 {控制面板程序} winexec('rundll32.exe shell32.dll, Control_RunDLL', 9); {辅助选项 属性-键盘} winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 1', 9); {辅助选项 属性-声音} winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 2', 9); {辅助选项 属性-显示} winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 3', 9); {辅助选项 属性-鼠标} winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 4', 9); {辅助选项 属性-常规} winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 5', 9); {添加/删除程序 属性-安装/卸载} winexec('rundll32.exe shell32.dll, Control_RunDLL Appwiz.cpl, 1', 9); {添加/删除程序 属性-Windows安装程序} winexec('rundll32.exe shell32.dll, Control_RunDLL Appwiz.cpl, 2', 9); {添加/删除程序 属性-启动盘} winexec('rundll32.exe shell32.dll, Control_RunDLL Appwiz.cpl, 3', 9); {显示 属性-背景} winexec('rundll32.exe shell32.dll, Control_RunDLL desk.cpl, 0', 9); {显示 属性-屏幕保护程序} winexec('rundll32.exe shell32.dll, Control_RunDLL desk.cpl, 1', 9); {显示 属性-外观} winexec('rundll32.exe shell32.dll, Control_RunDLL desk.cpl, 2', 9); {显示 属性-设置} winexec('rundll32.exe shell32.dll, Control_RunDLL desk.cpl, 3', 9); {Internet 属性-常规} winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 0', 9); {Internet 属性-安全} winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 1', 9); {Internet 属性-内容} winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 2', 9); {Internet 属性-连接} winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 3', 9); {Internet 属性-程序} winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 4', 9); {Internet 属性-高级} winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 5', 9); {区域设置 属性-区域设置} winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 0', 9); {区域设置 属性-数字} winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 1', 9); {区域设置 属性-货币} winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 2', 9); {区域设置 属性-时间} winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 3', 9); {区域设置 属性-日期} winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 4', 9); {游戏控制器-一般} winexec('rundll32.exe shell32.dll, Control_RunDLL Joy.cpl, 0', 9); {游戏控制器-高级} winexec('rundll32.exe shell32.dll, Control_RunDLL Joy.cpl, 1', 9); {鼠标 属性} winexec('rundll32.exe shell32.dll, Control_RunDLL Main.cpl', 9); {多媒体 属性-音频} winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 0', 9); {多媒体 属性-视频} winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 1', 9); {多媒体 属性-MIDI} winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 2', 9); {多媒体 属性-CD音乐} winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 3', 9); {多媒体 属性-设备} winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 4', 9); {调制解调器 属性} winexec('rundll32.exe shell32.dll, Control_RunDLL Modem.cpl', 9); {网络} winexec('rundll32.exe shell32.dll, Control_RunDLL Netcpl.cpl', 9); {密码 属性} winexec('rundll32.exe shell32.dll, Control_RunDLL Password.cpl', 9); {扫描仪与数字相机 属性} winexec('rundll32.exe shell32.dll, Control_RunDLL Sticpl.cpl', 9); {系统 属性-常规} winexec('rundll32.exe shell32.dll, Control_RunDLL Sysdm.cpl, 0', 9); {系统 属性-设备管理器} winexec('rundll32.exe shell32.dll, Control_RunDLL Sysdm.cpl, 1', 9); {系统 属性-硬件配置文件} winexec('rundll32.exe shell32.dll, Control_RunDLL Sysdm.cpl, 2', 9); {系统 属性-性能} winexec('rundll32.exe shell32.dll, Control_RunDLL Sysdm.cpl, 3', 9); {日期/时间 属性} winexec('rundll32.exe shell32.dll, Control_RunDLL timedate.cpl', 9); {电源管理 属性} winexec('rundll32.exe shell32.dll, Control_RunDLL Powercfg.cpl', 9); {拨号属性} winexec('rundll32.exe shell32.dll, Control_RunDLL Telephon.cpl', 9); 关于调用后的判断处理建议: 先声明一个cardinal类型的变量RtnCardinal获取返回值进行判断如: RtnCardinal := winexec('rundll32.exe shell32.dll, Control_RunDLL Telephon.cpl', 9); 返回值 可能原因 0 程序超出内存 ERROR_BAD_FORMAT 程序为一个非法的Win32.EXE程序 ERROR_FILE_NOT_FOUND 指定文件没找到 ERROR_PATH_NOT_FOUND 指定路径没找到 这个: IShellDispatch* pShellDispatch = NULL; HRESULT hResult = CoInitialize(NULL); if(FAILED(hResult)) { return; } hResult = CoCreateInstance(CLSID_Shell,NULL,CLSCTX_INPROC_SERVER, IID_IDispatch,(LPVOID*)&pShellDispatch); if(FAILED(hResult)) { return; } hResult = pShellDispatch->FileRun();就可以了 extern char *applname; extern char *progname; HANDLE t1; char buf[1024]; BOOL b; #define errno GetLastError() sprintf(buf, "%s-child", applname); buf[sizeof(buf)-1] = 0; STARTUPINFO startInfo; PROCESS_INFORMATION pidInfo; // // child process // startInfo.cb = sizeof(STARTUPINFO); startInfo.lpReserved = NULL; startInfo.lpTitle = buf; startInfo.lpDesktop = NULL; startInfo.dwX = 0; startInfo.dwY = 0; startInfo.dwXSize = 0; startInfo.dwYSize = 0; startInfo.dwXCountChars = 0; startInfo.dwYCountChars = 0; startInfo.dwFlags = STARTF_USESTDHANDLES; startInfo.wShowWindow = 0; //SW_SHOWDEFAULT; startInfo.lpReserved2 = NULL; startInfo.cbReserved2 = 0; startInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); startInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); startInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); b = CreateProcess( progname, buf, NULL, NULL, TRUE, 0,//CREATE_NEW_CONSOLE, NULL, NULL, &startInfo, &pidInfo); if(!b) { printf("Creation of child process failed: err=%d\n", errno); printf("applname=<%s>, buf=<%s>\n",applname,buf); return; } t1 = pidInfo.hProcess; 多谢三位,三种方法都特别启发,可是要完成我的目的还有点问题:第一种是调用windows系统调用接口,我试过了,要执行自己的exe是可以的,可是不能输入初始参数。第二种是COM接口,我没有试,FileRun()就是弹出run窗口是吗,如果我不想手工输入要执行的程序可以吗?第三种的建立进程我就是不知道怎样传递一个初始参数进去供exe中使用。CreateProcess( LPCWSTR lpszImageName, LPCWSTR lpszCmdLine, ...)怎样传参?我在第二个参数中传进去,但是程序不认。 你可以用ShellExecute来做系统调用HINSTANCE ShellExecute( HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile, LPCTSTR lpParameters, // 初始化参数 LPCTSTR lpDirectory, INT nShowCmd); ::ShellExecute(NULL, "OPEN", "C:\\tux.exe", "argument", NULL, SW_SHOW);HINSTANCE ShellExecute( HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile, //应用程序绝对路径 LPCTSTR lpParameters, //参数 LPCTSTR lpDirectory, INT nShowCmd); CreateProcess( LPCWSTR lpszImageName, LPCWSTR lpszCmdLine, ...)第二个lpszCmdLine别是把执行文件和参数当成字符串放在一起的如"CMD.EXE DIR /B C:\WINDOWS" WinExec("notepad.exe c:\新建文件夹.txt",SW_SHOW); 十分感谢!那就结帖了,本来我还有个问题,但是这样太委屈大家了。我将开新帖“VC中EXE的初始化参数”,是本帖的延续, 本帖分数太少,大家回复就有分! COM中出现0x00000000地址错误 请教:使用jrtplib 进行一对多实时接收文件的解决方案. VC2005环境下使用ADO的问题 谁能解释 Visual assist X居然和 "2005"冲突? 关于管道的问题 用afxMessageBOX显示的内容怎么用对话框显示 昨天的问题没弄懂 关于typedef 禁止屏幕保护的问题(WM_SYSCOMMAND) 如何将内存中一个结构体的内容复制到内存中另一片相同的结构体中? vs7装不上。 机器有多个网卡,怎么提取ip 又一个关于指针的问题!兄弟们来帮忙!
winexec(Pchar('StrCommand'),sw_Show);
其中"StrCommand"代表以下命令之一(使用Windows中的运行不要加引号):
"rundll32 shell32,Control_RunDLL" - 运行控制面板
"rundll32 shell32,OpenAs_RunDLL" - 打开"打开方式"窗口
"rundll32 shell32,ShellAboutA Info-Box" - 打开"关于"窗口
"rundll32 shell32,Control_RunDLL desk.cpl" - 打开"显示属性"窗口
"rundll32 user,cascadechildwindows" - 层叠全部窗口
"rundll32 user,tilechildwindows" - 最小化所有的子窗口
"rundll32 user,repaintscreen" - 刷新桌面
"rundll32 shell,shellexecute Explorer" - 重新运行Windows Explorer
"rundll32 keyboard,disable" - 锁写键盘
"rundll32 mouse,disable" - 让鼠标失效
"rundll32 user,swapmousebutton" - 交换鼠标按钮
"rundll32 user,setcursorpos" - 设置鼠标位置为(0,0)
"rundll32 user,wnetconnectdialog" - 打开"映射网络驱动器"窗口
"rundll32 user,wnetdisconnectdialog" - 打开"断开网络驱动器"窗口
"rundll32 user,disableoemlayer" - 显示BSOD窗口, (BSOD) = Blue Screen Of Death, 即蓝屏
"rundll32 diskcopy,DiskCopyRunDll" - 打开磁盘复制窗口
"rundll32 rnaui.dll,RnaWizard" - 运行"Internet连接向导", 如果加上参数"/1"则为silent模式
"rundll32 shell32,SHFormatDrive" - 打开"格式化磁盘(A)"窗口
"rundll32 shell32,SHExitWindowsEx -1" - 冷启动Windows Explorer
"rundll32 shell32,SHExitWindowsEx 1" - 关机
"rundll32 shell32,SHExitWindowsEx 0" - 退当前用户
"rundll32 shell32,SHExitWindowsEx 2" Windows9x 快速重启
"rundll32 krnl386.exe,exitkernel" - 强行退出Windows 9x(无确认)
"rundll rnaui.dll,RnaDial "MyConnect" - 运行"网络连接"对话框
"rundll32 msprint2.dll,RUNDLL_PrintTestPage" - 选择打印机和打印测试页
"rundll32 user,setcaretblinktime" - 设置光标闪烁速度
"rundll32 user, setdoubleclicktime" - 测试鼠标双击速度
"rundll32 sysdm.cpl,InstallDevice_Rundll" - 搜索非PnP设备
控制面板中的各项功能
{控制面板程序}
winexec('rundll32.exe shell32.dll, Control_RunDLL', 9);
{辅助选项 属性-键盘}
winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 1', 9);
{辅助选项 属性-声音}
winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 2', 9);
{辅助选项 属性-显示}
winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 3', 9);
{辅助选项 属性-鼠标}
winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 4', 9);
{辅助选项 属性-常规}
winexec('rundll32.exe shell32.dll, Control_RunDLL access.cpl, 5', 9);
{添加/删除程序 属性-安装/卸载}
winexec('rundll32.exe shell32.dll, Control_RunDLL Appwiz.cpl, 1', 9);
{添加/删除程序 属性-Windows安装程序}
winexec('rundll32.exe shell32.dll, Control_RunDLL Appwiz.cpl, 2', 9);
{添加/删除程序 属性-启动盘}
winexec('rundll32.exe shell32.dll, Control_RunDLL Appwiz.cpl, 3', 9);
{显示 属性-背景}
winexec('rundll32.exe shell32.dll, Control_RunDLL desk.cpl, 0', 9);
{显示 属性-屏幕保护程序}
winexec('rundll32.exe shell32.dll, Control_RunDLL desk.cpl, 1', 9);
{显示 属性-外观}
winexec('rundll32.exe shell32.dll, Control_RunDLL desk.cpl, 2', 9);
{显示 属性-设置}
winexec('rundll32.exe shell32.dll, Control_RunDLL desk.cpl, 3', 9);
{Internet 属性-常规}
winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 0', 9);
{Internet 属性-安全}
winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 1', 9);
{Internet 属性-内容}
winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 2', 9);
{Internet 属性-连接}
winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 3', 9);
{Internet 属性-程序}
winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 4', 9);
{Internet 属性-高级}
winexec('rundll32.exe shell32.dll, Control_RunDLL Inetcpl.cpl, 5', 9);
{区域设置 属性-区域设置}
winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 0', 9);
{区域设置 属性-数字}
winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 1', 9);
{区域设置 属性-货币}
winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 2', 9);
{区域设置 属性-时间}
winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 3', 9);
{区域设置 属性-日期}
winexec('rundll32.exe shell32.dll, Control_RunDLL Intl.cpl, 4', 9);
{游戏控制器-一般}
winexec('rundll32.exe shell32.dll, Control_RunDLL Joy.cpl, 0', 9);
{游戏控制器-高级}
winexec('rundll32.exe shell32.dll, Control_RunDLL Joy.cpl, 1', 9);
{鼠标 属性}
winexec('rundll32.exe shell32.dll, Control_RunDLL Main.cpl', 9);
{多媒体 属性-音频}
winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 0', 9);
{多媒体 属性-视频}
winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 1', 9);
{多媒体 属性-MIDI}
winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 2', 9);
{多媒体 属性-CD音乐}
winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 3', 9);
{多媒体 属性-设备}
winexec('rundll32.exe shell32.dll, Control_RunDLL Mmsys.cpl, 4', 9);
{调制解调器 属性}
winexec('rundll32.exe shell32.dll, Control_RunDLL Modem.cpl', 9);
{网络}
winexec('rundll32.exe shell32.dll, Control_RunDLL Netcpl.cpl', 9);
{密码 属性}
winexec('rundll32.exe shell32.dll, Control_RunDLL Password.cpl', 9);
{扫描仪与数字相机 属性}
winexec('rundll32.exe shell32.dll, Control_RunDLL Sticpl.cpl', 9);
{系统 属性-常规}
winexec('rundll32.exe shell32.dll, Control_RunDLL Sysdm.cpl, 0', 9);
{系统 属性-设备管理器}
winexec('rundll32.exe shell32.dll, Control_RunDLL Sysdm.cpl, 1', 9);
{系统 属性-硬件配置文件}
winexec('rundll32.exe shell32.dll, Control_RunDLL Sysdm.cpl, 2', 9);
{系统 属性-性能}
winexec('rundll32.exe shell32.dll, Control_RunDLL Sysdm.cpl, 3', 9);
{日期/时间 属性}
winexec('rundll32.exe shell32.dll, Control_RunDLL timedate.cpl', 9);
{电源管理 属性}
winexec('rundll32.exe shell32.dll, Control_RunDLL Powercfg.cpl', 9);
{拨号属性}
winexec('rundll32.exe shell32.dll, Control_RunDLL Telephon.cpl', 9);
关于调用后的判断处理建议:
先声明一个cardinal类型的变量RtnCardinal获取返回值进行判断如:
RtnCardinal := winexec('rundll32.exe shell32.dll, Control_RunDLL Telephon.cpl', 9);
返回值 可能原因
0 程序超出内存
ERROR_BAD_FORMAT 程序为一个非法的Win32.EXE程序
ERROR_FILE_NOT_FOUND 指定文件没找到
ERROR_PATH_NOT_FOUND 指定路径没找到
IShellDispatch* pShellDispatch = NULL;
HRESULT hResult = CoInitialize(NULL);
if(FAILED(hResult))
{
return;
} hResult = CoCreateInstance(CLSID_Shell,NULL,CLSCTX_INPROC_SERVER,
IID_IDispatch,(LPVOID*)&pShellDispatch);
if(FAILED(hResult))
{
return;
} hResult = pShellDispatch->FileRun();
就可以了
extern char *progname;
HANDLE t1;
char buf[1024];
BOOL b;
#define errno GetLastError() sprintf(buf, "%s-child", applname);
buf[sizeof(buf)-1] = 0; STARTUPINFO startInfo;
PROCESS_INFORMATION pidInfo; //
// child process
//
startInfo.cb = sizeof(STARTUPINFO);
startInfo.lpReserved = NULL;
startInfo.lpTitle = buf;
startInfo.lpDesktop = NULL;
startInfo.dwX = 0;
startInfo.dwY = 0;
startInfo.dwXSize = 0;
startInfo.dwYSize = 0;
startInfo.dwXCountChars = 0;
startInfo.dwYCountChars = 0;
startInfo.dwFlags = STARTF_USESTDHANDLES;
startInfo.wShowWindow = 0; //SW_SHOWDEFAULT;
startInfo.lpReserved2 = NULL;
startInfo.cbReserved2 = 0;
startInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
startInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
startInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); b = CreateProcess(
progname,
buf,
NULL,
NULL,
TRUE,
0,//CREATE_NEW_CONSOLE,
NULL,
NULL,
&startInfo,
&pidInfo); if(!b) {
printf("Creation of child process failed: err=%d\n", errno);
printf("applname=<%s>, buf=<%s>\n",applname,buf);
return;
}
t1 = pidInfo.hProcess;
CreateProcess( LPCWSTR lpszImageName, LPCWSTR lpszCmdLine, ...)怎样传参?我在第二个参数中传进去,但是程序不认。
HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters, // 初始化参数
LPCTSTR lpDirectory,
INT nShowCmd
);
HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile, //应用程序绝对路径
LPCTSTR lpParameters, //参数
LPCTSTR lpDirectory,
INT nShowCmd
);
第二个lpszCmdLine别是把执行文件和参数当成字符串放在一起的
如"CMD.EXE DIR /B C:\WINDOWS"