Solved

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

Posted on 2007-03-21
5
634 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now