?
Solved

Modify the working directory of a .lnk file (shortcut file)

Posted on 2003-02-21
6
Medium Priority
?
475 Views
Last Modified: 2010-05-18
I need to change the working directory for a massive amount of *.lnk files in sub directories from a root directory. Everything I've found concerning *.lnk files has been on how to create them using iShellLink and I can't seem to figure out how to simply open an existing shortcut, change its working directory (I know its SetWorkingDirectory, but just initializing the shortcut as a object is the problem I'm having), then saving the changes.

Any help will be greatly appreciated.
0
Comment
Question by:cha0ticz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 7994327
Well, you'll have to use the 'IShellLink' IF like

HRESULT ChangeLinkPath(LPCSTR lpszNewPathObj, LPCSTR lpszPathLink)
{
  HRESULT hres;
  IShellLink* psl;

  // Get a pointer to the IShellLink interface.
  hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                          IID_IShellLink, (LPVOID*)&psl);
  if (SUCCEEDED(hres))
  {
      IPersistFile* ppf;

      // Query IShellLink for the IPersistFile interface 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);
             
          // TODO: Check return value from MultiByteWideChar to ensure
                   success.

          // Load the link by calling IPersistFile::Load.
           ppf->Load ( wsz, STGM_READWRITE);

             // Set the path to the shortcut target
            psl->SetPath(lpszPathObj);

          // Save the link by calling IPersistFile::Save.
          hres = ppf->Save(wsz, TRUE);
          ppf->Release();
      }
      psl->Release();
  }
  return hres;
}

(just altered the code from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/Shell/programmersguide/shell_int/shell_int_programming/shortcuts/shortcut.asp - "Shell Links")
0
 

Author Comment

by:cha0ticz
ID: 8011178
jkr,

Thank you for the code, however I'm getting a fail with

hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,IID_IShellLink, (LPVOID*)&psl);

I have objbase and shlobj included as well as ole32.lib and shell32.lib linked so I have no clue why that is failing.
0
 
LVL 86

Expert Comment

by:jkr
ID: 8011245
What is the HRESULT value that is returned?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cha0ticz
ID: 8011340
I see a return value of -2147221008 for it. No clue what that is, but it doesn't match S_OK, REGDB_E_CLASSNOTREG, or CLASS_E_NOAGGREGATION . Maybe I'm wrong on that though...
0
 
LVL 86

Expert Comment

by:jkr
ID: 8011385
>>-2147221008

...aka 0x800401F0

//
// MessageId: CO_E_NOTINITIALIZED
//
// MessageText:
//
//  CoInitialize has not been called.
//
#define CO_E_NOTINITIALIZED              _HRESULT_TYPEDEF_(0x800401F0L)

I suspect you have not called 'CoInitialize()' :o)
0
 

Author Comment

by:cha0ticz
ID: 8013041
Doh, duh. Thanks for your help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question