PC Review


Reply
Thread Tools Rate Thread

Access denied fro GetShortPathName in Windows Vista

 
 
tuma
Guest
Posts: n/a
 
      22nd Aug 2008
I have a win32 application that creates a directory in Users's Application
Data directory (Example: c:\users\<username>\Local Settings\Application
Data\MyFolder) and writing application specific data there. Thinks are
working fine. But when I use GetShortPathName to get the short path for the
above path, it fails with Access denied error. I need it to pass the short
path to other application which only understands the short path.

I don't understand why this API should throw Access denied when I ask short
path for the folder that I created from the same application.

Any workaround for this issue?

 
Reply With Quote
 
 
 
 
David Lowndes
Guest
Posts: n/a
 
      22nd Aug 2008
>I have a win32 application that creates a directory in Users's Application
>Data directory (Example: c:\users\<username>\Local Settings\Application
>Data\MyFolder) and writing application specific data there. Thinks are
>working fine. But when I use GetShortPathName to get the short path for the
>above path, it fails with Access denied error. I need it to pass the short
>path to other application which only understands the short path.
>
>I don't understand why this API should throw Access denied when I ask short
>path for the folder that I created from the same application.


Your results do seem odd - do you have a short code (ideally a console
application) example that reproduces the issue?

Dave
 
Reply With Quote
 
 
 
 
tuma
Guest
Posts: n/a
 
      22nd Aug 2008
SHGetSpecialFolderPath(NULL, profilePath, CSIDL_PROFILE, 0);

//some code here

_tcscat_s(profilePath, MAX_BUFFER, _T("\\Local Settings\\Application
Data\\MyAPPFolder"));
if(PathFileExists(profilePath) == FALSE )
{
err = MyCreateDirectory(profilePath);
if (err != kAMCPErrorNone)
{
return err;
}
}


if (GetShortPathName(profilePath, gMyAppDataPath, MAX_PATH) == 0)
{
DispalyOSError(profilePath); // Access Denied error is the result
exit(0);
}


"David Lowndes" wrote:

> >I have a win32 application that creates a directory in Users's Application
> >Data directory (Example: c:\users\<username>\Local Settings\Application
> >Data\MyFolder) and writing application specific data there. Thinks are
> >working fine. But when I use GetShortPathName to get the short path for the
> >above path, it fails with Access denied error. I need it to pass the short
> >path to other application which only understands the short path.
> >
> >I don't understand why this API should throw Access denied when I ask short
> >path for the folder that I created from the same application.

>
> Your results do seem odd - do you have a short code (ideally a console
> application) example that reproduces the issue?
>
> Dave
>

 
Reply With Quote
 
David Lowndes
Guest
Posts: n/a
 
      22nd Aug 2008
>//some code here

Can you knock it into something stand-alone that anyone could paste
into a console project and compile cleanly.

Thanks
Dave
 
Reply With Quote
 
tuma
Guest
Posts: n/a
 
      25th Aug 2008
//***********************************************
// It has dependency with shlwapi.lib. Add it in your project settings.
//
//***********************************************

#include <windows.h>
#include <tchar.h>
#include <shlobj.h>
#include <shlwapi.h>

#define MAX_BUFFER 1000

_TCHAR gMyAppDataPath[MAX_PATH];
BOOL MyCreateDirectory(_TCHAR *path);
void DispalyOSError();

void main()
{
_TCHAR profilePath[MAX_BUFFER];

SHGetSpecialFolderPath(NULL, profilePath, CSIDL_PROFILE, 0);

//some code here

_tcscat_s(profilePath, MAX_BUFFER, _T("\\Local Settings\\Application
Data\\MyAPPFolder"));
if(PathFileExists(profilePath) == FALSE )
{
if (MyCreateDirectory(profilePath) != TRUE)
exit(0);
}


if (GetShortPathName(profilePath, gMyAppDataPath, MAX_PATH) == 0)
{
DispalyOSError(); // Access Denied error is the result
exit(0);
}
}

BOOL MyCreateDirectory(_TCHAR *dirName)
{
BOOL error = TRUE;

if (CreateDirectory(dirName,NULL) == FALSE)
{
_TCHAR temp[MAX_PATH], *ptr = NULL;
_tcscpy_s(temp, MAX_PATH, dirName);
ptr = _tcsrchr(temp, _T('\\'));
if (ptr)
{
*ptr = 0;
error = MyCreateDirectory(temp);
if (error != TRUE) return error;
}

error = CreateDirectory(dirName,NULL);
}

return error;
}

void DispalyOSError()
{
LPVOID lpMsgBuf;
DWORD dw = GetLastError();

FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL );

MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK);
}
 
Reply With Quote
 
David Lowndes
Guest
Posts: n/a
 
      25th Aug 2008
>// It has dependency with shlwapi.lib. Add it in your project settings.

FWIW...

#include <shlwapi.h>
#pragma comment( lib, "shlwapi.lib" )
.... neatly sorts that out


Thanks for the example. I can reproduce the issue with your code under
Vista. If you change your code to:

SHGetSpecialFolderPath(NULL, profilePath, CSIDL_LOCAL_APPDATA , 0);

_tcscat_s(profilePath, MAX_BUFFER, _T("\\MyAPPFolder"));

.... it'll use the correct directory under Vista (and XP)

Dave
 
Reply With Quote
 
tuma
Guest
Posts: n/a
 
      25th Aug 2008
Thanks. It works. But I am very curious to know why the “Access Denied”
error when I hard code the path.

 
Reply With Quote
 
SvenC
Guest
Posts: n/a
 
      25th Aug 2008
Hi tuma,

> Thanks. It works. But I am very curious to know why the “Access
> Denied” error when I hard code the path.


Compare your hard coded path and the path returned from
CSIDL_LOCAL_APPDATA. They are not identical, right?
Vista uses hard links to "support" the old (pre-Vista) directory
names in a limited way. Those hard links have a different
default security compared with what you get through the
above CSIDL. That might bight you.

In short: never use hard coded paths as different Windows
versions and languages will have slightly varying paths.

--
SvenC

 
Reply With Quote
 
David Lowndes
Guest
Posts: n/a
 
      25th Aug 2008
>Thanks. It works. But I am very curious to know why the Access Denied
>error when I hard code the path.


As Sven has pointed out, it's not really the same path under Vista.

Dave
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Upgrading fro Windows XP MCE SP3 to Vista. Harold A. Climer Windows Vista Installation 2 10th Sep 2011 08:42 AM
GetShortPathName Milton Snider Microsoft Access Form Coding 3 23rd Mar 2007 12:31 PM
Access denied ! Access Denied ! Access Denied! =?Utf-8?B?Y2hyaXNncnVudGxlZA==?= Windows Vista Administration 9 10th Jul 2006 05:04 PM
colin mcrae rally 04 fro windows vista =?Utf-8?B?Q2hyaXM=?= Windows Vista Installation 0 19th Jun 2006 04:00 PM
GetShortPathName API function Sakharam Phapale Microsoft VB .NET 2 9th Sep 2004 09:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:53 AM.