即:如果我的程序与exe文件类型关联,则运行其它*.exe程序,首先运行我的程序,我的程序启动后,再运行对应的.exe文件。如何实现?
我这儿有一段代码,挺简单的,但是有一个错误我不明白,请大家帮我分析一下。
BOOL MyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CString str;
str.Format("%s", AfxGetApp()->m_lpCmdLine);//取得传入的命令行参数
if(str!="")
{
CString temp;
char ch;
int length =str.GetLength();
for(int i=0;i<length;i++)
{
ch=str.GetAt(i);
if(ch=='\\')
temp=temp+"\\\\";
else 
temp=temp+ch;
}
temp = temp.Left(temp.GetLength() - 2);
temp = temp.Mid(1);
//上面的函数是把字符串中所有的”\”变为”\\”
char str1[]="\"%1\" %*";
::RegSetValue(HKEY_CLASSES_ROOT,"exefile\\shell\\open\\command",REG_SZ,
(LPCTSTR)str1 , strlen(str1) + 1);
//在执行原有程序之前必须把注册表恢复,否则用ShellExecute还是执行自己的程序
ShellExecute(NULL,"open",temp,NULL,NULL,SW_SHOW);//执行原有的程序
AfxMessageBox(temp);
}
//在原程序启动并执行完成后,再把注册表改为我自己的程序........(1)
TCHAR str2[256];
// 得到程序全路径名
GetModuleFileName( NULL, str2, 255 );
AfxMessageBox(str2);
strcat(str2," \"%1\" %*");
::RegSetValue(HKEY_CLASSES_ROOT,"exefile\\shell\\open\\command",
REG_SZ,(LPCTSTR)str2 , strlen(str2) + 1);对于上面的程序,经过我的测试,程序首次运行,str为空(""),所以程序执行(1)句以后的部分,即将注册表内的exe文件类型关联为我的程序, 所以,以后每次执行exe文件,则实际上执行的是我的程序。但我发现,运行别的exe文件后(比如说运行d:\winamp.exe),str确实是非空了,str在程序中的意义为命令行参数,但奇怪的是这个str的值为:"c:\winnt\system32\notepad.exe"D:\winamp.exe,我不知道前面为什么会有c:\winnt\system32\notepad.exe,不仅是运行winamp.exe,运行任何一个.exe文件,str前面都会有"c:\winnt\system32\notepad.exe",这是为什么?
上面是我的问题,而且,我不太明白命令行到底有什么用,为什么第一次运行str为空呢?而通过运行其它exe文件来运行我的程序后,str就不为空呢?而且为什么str前面会有"c:\\winnt\systeme32\notepad.exe"标志呢。如果我上面错误比较多,请有经验的大哥给点相关的代码,只要能实现我的功能就可以。

解决方案 »

  1.   

    我试过了,没任何问题
    不过我不知道你为什么要加这一段
    CString temp;
    char ch;
    int length =str.GetLength();
    for(int i=0;i<length;i++)
    {
    ch=str.GetAt(i);
    if(ch=='\\')
    temp=temp+"\\\\";
    else 
    temp=temp+ch;
    }
    temp = temp.Left(temp.GetLength() - 2);
    temp = temp.Mid(1);
    //上面的函数是把字符串中所有的”\”变为”\\”直接
    CString str;
    str.Format("%s", AfxGetApp()->m_lpCmdLine);//取得传入的命令行参数
    if(str!="")
    {
    char str1[]="\"%1\" %*";
    ::RegSetValue(HKEY_CLASSES_ROOT,"exefile\\shell\\open\\command",REG_SZ,
    (LPCTSTR)str1 , strlen(str1) + 1);
    //在执行原有程序之前必须把注册表恢复,否则用ShellExecute还是执行自己的程序
    ShellExecute(NULL,"open",str,NULL,NULL,SW_SHOW);//执行原有的程序
    AfxMessageBox(str);
    }
    //在原程序启动并执行完成后,再把注册表改为我自己的程序........(1)
    TCHAR str2[256];
    // 得到程序全路径名
    GetModuleFileName( NULL, str2, 255 );
    AfxMessageBox(str2);
    strcat(str2," \"%1\" %*");
    ::RegSetValue(HKEY_CLASSES_ROOT,"exefile\\shell\\open\\command",
    REG_SZ,(LPCTSTR)str2 , strlen(str2) + 1);就可以了