Thanks for formatting your code.
It does look like it should be more or less doing the right thing, even though it's not handling errors and is leaking memory.
But what exactly is the problem? Does it not work for you? Or what??
Altering the paths to "C:\\Test\\hello.exe" and "C:\\Test\\test.lnk" for testing purposes, the code creates a link for me.
To complete your code you need to:
- handle the return codes. You usually use the SUCEEDED() and FAILED() macros in COM programming, e.g.
if( SUCCEEDED(hres) ) // etc
- release all interfaces you successfully acquire (not just pPersistFile)
- you should obtain the path to the user's desktop folder using SHGetKnownFolderPath (Windows Vista and newer) or SHGetFolderPath.
- preferably call CoUninitialize(), to balance the call to CoInitialize()
initialize all variable before use, e.g.
- preferably initialize all variable before use, e.g.
IShellLink* pShellLink = NULL;
IShellLink* pShellLink = NULL;
(And add some program output?)
See example code on this page:
Shell Links
http://msdn.microsoft.com/en-us/library/windows/desktop/bb776891%28v=vs.85%29.aspx
Andy