转深入浅出ShellExecute

原文地址:转深入浅出ShellExecute 作者:黑洞

ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。

原型及参数含义

ShellExecute函数原型及参数含义如下:

  ShellExecute(

  hWnd: HWND; {指定父窗口句柄}

  Operation: PChar; {指定动作, 譬如: open、print}

  FileName: PChar; {指定要打开的文件或程序}

  Parameters: PChar; {给要打开的程序指定参数; 如果打开的是文件这里应该是 nil}

  Directory: PChar; {缺省目录}

  ShowCmd: Integer {打开选项}

  ): HINST; {执行成功会返回应用程序句柄; 如果这个值 <= 32, 表示执行错误}

  //返回值可能的错误有: = 0 {内存不足}

  ERROR_FILE_NOT_FOUND = 2; {文件名错误}

  ERROR_PATH_NOT_FOUND = 3; {路径名错误}

  ERROR_BAD_FORMAT = 11; {EXE 文件无效}

  SE_ERR_SHARE = 26; {发生共享错误}

  SE_ERR_ASSOCINCOMPLETE = 27; {文件名不完全或无效}

  SE_ERR_DDETIMEOUT = 28; {超时}

  SE_ERR_DDEFAIL = 29; {DDE 事务失败}

  SE_ERR_DDEBUSY = 30; {正在处理其他 DDE 事务而不能完成该 DDE 事务}

  SE_ERR_NOASSOC = 31; {没有相关联的应用程序}

  //ShowCmd 参数可选值:SW_HIDE = 0; {隐藏}

  SW_SHOWNORMAL = 1; {用最近的大小和位置显示, 激活}

  SW_NORMAL = 1; {同 SW_SHOWNORMAL}

  SW_SHOWMINIMIZED = 2; {最小化, 激活}

  SW_SHOWMAXIMIZED = 3; {最大化, 激活}

  SW_MAXIMIZE = 3; {同 SW_SHOWMAXIMIZED}

  SW_SHOWNOACTIVATE = 4; {用最近的大小和位置显示, 不激活}

  SW_SHOW = 5; {同 SW_SHOWNORMAL}

  SW_MINIMIZE = 6; {最小化, 不激活}

  SW_SHOWMINNOACTIVE = 7; {同 SW_MINIMIZE}

  SW_SHOWNA = 8; {同 SW_SHOWNOACTIVATE}

  SW_RESTORE = 9; {同 SW_SHOWNORMAL}

  SW_SHOWDEFAULT = 10; {同 SW_SHOWNORMAL}

  SW_MAX = 10; {同 SW_SHOWNORMAL}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Q: 如何打开一个应用程序?

ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );

ShellExecute(this->m_hWnd,"open","notepad.exe",
    "c:\MyLog.log","",SW_SHOW );

正如您所看到的,我并没有传递程序的完整路径。
[转载]转深入浅出ShellExecuteQ: 如何打开一个同系统程序相关连的文档?

ShellExecute(this->m_hWnd,"open","c:\abc.txt","","",SW_SHOW );
Q: 如何打开一个网页?
ShellExecute(this->m_hWnd,"open","http://www.google.com","","", SW_SHOW );

[转载]转深入浅出ShellExecuteQ: 如何激活相关程序,发送EMAIL?

ShellExecute(this->m_hWnd,"open", "mailto:[email protected]","","", SW_SHOW );

[转载]转深入浅出ShellExecuteQ: 如何用系统打印机打印文档?

ShellExecute(this->m_hWnd,"print","c:\abc.txt","","", SW_HIDE);

[转载]转深入浅出ShellExecuteQ: 如何用系统查找功能来查找指定文件?

ShellExecute(m_hWnd,"find","d:\nish",NULL,NULL,SW_SHOW);

[转载]转深入浅出ShellExecuteQ: 如何启动一个程序,直到它运行结束?

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c:\MyProgram.exe";                
ShExecInfo.lpParameters = "";   
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;     
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

或:

PROCESS_INFORMATION ProcessInfo; 
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field
if(CreateProcess("c:\winnt\notepad.exe", NULL, NULL,NULL,FALSE,0,NULL,NULL,&StartupInfo,&ProcessInfo))
{ 
    WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
    
    CloseHandle(ProcessInfo.hThread);
    
    CloseHandle(ProcessInfo.hProcess);
}  
else
{
    MessageBox("The process could not be started...");
}

[转载]转深入浅出ShellExecuteQ: 如何显示文件或文件夹的属性?

SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "properties";
ShExecInfo.lpFile = "c:\"; //can be a file as well
ShExecInfo.lpParameters = ""; 
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL; 
ShellExecuteEx(&ShExecInfo);


 

你可能感兴趣的:(Windows编程)