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_She llLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
reinterpret_cast<void**>(& pShLink));
if (SUCCEEDED(hres))
{
pShLink->SetPath(lpszPath) ;
pShLink->SetDescription(lp szDesc);
pShLink->SetWorkingDirecto ry(lpszWor kingDir);
IPersistFile *pPersistFile;
hres = pShLink->QueryInterface( IID_IPersistFile, reinterpret_cast<void**>(& pPersistFi le));
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(SHGetFolderPa th(NULL, CSIDL_WINDOWS, NULL, 0, DesktopPath)))
printf("Done! The path is %s\n", DesktopFolderPath);
else
printf("Error creating desktop shortcut!\n");
}
#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;
/*------------------------
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_She
reinterpret_cast<void**>(&
if (SUCCEEDED(hres))
{
pShLink->SetPath(lpszPath)
pShLink->SetDescription(lp
pShLink->SetWorkingDirecto
IPersistFile *pPersistFile;
hres = pShLink->QueryInterface( IID_IPersistFile, reinterpret_cast<void**>(&
if (SUCCEEDED(hres))
{
wchar_t wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP
// Save the link
hres = pPersistFile->Save( wsz, TRUE);
pPersistFile->Release();
}
else
printf("Failed to create link!\n");
pShLink->Release();
}
CoUninitialize();
return hres;
}
void main()
{
/*------------------------
TCHAR DesktopPath[MAX_PATH];
if(SUCCEEDED(SHGetFolderPa
printf("Done! The path is %s\n", DesktopFolderPath);
else
printf("Error creating desktop shortcut!\n");
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You should try with CSIDL_PROFILE / CSIDL_PROFILES rather than CSIDL_WINDOWS to get Desktop folders
Best Regards,
DeepuAbrahamK
Best Regards,
DeepuAbrahamK
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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!
Can you tell where it goes wrong and what gows wrong?