请问一下,如何用C语言实现在桌面创建一个文件的快捷方式?
------最佳解决方案--------------------------------------------------------
楼上很不错。我也一直就这个。
------其他解决方案--------------------------------------------------------
已经搞定了,代码如下:
void CreateOnDesktop()
{
IShellLink *pShellLink;//定义IShellLink指针
IPersistFile *pPersiitFile;//定义Ipersistfile指针
HRESULT hResult;//初始化com
char savePath[MAX_PATH];
int len=0;
OLECHAR widePath[MAX_PATH];
char pFileName[MAX_PATH];
HMODULE module;
static char * username = NULL;
static char * password = NULL; //static char *
static char * domain = NULL;
static char * srvaddr = NULL;
char cmdline[MAX_PATH];
username = g_loginInfo.username;
password = g_loginInfo.password;
domain = g_loginInfo.domain;
srvaddr = g_loginInfo.serveraddress;
module = GetModuleHandle(0);
GetModuleFileName(module, pFileName, MAX_PATH); //得到应用程序exe的路径
memset(cmdline,0,MAX_PATH);
strcat(cmdline,username);
strcat(cmdline," ");
strcat(cmdline,password);
strcat(cmdline," ");
strcat(cmdline,domain);
strcat(cmdline," ");
strcat(cmdline,srvaddr);
//hResult =CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
//if (hResult==S_OK)
//{//初始化成功则申请组件对象并获得IShellLink接口指针
hResult=CoCreateInstance(&CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
&IID_IShellLink,
(void **)&pShellLink);
if (hResult==S_OK)
{//获得IShellLink接口指针成功则查询并获得IPersistFile接口指针
hResult=pShellLink->lpVtbl->QueryInterface(pShellLink,
&IID_IPersistFile,
&pPersiitFile);
if (hResult==S_OK)
{
hResult=pShellLink->lpVtbl->SetPath(pShellLink,pFileName);//指明被创建快捷方式文件路径 "c:\\COMZUJIAN.txt"
pShellLink->lpVtbl->SetArguments(pShellLink,cmdline); //设置命令行参数
pShellLink->lpVtbl->SetDescription(pShellLink,"Shortcut config.sys");//设置快捷方式图标的文本
GetWindowsDirectory(savePath,MAX_PATH);
//GetCurrentDirectory(MAX_PATH,savePath);
len=strlen(savePath);
strcpy(&savePath[len],"\\COMZUJIAN.lnk");
MultiByteToWideChar(CP_ACP,0,savePath,-1,widePath,MAX_PATH);
pPersiitFile->lpVtbl->Save(pPersiitFile,widePath,TRUE);//保存快捷方式
pPersiitFile->lpVtbl->Release(pPersiitFile);//组件对象引用计数减1
}
}
pShellLink->lpVtbl->Release(pShellLink);//组件对象引用计数减1
//}
//CoUninitialize();
}
------其他解决方案--------------------------------------------------------
该回复于2012-04-18 16:25:25被版主删除
------其他解决方案--------------------------------------------------------
该回复于2012-04-18 16:25:17被版主删除