Board index » cppbuilder » AccessVialoation on SHGetSpecialFolderLocation

AccessVialoation on SHGetSpecialFolderLocation


2005-08-06 05:22:10 AM
cppbuilder10
Hello, I am trying to use the following code to read the user's Send To
directory:
char cBuf[MAX_PATH];
LPITEMIDLIST pidl;
SHGetSpecialFolderLocation(0, CSIDL_SENDTO, &pidl);
SHGetPathFromIDList(pidl, cBuf);
It compiles OK, but I get an access violation Exception on the call to
SHGetSpecialFolderLocation.
Can anyone suggest why?
 
 

Re:AccessVialoation on SHGetSpecialFolderLocation

"George Francis" < XXXX@XXXXX.COM >wrote in message
Quote
Hello, I am trying to use the following code to read the user's Send To
directory:

char cBuf[MAX_PATH];
LPITEMIDLIST pidl;
SHGetSpecialFolderLocation(0, CSIDL_SENDTO, &pidl);
SHGetPathFromIDList(pidl, cBuf);

It compiles OK, but I get an access violation Exception on the call to
SHGetSpecialFolderLocation.
Can anyone suggest why?
No, but maybe you might want to use this code instead:
AnsiString __fastcall GetSpecialPath(int Folder)
{
char Path[MAX_PATH + 1] = { 0 };
if( ::SHGetSpecialFolderPath( NULL, Path, Folder, 0 ) )
return AnsiString( Path );
return "";
}
GetSpecialPath( CSIDL_SENDTO );
Jonathan
 

Re:AccessVialoation on SHGetSpecialFolderLocation

Jonathan Benedicto wrote:
Quote
"George Francis" < XXXX@XXXXX.COM >wrote in message
news:42f3d881$ XXXX@XXXXX.COM ...

>Hello, I am trying to use the following code to read the user's Send To
>directory:
>
>char cBuf[MAX_PATH];
>LPITEMIDLIST pidl;
>SHGetSpecialFolderLocation(0, CSIDL_SENDTO, &pidl);
>SHGetPathFromIDList(pidl, cBuf);
>
>It compiles OK, but I get an access violation Exception on the call to
>SHGetSpecialFolderLocation.
>Can anyone suggest why?


No, but maybe you might want to use this code instead:

AnsiString __fastcall GetSpecialPath(int Folder)
{
char Path[MAX_PATH + 1] = { 0 };
if( ::SHGetSpecialFolderPath( NULL, Path, Folder, 0 ) )
return AnsiString( Path );
return "";
}

GetSpecialPath( CSIDL_SENDTO );

Jonathan


Thanks but I've discovered the actual exception is being thrown from the
last line of this code:
IUnknown* Unknown = CreateComObject(CLSID_ShellLink);
IShellLink* ISLink = ((IShellLink*) Unknown );
ISLink->SetPath( _argv[0].c_str());
 

{smallsort}

Re:AccessVialoation on SHGetSpecialFolderLocation

"George Francis" < XXXX@XXXXX.COM >wrote in message
Quote
Thanks but I've discovered the actual exception is being thrown from the
last line of this code:

IUnknown* Unknown = CreateComObject(CLSID_ShellLink);
IShellLink* ISLink = ((IShellLink*) Unknown );
ISLink->SetPath( _argv[0].c_str());
Is that your code ? Because it seems like that last line ought to be:
ISLink->SetPath( _argv[0] );
Jonathan
 

Re:AccessVialoation on SHGetSpecialFolderLocation

Jonathan Benedicto wrote:
Quote
"George Francis" < XXXX@XXXXX.COM >wrote in message
news:42f3e0e2$ XXXX@XXXXX.COM ...

>Thanks but I've discovered the actual exception is being thrown from the
>last line of this code:
>
>IUnknown* Unknown = CreateComObject(CLSID_ShellLink);
>IShellLink* ISLink = ((IShellLink*) Unknown );
>ISLink->SetPath( _argv[0].c_str());


Is that your code ? Because it seems like that last line ought to be:

ISLink->SetPath( _argv[0] );

Jonathan


Its ok Im all sorted now thx.
 

Re:AccessVialoation on SHGetSpecialFolderLocation

"George Francis" < XXXX@XXXXX.COM >wrote in message
Quote
SHGetSpecialFolderLocation(0, CSIDL_SENDTO, &pidl);
SHGetPathFromIDList(pidl, cBuf);
You should be checking the return value of SHGetSpecialFolderLocation()
before calling SHGetPathFromIDList():
LPITEMIDLIST pidl = NULL;
if( SUCCEEDED(SHGetSpecialFolderLocation(0, CSIDL_SENDTO, &pidl)) )
{
char cBuf[MAX_PATH+1] = {0};
SHGetPathFromIDList(pidl, cBuf);
//...
CoTaskMemFree(pidl);
}
Gambit
 

Re:AccessVialoation on SHGetSpecialFolderLocation

"George Francis" < XXXX@XXXXX.COM >wrote in message
Quote
IShellLink* ISLink = ((IShellLink*) Unknown );
That is not the proper way to cast COM interfaces. You *must* use the
QueryInterface() method instead (and don't forget to handle reference
counting) as well:
IUnknown* Unknown = CreateComObject(CLSID_ShellLink);
if( Unknown )
{
IShellLink* ISLink = NULL;
if( SUCCEEDED(Unknown->QueryInterface(IID_IShellLink,
(void**)&ISLink)) )
{
ISLink->SetPath( _argv[0].c_str());
//...
ISLink->Release();
}
Unknown->Release();
}
Which can be wrapped using the DelphiInterface class (which
CreateComObject() uses anyway):
typedef DelphiInterface<IShellLink>_di_IShellLink;
_di_IUnknown Unknown = CreateComObject(CLSID_ShellLink);
if( Unknown )
{
_di_IShellLink ISLink = Unknown;
if( ISLink )
{
ISLink->SetPath( _argv[0].c_str());
//...
// do not call Release() here, DelphiInterface handles that
automatically
}
// do not call Release() here, DelphiInterface handles that
automatically
}
Which can be simplified to the following:
typedef DelphiInterface<IShellLink>_di_IShellLink;
_di_IShellLink ISLink = CreateComObject(CLSID_ShellLink);
if( ISLink )
{
ISLink->SetPath( _argv[0].c_str());
//...
// do not call Release() here, DelphiInterface handles that
automatically
}
Gambit
 

Re:AccessVialoation on SHGetSpecialFolderLocation

"George Francis" < XXXX@XXXXX.COM >wrote in message
Quote
ISLink->SetPath( _argv[0].c_str());
That will not compile, as _argv[0] is not an AnsiString or a std::string.
It is a char*:
ISLink->SetPath( _argv[0] );
Gambit