Folder creation under <ApplicationData>

S

Sankalp

Hi,
I have a excel add-in that needs to create/copy some files under the
user specific profile. (i.e under C:\Documents and Settings\<current user
profile>\Application Data\<CosName>\Excel\).

The SHGetSpecialFolderLocation and SHGetPathFromIDList are being used to
get the path of user's profile. To this path is appended the the
"CompanyName" and then the "ApplicationName".
Subsequently, the SHCreateDirectory is used to create the necessary
directory. It is here the call fails.

Is it right to use the SHxxxx API's for this purpose? If not, what is
the alternative?

Following is the code snippet:

#include <Shlobj.h>
#include <Shfolder.h>

int main(int argc, char* argv[]) {

LPITEMIDLIST ppidl;
char lpPath[MAX_PATH];
char lpFilePath[MAX_PATH];

HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &ppidl);
if (SUCCEEDED(hr)) {
SHGetPathFromIDList( ppidl, lpPath );
printf( "Application Data: %s\n\n", lpPath );
}
::CoTaskMemFree(ppidl);

strcat(lpPath, "\\CoName\\Excel\\");
printf( "Application Data: %s\n\n", lpPath );

int iStatus = ::SHCreateDirectory(NULL, (LPCWSTR)lpPath); // returns 161 -
ERROR_BAD_PATHNAME
if (ERROR_SUCCESS == iStatus ||
ERROR_FILE_EXISTS == iStatus ||
ERROR_ALREADY_EXISTS == iStatus) {
sprintf(lpFilePath, "%s\\config.dat", lpPath);
printf( "Config file path: %s\n\n", lpFilePath );
// Copy only if the file does NOT exist.
//CopyFile(configfile, lpPath, TRUE);
}

return 0;
}

Thanks,
Sankalp
 
S

Sankalp

Update - SHCreateDirectoryEx works fine, but am still not sure if the SHxxx
API's are the ones that need to be used.
Is there some ATL function that is recommended over the SHxxx API's?

Thanks
Sankalp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top