CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Nov 2008
    Posts
    26

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Creating Shortcut by IShellLink

    Could this article help you?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Creating Shortcut by IShellLink

    Well, both articles are almost twins. So I belive the key point is
    but It gives many errors
    most likely than
    using Dev C++ compiler.
    And the suggestion is to focus on the errors first. What about them?
    Best regards,
    Igor

  4. #4
    Join Date
    Nov 2008
    Posts
    26

    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

  5. #5
    Join Date
    Dec 2008
    Posts
    114

    Re: Creating Shortcut by IShellLink

    Instead of copying wrong and newbie code, simply copy-paste the official MSDN sample on IShellLink + IPersistFile...

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Creating Shortcut by IShellLink

    Quote Originally Posted by FastCode2010 View Post
    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); 
    . . .
    Best regards,
    Igor

  7. #7
    Join Date
    Nov 2008
    Posts
    26

    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)

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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);
    Best regards,
    Igor

  9. #9
    Join Date
    Nov 2008
    Posts
    26

    Re: Creating Shortcut by IShellLink

    Thanks Igor ...it is working perfect

  10. #10
    Join Date
    Feb 2009
    Posts
    1

    Re: Creating Shortcut by IShellLink

    I want to delete the shortcut thus created. How do I do it?

  11. #11
    Join Date
    Dec 2008
    Posts
    114

    Re: Creating Shortcut by IShellLink

    Quote Originally Posted by rajanisk View Post
    I want to delete the shortcut thus created. How do I do it?
    Like for a file...

  12. #12
    Join Date
    Aug 2006
    Posts
    3

    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?

  13. #13
    Join Date
    Feb 2005
    Posts
    2,160

    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\""

  14. #14
    Join Date
    Aug 2006
    Posts
    3

    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().

  15. #15
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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.
    Attached Files Attached Files
    Last edited by Igor Vartanov; December 22nd, 2009 at 04:46 PM.
    Best regards,
    Igor

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured