Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the following code to resolve a shortcut, but the CoUninitialize gives an access violation. What's wrong?

WCHAR FullName[MAX_PATH];
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
HRESULT hr = ResolveIt(fname, FullName, MAX_PATH);
CoUninitialize(); // This still gives access violation

HRESULT ResolveIt(WCHAR *LinkFile, WCHAR Path[], int iPathBufferSize)
{
  Path[0] = '\0'; // Assume failure 

  // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
  // has already been called. 
  IShellLink* psl;
  HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
  if (SUCCEEDED(hres))
  {
    IPersistFile* ppf;

    // Get a pointer to the IPersistFile interface. 
    hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);

    if (SUCCEEDED(hres))
    {

      // Load the shortcut. 
      hres = ppf->Load(LinkFile, STGM_READ);
hres = psl->Resolve(GetDesktopWindow(), 0);
      if (SUCCEEDED(hres))
      {

          // Get the path to the link target. 
          hres = psl->GetPath(Path, MAX_PATH, 0, SLGP_SHORTPATH);

      }
      // Release the pointer to the IPersistFile interface. 
      ppf->Release();

    }
    // Release the pointer to the IShellLink interface. 
    psl->Release();
  }
  return hres;
}


What I have tried:

I tried doing without the ->Resolve interface call and changing parameters, but the access violation stays. My sample uses UNICODE, so character to wide character conversion should not be necessary. The returned path is correct and all returned HRESULT values are S_OK.
Posted
Updated 1-Jul-20 6:38am
Comments
Andre Oosthuizen 1-Jul-20 5:15am    
Maybe this will help? - https://stackoverflow.com/questions/2653797/why-does-couninitialize-cause-an-error-on-exit

Sorry for posting too quickly, the access violation disappears when I use CoInitialize(0) instead of CoInitializeEx(..)
 
Share this answer
 
Comments
Richard MacCutchan 1-Jul-20 5:47am    
That does seem odd. I have a similar application and I use the same call. However I have another application where I use:
hResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

I have never had a problem with either one.
The CoInitializeEx() call is doing some stuff like loading libs and reading from registry. So it is better to give it some time (milliseconds) to run and than do your stuff. And it is better to do it only once in an app like on startup.

PS: remove your "solution" but modify the questions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900