codeBlocks+C语言创建包含中文的快捷方式失败
本帖最后由 Qiaorui 于 2013-05-28 10:43:25 编辑 参照http://bbs.csdn.net/topics/390008898来创建快捷方式,快捷方式不含中文比较顺利,但是包含中文就问题多多,没办法,对快捷方式的创建还不熟悉。
环境:codeBlocks+MinGW,使用UTF-8编码,使用 UNICODE 编译。
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 * 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();
}