bool SetAutoStart_WinStart(bool _blSet,AnsiString _strExeName,AnsiString _strDescription)
{
IShellLink* pLink;
IPersistFile* pPersistFile;
LPMALLOC ShellMalloc;
LPITEMIDLIST DesktopPidl;
char DesktopDir[MAX_PATH];
memset(DesktopDir,'/0',MAX_PATH);
AnsiString astrLinkFile=ExtractFilePath(Application->ExeName)+_strExeName+".exe";
bool blRet=false;
if(FAILED(SHGetMalloc(&ShellMalloc)))return blRet;
if(FAILED(SHGetSpecialFolderLocation(NULL,/*CSIDL_DESKTOPDIRECTORY*/CSIDL_COMMON_STARTUP,&DesktopPidl)))return blRet;
if(!SHGetPathFromIDList(DesktopPidl,DesktopDir))
{
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
return false;
}
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&pLink)))
{
pLink->SetPath(astrLinkFile.c_str());
pLink->SetDescription(_strDescription.c_str());
pLink->SetShowCmd(SW_SHOW);
pLink->SetWorkingDirectory(ExtractFilePath(Application->ExeName).c_str());
if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,(void**)&pPersistFile)))
{
strcat(DesktopDir,"//");
strcat(DesktopDir,_strExeName.c_str());
strcat(DesktopDir,".lnk ");
if(_blSet)
{
AnsiString astrTemp;
astrTemp=DesktopDir;
wchar_t * wcp=(wchar_t *)malloc(sizeof(wchar_t)*(astrTemp.Length()+1));
memset(wcp,'/0',sizeof(wchar_t)*(astrTemp.Length()+1));
astrTemp.WideChar(wcp,astrTemp.Length());
if(SUCCEEDED(pPersistFile->Save(wcp,TRUE)))
{
blRet=true;
}
free(wcp);
}
else
{
if(DeleteFile(DesktopDir))
{
blRet=true;
}
}
pPersistFile->Release();
}
pLink->Release();
}
CoUninitialize();
}
return blRet;
}
设置程序能够在开始菜单
最新推荐文章于 2022-10-12 15:39:28 发布