// CreateLink - Uses the Shell's IShellLink and IPersistFile interfaces // to create and store a shortcut to the specified object. // // Returns the result of calling the member functions of the interfaces. // // Parameters: // lpszPathObj - Address of a buffer that contains the path of the object, // including the file name. // lpszPathLink - Address of a buffer that contains the path where the // Shell link is to be stored, including the file name. // lpszDesc - Address of a buffer that contains a description of the // Shell link, stored in the Comment field of the link // properties. /* #include "stdafx.h" #include "windows.h" #include "winnls.h" #include "shobjidl.h" #include "objbase.h" #include "objidl.h" #include "shlguid.h" */ #include #include HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc) { HRESULT hres; IShellLink* psl; // Get a pointer to the IShellLink interface. It is assumed that CoInitialize // has already been called. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Set the path to the shortcut target and add the description. psl->SetPath(lpszPathObj); psl->SetDescription(lpszDesc); // Query IShellLink for the IPersistFile interface, used for saving the // shortcut in persistent storage. hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); // Add code here to check return value from MultiByteWideChar // for success. // Save the link by calling IPersistFile::Save. hres = ppf->Save(wsz, TRUE); ppf->Release(); } psl->Release(); } return hres; } #include //******************************************************************************* // Name : _tmain // Create : 2020/01/14 //******************************************************************************* int _tmain (int argc,TCHAR* argv[]) { TCHAR exe_path[MAX_PATH] ; ::GetModuleFileName(NULL,exe_path,MAX_PATH) ; TCHAR exe_name[MAX_PATH] ; ::_tcscpy(exe_name,_T("CreateLnk")) ; TCHAR tmp_path[MAX_PATH] ; ::GetTempPath(MAX_PATH,tmp_path) ; TCHAR lnk_name[MAX_PATH] ; ::_tcscpy(lnk_name,exe_name) ; ::_tcscat(lnk_name,_T(".lnk")) ; TCHAR lnk_path[MAX_PATH] ; ::_tcscpy(lnk_path,tmp_path) ; ::_tcscat(lnk_path,lnk_name) ; TCHAR descript[MAX_PATH] ; ::_tcscpy(descript,_T("shortcut ")) ; if (_taccess(lnk_path,0) != 0) { ::CoInitialize(NULL) ; char mb_lnk_path[MAX_PATH] ; ::WideCharToMultiByte(CP_ACP,0,lnk_path,-1,mb_lnk_path,MAX_PATH,NULL,NULL) ; ::CreateLink(exe_path,mb_lnk_path,descript) ; ::CoUninitialize() ; } else { _tremove(lnk_path) ; } return 0 ; }