如何用MFC打开一个EXE文件?

只要实现一个很简单的功能,例如我在D盘有个游戏名为采金子.exe我现在要用MFC上做一个按钮,只要一点击这个按钮它就会打开这个名为采金子.exe的游戏,请问代码应该如何实... 只要实现一个很简单的功能,例如我在D盘有个游戏名为采金子.exe

我现在要用MFC上做一个按钮,只要一点击这个按钮它就会打开这个名为 采金子.exe 的游戏,请问代码应该如何实现?
展开
 我来答
可以叫我表哥
2015-08-08 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
采纳数:25897 获赞数:1464981
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。

向TA提问 私信TA
展开全部
打开程序用
WinExec("程序路径",SW_SHOW);
关闭自己用exit(0);
关闭别的程序,首先获得程序的PID,然后

int TerminateProcessFromId(DWORD dwId) //关闭进程
{
BOOL bRet=FALSE;

HANDLE hProcess=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwId);
if(hProcess!=NULL)
{
bRet=::TerminateProcess(hProcess,0);
}

::CloseHandle(hProcess);

if(bRet)
printf("%d 进程结束成功......\n\n\n\n",dwId);
else
printf("%d 进程结束失败......\n\n\n\n",dwId);

return 0;
}
dareny
2008-05-25 · TA获得超过505个赞
知道小有建树答主
回答量:287
采纳率:0%
帮助的人:214万
展开全部
调用这个函数就可以:
UINT WinExec(
LPCSTR lpCmdLine, // 输入你要打开的EXE文件路径
UINT uCmdShow // 可以选择SW_SHOW 或SW_HIDE
);
具体实现:
--------------------------------------------
WinExec("D:\采金子.exe",SW_HIDE);
--------------------------------------------如果你要打开文本文件或是打开一个网络的链接 你可以调用 ShellExecute函数 用法参考MSDN。
最后祝你成功!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
blindwood
2008-05-25 · 贡献了超过108个回答
知道答主
回答量:108
采纳率:0%
帮助的人:46.4万
展开全部
TCHAR szCmdLine[512];
HANDLE hProcessOrig = OpenProcess(SYNCHRONIZE, TRUE, GetCurrentProcessId());
wsprintf(szCmdLine, __TEXT("%s %d"), "c:\采金子.exe", hProcessOrig);
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
if( CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi) )
{
}
else
{
}

CloseHandle(hProcessOrig);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lzy9668
2008-05-25 · TA获得超过1319个赞
知道小有建树答主
回答量:485
采纳率:100%
帮助的人:0
展开全部
ShellExecute( NULL,"open", "D:\采金子.exe ",NULL,NULL,SW_SHOWNORMAL);
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
四罐菠菜
2008-05-25 · TA获得超过488个赞
知道答主
回答量:261
采纳率:0%
帮助的人:187万
展开全部
WinExec
The WinExec function runs the specified application.

Note This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.

UINT WinExec(
LPCSTR lpCmdLine, // command line
UINT uCmdShow // window style
);
Parameters
lpCmdLine
[in] Pointer to a null-terminated character string that contains the command line (file name plus optional parameters) for the application to be executed. If the name of the executable file in the lpCmdLine parameter does not contain a directory path, the system searches for the executable file in this sequence:
The directory from which the application loaded.
The current directory.
The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.
The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.
The directories listed in the PATH environment variable.
uCmdShow
[in] Specifies how a Windows-based application window is to be shown and is used to supply the wShowWindow member of the STARTUPINFO parameter to the CreateProcess function. For a list of the acceptable values, see the description of the nCmdShow parameter of the ShowWindow function. For a non-Windows – based application, the PIF file, if any, for the application determines the window state.
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:

Value Meaning
0 The system is out of memory or resources.
ERROR_BAD_FORMAT The .exe file is invalid.
ERROR_FILE_NOT_FOUND The specified file was not found.
ERROR_PATH_NOT_FOUND The specified path was not found.

Remarks
The WinExec function returns when the started process calls the GetMessage function or a time-out limit is reached. To avoid waiting for the time out delay, call the GetMessage function as soon as possible in any process started by a call to WinExec.

Security Remarks
The executable name is treated as the first white space-delimited string in lpCmdLine. If the executable or path name has a space in it, there is a risk that a different executable could be run because of the way the function parses spaces. The following example is dangerous because the function will attempt to run "Program.exe", if it exists, instead of "MyApp.exe".

WinExec("C:\Program Files\MyApp", ...)
If a malicious user were to create an application called "Program.exe" on a system, any program that incorrectly calls WinExec using the Program Files directory will run this application instead of the intended application.

To avoid this problem, use CreateProcess rather than WinExec. However, if you must use WinExec for legacy reasons, make sure the application name is enclosed in quotation marks as shown in the example below.

WinExec("\"C:\Program Files\MyApp.exe\" -L -S", ...)
Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.

====================
装个MSDN比较好点
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式