void MyFun::GetUrl(CString sURL)
{
HKEY hkRoot,hSubKey; //定义注册表根关键字及子关键字
char ValueName[256];
unsigned char DataValue[256];
unsigned long cbValueName=256;
unsigned long cbDataValue=256;
char ShellChar[256]; //定义命令行
DWORD dwType;
//打开注册表根关键字
if(RegOpenKey(HKEY_CLASSES_ROOT,NULL,&hkRoot)==ERROR_SUCCESS)
{
//打开子关键字
if(RegOpenKeyEx(hkRoot,
"htmlfile//shell//open//command",
0,
KEY_ALL_ACCESS,
&hSubKey)==ERROR_SUCCESS)
{
//读取注册表,获取默认浏览器的命令行
RegEnumValue(hSubKey,
0,
ValueName,
&cbValueName,
NULL,
&dwType,
DataValue,
&cbDataValue);
// 调用参数(主页地址)赋值
strcpy(ShellChar,(char *)DataValue);
CString str;
str.Format("%s", ShellChar);
str.Replace("-nohome","");
str.Replace("/"%1/"","");
str += sURL;
//strcat(ShellChar,sURL);
// 启动浏览器
//WinExec(ShellChar,SW_SHOW);
WinExec(str,SW_SHOW);
}
else
{
//关闭注册表
RegCloseKey(hSubKey);
RegCloseKey(hkRoot);
}
}
}
////////////////////////////////////////
ShellExecute(NULL, _T("open"), _T("explorer"), _T(www.huaxunsoft.com), NULL, SW_SHOWNORMAL);
VC中ShellExecute()函数使用方法
ShellExecute()函数用于打开或执行一个文件,在调用此函数时只须指定要打开或执行的文件名,而不必管用什么程序去打开或执行文件,WINDOWS会自动根据要打开或执行的文件去判断该如何执行文件或用什么程序去打开文件。函数中的参数lpOperation说明所要执行的操作,该值可以设置为"Open"、"Print"、"Explore",分别用来进行"打开"、"打印"、"浏览"操作。下面给出了ShellExecute()函数的一些使用方法:
(1)打开一个应用程序:
ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );
或
ShellExecute(this->m_hWnd,"open","notepad.exe", "c:MyLog.log","",SW_SHOW );
(2)打开一个同系统程序相关连的文档
ShellExecute(this->m_hWnd,"open", "c:abc.txt","","",SW_SHOW );
(3)打开一个网页
ShellExecute(this->m_hWnd,"open", " http://www.google.com","","",/ SW_SHOW );
在新窗口打开网页的办法:
ShellExecute(NULL,"open","IEXPLORE","http://www.huaxunsoft.com",NULL,SW_SHOWNORMAL);
(4)激活相关程序,发送EMAIL
ShellExecute(this->m_hWnd,"open","mailto:[email protected]","","", W_SHOW );
(5)用系统打印机打印文档
ShellExecute(this->m_hWnd,"print", "c:abc.txt","","", SW_HIDE);
(6)用系统查找功能来查找指定文件
ShellExecute(m_hWnd,"find","d:nish", NULL,NULL,SW_SHOW);
(7)启动一个程序,直到它运行结束
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:winntnotepad.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...");
}
或:
SHELLEXECUTEINFO exeInfo;
exeInfo.cbSize = sizeof(SHELLEXECUTEINFO);
exeInfo.fMask = (SEE_MASK_DOENVSUBST|SEE_MASK_FLAG_DDEWAIT|0x04000000|SEE_MASK_NOCLOSEPROCESS|SEE_MASK_NO_CONSOLE);
exeInfo.hwnd = NULL;
exeInfo.lpVerb = "open";
exeInfo.lpFile = (LPCTSTR)路径&文件名;
exeInfo.lpParameters = NULL;
exeInfo.lpDirectory = NULL;
exeInfo.nShow = SW_SHOWNORMAL;
exeInfo.hInstApp = NULL;
exeInfo.lpIDList = NULL;
exeInfo.lpClass = NULL;
exeInfo.hkeyClass = NULL;
exeInfo.dwHotKey = NULL;
exeInfo.hMonitor = NULL;
exeInfo.hIcon = NULL;
exeInfo.hProcess = NULL;
::ShellExecuteEx(&exeInfo);
if(exeInfo.hProcess)
{
::WaitForSingleObject(exeInfo.hProcess,INFINITE);
::CloseHandle(exeInfo.hProcess);
}
(8)显示文件或文件夹的属性
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还提供了一个与ShellExecuteEx()函数相类似的函数WinExec(),它相对于ShellExecuteEx()来说更简单易用,只是功能没有它强大而已,具体使用方法读者朋友自行参阅MSDN。