Creating Shortcut by IShellLink
Hii
I need a help how to create shortcut in c++ by using the IShellLink method .
I try to make one like this
http://www.intelliproject.net/articl...ortcut_cpp_com
but It gives many errors using Dev C++ compiler.
I dont know what files shall I include.
Can someone help me by provding small example .
Thanks in advance
Re: Creating Shortcut by IShellLink
Could this article help you?
Re: Creating Shortcut by IShellLink
Well, both articles are almost twins. :) So I belive the key point is
Quote:
but It gives many errors
most likely than
Quote:
using Dev C++ compiler.
And the suggestion is to focus on the errors first. What about them?
Re: Creating Shortcut by IShellLink
The code is
-----------------------------
#include <windows.h>
#include <shobjidl.h>
#include <shlguid.h>
BOOL CreateShortCut(CString file , CString shortCut)
{
IShellLink* pISL;
IPersistFile* pIPF;
HRESULT hr;
hr = CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&pISL);
if(!SUCCEEDED(hr))
{
return FALSE;
}
else
{
hr = pISL->SetPath(file);
if(!SUCCEEDED(hr))
{
return FALSE;
}
else
{
hr = pISL->QueryInterface(IID_IPersistFile,(void**)&pIPF);
if(SUCCEEDED(hr))
{
hr = pIPF->Save(CA2W(shortCut),FALSE);
pIPF->Release();
}
else
{
return FALSE;
}
}
pISL->Release();
}
return TRUE;
}
int main()
{
CoInitialize ( NULL );
CreateShortCut("C:\\Test.exe", "C:\\Test.lnk");
}
-----------------------------------
The errors by Dev C++
------------------------
4: error: `CString' was not declared in this scope
4: error: `CString' was not declared in this scope
5: error: initializer expression list treated as compound expression
5: error: expected `,' or `;' before '{' token
In function `int main()':
41: error: `CreateShortCut' cannot be used as a function
Execution terminated
Re: Creating Shortcut by IShellLink
Instead of copying wrong and newbie code, simply copy-paste the official MSDN sample on IShellLink + IPersistFile...
Re: Creating Shortcut by IShellLink
Quote:
Originally Posted by
FastCode2010
The errors by Dev C++
------------------------
4: error: `CString' was not declared in this scope
4: error: `CString' was not declared in this scope
5: error: initializer expression list treated as compound expression
5: error: expected `,' or `;' before '{' token
In function `int main()':
41: error: `CreateShortCut' cannot be used as a function
Execution terminated
Well, as I can see the problem is CString only (all further ones are caused by that), and it has nothing to do with shell link. You have to provide an implementation of CString class, or otherwise go with some other string implementation.
Code:
#include <comdef.h> // for _bstr_t
BOOL CreateShortCut(LPCTSTR file , LPCTSTR shortCut)
. . .
hr = pIPF->Save(_bstr_t(shortCut),FALSE);
. . .
Re: Creating Shortcut by IShellLink
Igor ....Thanks it working good now but by Visual studio .
using Dev c++ required
#include <comdef.h> // for _bstr_t
which required some more headers file .
....any suggestion how can I use someting else than _bstr_t to make it work on Dev
( I need Dev becuse my whole app compiled by Dev)
Re: Creating Shortcut by IShellLink
In fact, the point is to provide OLESTR string (which is just a WCHAR string). So this would do:
Code:
WCHAR wshortcut[MAX_PATH] = {0};
mbstowcs(wshortcut, shortCut, MAX_PATH); // you need this only if your original string is MBCS one
hr = pIPF->Save(wshortcut,FALSE);
Re: Creating Shortcut by IShellLink
Thanks Igor ...it is working perfect
Re: Creating Shortcut by IShellLink
I want to delete the shortcut thus created. How do I do it?
Re: Creating Shortcut by IShellLink
Quote:
Originally Posted by
rajanisk
I want to delete the shortcut thus created. How do I do it?
Like for a file...
Re: Creating Shortcut by IShellLink
IShellLink::SetPath() seems to jerk with the slashes and quotes that are passed in. I need to have something like
c:\windows\system32\cmd.exe /C "C:\Program Files\Microsoft Is Killing Me\ForReal.bat"
in the shortcut target. This exact string above works if I use the GUI and type it into the Target field on the shortcut properties. However, SetPath() adds a leading and trailing quotation mark and changes the forward slash to a backslash.
Is there a way to use the IShellLink interface and specify *exactly* what is used for the target string?
Re: Creating Shortcut by IShellLink
Well both the double quote character and the backslash character are special characters in C/C++ so you have to "escape" them with a backslash:
Code:
"c:\\windows\\system32\\cmd.exe /C \"C:\\Program Files\\Microsoft Is Killing Me\\ForReal.bat\""
Re: Creating Shortcut by IShellLink
Please read my original post more carefully. My problem is not with the escaping of the quotes or backslash.
My problem is
#1 - IShellLink::SetPath() changes a *forward slash* to a backslash. This is the "/C" following the "cmd.exe" in my string. You don't need to escape forward slashes in C++ strings ;)
#2 - Having the quotes put at the beginning and end of my string makes the shortcut not work. What I want to be the target is
c:\windows\system32\cmd.exe /C "C:\Program Files\Microsoft Whatever\something.bat"
But what IShellLink::SetPath makes it is
"c:\windows\system32\cmd.exe \C "C:\Program Files\Microsoft Whatever\something.bat""
The foward slash to backslash issue is not as big of a deal as the quote issue. If I want to call out cmd.exe followed by a path with spaces in it, it's not possible with IShellLink::SetPath().
1 Attachment(s)
Re: Creating Shortcut by IShellLink
You evidently failed to read documentation carefully. SetPath is intended for path specification only. And SetArguments does the trick in its turn.