Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define UNICODE
- #define _UNICODE
- #include <objbase.h>
- #include <winuser.h>
- #include <windef.h>
- #include <shlobj.h>
- #include <olectlid.h>
- #include <objidl.h>
- #include <tchar.h>
- #include <iostream>
- 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;
- }
- int main(int argc, char** argv) {
- CoInitialize(0);
- LPCWSTR lpszPathObj = L"C:\\temp";
- LPCSTR lpszPathLink = "C:\\temp\\test.lnk";
- LPCWSTR lpszDesc = L"test";
- CreateLink(lpszPathObj, lpszPathLink, lpszDesc);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement