Solved

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

Posted on 2007-03-21
5
654 Views
Last Modified: 2008-01-09
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");
}
0
Comment
Question by:fuzzyling
5 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18762811
>>>> I do not get the desktop directory returned properly
Can you tell where it goes wrong and what gows wrong?
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
ID: 18762870
CSIDL_WINDOWS identifies the WINDIR or Windows directory, e. g. C:\Windows, rather than the desktop.

I read in the docs that the return value of SHGetFolderPathA and SHGetFolderPathW were different. So maybe the SUCCEEDED macro was not redefined properly.

MSDN:
E_FAIL SHGetFolderPathW only. The CSIDL in nFolder is valid, but the folder does not exist. Note that the failure code is different for the ANSI and Unicode versions of this function.

Try:

if (!(E_FAIL == SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, 0, DesktopPath)))
      printf("Done! The path is %s\n", DesktopFolderPath);
   else
      printf("Error creating desktop shortcut!\n");
}

Regards, Alex
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18764553
You should try with CSIDL_PROFILE / CSIDL_PROFILES rather than CSIDL_WINDOWS to get Desktop folders

Best Regards,
DeepuAbrahamK
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 18765008
Keep your parameters and string types consitent:

#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 -------------------------*/

// not 'LPSTR lpszPathLink', but 'LP*T*STR lpszPathLink'
HRESULT CreateSC(LPCTSTR lpszPath, LPTSTR 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))
      {
      /* no conversion needed if you pass a UNICODE path to link
         wchar_t wsz[MAX_PATH];    
         MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
        */      
         // Save the link
         hres = pPersistFile->Save( lpszPathLink, TRUE);
         pPersistFile->Release();
        }
         else
            wprintf(L"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)))
      wprintf(L"Done! The path is %s\n", DesktopFolderPath);
   else
      wprintf(L"Error creating desktop shortcut!\n");

}
0
 

Author Comment

by:fuzzyling
ID: 18768729
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!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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