Can I create shortcut in C++

I can't debug this code on DEV-C . Who can solve this problem ? .please help me !

#include <windows.h>
#include <shlobj.h>
#include <iostream>
using namespace std;

int main()
{
CoInitialize(NULL);
IShellLink* pShellLink ;
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL,IID_IShellLink, (void**)&pShellLink);
pShellLink->SetPath("E:\\Set.exe");
IPersistFile *pPersistFile;
hres = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);
hres = pPersistFile->Save(L"C:\\Users\\Kudoshinichi\\Desktop\\Set.lnk", TRUE);
pPersistFile->Release();

return 0;
}
Please use code tags --> the <> button to the right of the cplusplus.com post edit box. As well as looking nice, it provides line numbers which make it easier to refer to posted code.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {
    cout << "Hello nicely formatted world!\n";

    return 0;
}


And firedraco's article: "How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/

You can even go back and add tags to your earlier posts!

Andy
Oh,Thanks,i'm a new member.hi.

ah,Can u solve my problem ?
I did it on Visual Studio C++ 2008 .It run ,but on Dev-c ,It can't debug.

I don't understand !
this code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <windows.h>
#include <shlobj.h>
#include <iostream>
using namespace std;

int main()
{
CoInitialize(NULL);
IShellLink* pShellLink ;
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL,IID_IShellLink, (void**)&pShellLink);
pShellLink->SetPath("E:\\Set.exe");
IPersistFile *pPersistFile;
hres = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);
hres = pPersistFile->Save(L"C:\\Users\\Kudoshinichi\\Desktop\\Set.lnk", TRUE);
pPersistFile->Release();

return 0;
}
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
Last edited on
Topic archived. No new replies allowed.