Link to home
Start Free TrialLog in
Avatar of fuzzyling
fuzzyling

asked on

How to get 'SHGetFolderPath' to work with '#define UNICODE'

I am trying to get 'SHGetFolderPath' to work with the '#define UNICODE'. I need '#define UNICODE' as within the same piece of code, i need to pass in unicode for a function to work. Unfortunately when i try to compile and run the following code, I do not get the desktop directory returned properly. Any idea why?

#define UNICODE
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
#include <lm.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <string>
#include <aclapi.h>
#include <winerror.h>
#include <tchar.h>
using namespace std;

/*------------------------- Function to provide desktop shortcut -------------------------*/
HRESULT CreateSC(LPCTSTR lpszPath, LPSTR lpszPathLink, LPCTSTR lpszDesc, LPCTSTR lpszWorkingDir)
{
   CoInitialize(NULL);
   HRESULT hres;
   IShellLink *pShLink;
   // Get a pointer to the IShellLink interface.
   hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
   reinterpret_cast<void**>(&pShLink));
   if (SUCCEEDED(hres))
   {          
      pShLink->SetPath(lpszPath);
      pShLink->SetDescription(lpszDesc);
      pShLink->SetWorkingDirectory(lpszWorkingDir);

      IPersistFile *pPersistFile;
      hres = pShLink->QueryInterface( IID_IPersistFile, reinterpret_cast<void**>(&pPersistFile));
         
      if (SUCCEEDED(hres))
      {
         wchar_t wsz[MAX_PATH];    
         MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
               
         // Save the link
         hres = pPersistFile->Save( wsz, TRUE);
         pPersistFile->Release();
        }
         else
            printf("Failed to create link!\n");
         pShLink->Release();
   }
   CoUninitialize();
   return hres;
}

void main()
{
/*------------------------ Creates a desktop shortcut -------------------------*/
   TCHAR DesktopPath[MAX_PATH];
    if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, 0, DesktopPath)))
      printf("Done! The path is %s\n", DesktopFolderPath);
   else
      printf("Error creating desktop shortcut!\n");
}
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

>>>> I do not get the desktop directory returned properly
Can you tell where it goes wrong and what gows wrong?
SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Deepu Abraham
You should try with CSIDL_PROFILE / CSIDL_PROFILES rather than CSIDL_WINDOWS to get Desktop folders

Best Regards,
DeepuAbrahamK
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fuzzyling
fuzzyling

ASKER

Hi,
Thanks Alex for pointing out that 'CSIDL_WINDOWS' identifies the windows directory. I was debugging and changed it from 'CSIDL_DESKTOPDIRECTORY'. Thanks jkr for helping me to correct those inconsistencies!