Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

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

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
cha0ticz
Asked:
cha0ticz
  • 3
  • 3
1 Solution
 
jkrCommented:
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
 
cha0ticzAuthor Commented:
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
 
jkrCommented:
What is the HRESULT value that is returned?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cha0ticzAuthor Commented:
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
 
jkrCommented:
>>-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
 
cha0ticzAuthor Commented:
Doh, duh. Thanks for your help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now