본문 바로가기
IT-개발,DB

[개발/VC++] ShellExecuteEx, CreateProcess, 연결된 프로그램, 디폴트 브라우저

by SB리치퍼슨 2015. 11. 9.





직접 예제를 만들수도 있지만 시간상 핑계로 인터넷에 있던 팁들을 나열했다.


Q: I want to bring up the Windows Find window on a particular folder.
A: We use the find verb as the operation parameter and we have the Windows Find window open up with the directory we have specified. This can be rather handy if you want to allow users to find some file within some folder. Just ask them for their folder and pop up a Find Window which has their folder as the root folder.

ShellExecute(m_hWnd,"find","d:\\nish",
    NULL,NULL,SW_SHOW);Big Brother - ShellExecuteEx
ShellExecuteEx is a more flexible call, in that it allows us to retrieve information about the program we just spawned. You'll need to fill up the SHELLEXECUTEINFO structure and pass it's address to ShellExecuteEx. Please lookup both on your copy of MSDN.

Q: How do I start a program, and halt execution of my current program, till that program exits?
A: You start the program using ShellExecuteEx and use WaitForSingleObject on the process handle.

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);


Q: I want to show the File or Folder properties window for a file or a folder.
A: This time what we do is to pass properties as the operation verb. We also need to specify SEE_MASK_INVOKEIDLIST as the fmask parameter of the SHELLEXECUTEINFO structure. That's why we have to use ShellExecuteEx here instead of ShellExecute. I owe this tip to David Lowndes, Microsoft MVP because it was David who helped me with that tip about the SEE_MASK_INVOKEIDLIST flag.

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);


CreateProcess - an ultra brief introduction

The CreateProcess function is part of Kernel32.dll. Windows uses this call to create a new process and a primary thread for the new process. The primary thread then starts executing the specified executable. Normally, if this is a C++  program, execution starts with your WinMain [actually prior to this the CRT library is loaded and initialized]. For a more comprehensive tutorial on the use of CreateProcess, I recommend that you read Joseph M Newcomer's article, An Introduction to Processes: Asynchronous Process Notification.

PROCESS_INFORMATION ProcessInfo; //This is what we get as an [out] parameter
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...");
}
As you can observe, I am using WaitForSingleObject on the process handle. But since CreateProcess creates a thread object in addition to the process object, I might as well have waited on the thread handle as in :-

WaitForSingleObject(ProcessInfo.hThread,INFINITE);This might cause problems though if, for some reasons, one or more of your secondary threads are still active even after the main thread finishes. MSDN says that a process is fully terminated only when all it's threads have ceased execution. So I recommend that you wait on the process handle rather than on the thread handle.

// 디폴트 브라우저 감지하기
void GetDefaultBrowser(LPTSTR szBrowerName)
{
HFILE h = lcreate("dummy.htm", 0);
_lclose(h);

FindExecutable("dummy.htm", NULL, szBrowserName);
DeleteFile("dummy.htm");
}

// 파일에 연결된 프로그램 경로 읽어오기
HINSTANCE FindExecutableEx(... 인자는 동일하다 ...) 

TCHAR drive[_MAX_DRIVE]; 
TCHAR dir[_MAX_DIR]; 
TCHAR dir1[_MAX_DIR]; 
TCHAR file[_MAX_FILE]; 
TCHAR ext[_MAX_EXT];

HINSTANCE hi = FindExecutable(file, dir, result); 
result[lstrlen(result)] = 32;

_splitpath(result, deive, dir, file, ext);

LPTSTR p = strchr(dir, ':'); 
if(p != NULL) 

--p; 
dir[p-dir] = 0; 
_splitpath(dir, NULL, dir1, file, ext); 
p = strchr(ext, 32); 
ext[p-ext] = 0; 
_makepath(result, drive, dir1, file, ext); 

return hi; 



출처 : 인터넷


반응형

댓글