LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

A better way to get AppData location?

Solved!
Go to solution

To conform better with Vista I have taken to retrieving the "AppData" location in the registry in order to determine a safe place for my app to write files. The code I use is like this:

err = RegReadString (REGKEY_HKCU, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "AppData", appdatapath, PATHLEN, &keysize);

 

By and large, this works but I have had a few reports that it does not - where the AppData registry entry is corrupt or invalid.  How that arises I have no idea.

 

But, I'm intrigued by a key in that location on my Vista machine. The key is "!Do not use this registry key" and the value is "Use the SHGetFolderPath or SHGetKnownFolderPath function instead".  Looks like a pretty good clue! 

 

Can anyone tell me whether these SDK functions will solve my problem, or how they might differ from simply reading the registry string?  Does anyone here use them?

 

Thanks,

Ian

0 Kudos
Message 1 of 18
(12,150 Views)
Solution
Accepted by Ian.W

 

Hello Ian,

 

I'm not really sure why that registry location works some times but not others, but Microsoft definitely wants us to use the function and not the registry.

 

Note that SHGetKnownFolderPath is too new to be included in the version of the Windows SDK that comes with CVI, but you can definitely use  SHGetFolderPath which works just as well, in Vista and in XP/2000:

 

#include <shlobj.h>
...

char path[MAX_PATHNAME_LEN];
 

SHGetFolderPath (0, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);

 

(you'll have to add shell32.lib to your CVI project).

 

CSIDL_COMMON_APPDATA gives you the common, "all users" folder. If you want the per-user folder, you can use CSIDL_APPDATA instead.

 

Luis

Message 2 of 18
(12,154 Views)

I forgot to mention that you'll need to have the full SDK installed in order to use shell32.lib. It doesn't come with the base SDK.

 

Luis

0 Kudos
Message 3 of 18
(12,154 Views)

Thanks, Luis! 

 

I have created a diagnostic for problem users to run. It reports the results of querying the registry, the sdk function, and the environment variable. I'll post back with results.

 

Any insights from people on how those three methods could end up being different very much appreciated.

 

--Ian

0 Kudos
Message 4 of 18
(12,154 Views)

Final update: The SDK call cleared up all the issues we were seeing. Definitely the way to go.

 

--Ian

0 Kudos
Message 5 of 18
(11,653 Views)

a bit off-topic but here is a little thought i want to share: there is a lot of fuss around the net about reading/writing to the registry and vista that broke everything, etc.

 

generally, microsoft do provide a function for retrieving specific settings of the OS. there is no need to try to hack around the registry: it is only a place used by the system to store some informations, its content and organization is subject to change without notice, just because it is generally not meant to be accessed directly. whenever possible, please try to find the SDK function which performs what you want instead of messing around in the registry.

 

the standard shell folders is one example, another example is the current hardware configuration of the system, and there is a lot more places in the registry where you definitely do not want to read or write.

0 Kudos
Message 6 of 18
(11,632 Views)
Are there any plans to provide this functionality through CVI library functions so we don't have to go through the SDK?  It seems to that this is going to be functionality that is commonly needed for any applications that are going to be deployed on Windows Vista or 7.
0 Kudos
Message 7 of 18
(10,843 Views)

There are no immediate plans, but that's not a bad idea. We'll put that idea in our roadmap and hopefully we can include it in an upcoming CVI release.

 

Thanks for the suggestion, it's a good one.

 

Luis

National Instruments

0 Kudos
Message 8 of 18
(10,796 Views)

I'm using the full development system of LabWindows/CVI 2009 and do not see shell32.lib in C:\Program Files\National Instruments\CVI2009\sdk\lib\msvc.  Have I failed to install something?  I have 16 other lib files in that path.  Also, any word on if/when SHGetFolderPath and/or SHGetKnownFolderPath would be implemented in the CVI library?

0 Kudos
Message 9 of 18
(10,337 Views)

You have to select that feature when you run the installer. You can still re-run the installer and then select that feature:

 

18299iD4F9D5512C85E2B7

0 Kudos
Message 10 of 18
(10,313 Views)