Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SHGetFolderPath Causes exception

Posted on 2007-04-11
7
Medium Priority
?
853 Views
Last Modified: 2012-06-27
Hi there,

In an attempt to make my software more Vista compatable, I am using SHGetFolderPath to get the AppData folder so I can then create a subfolder and write to it.

Now, my application has two forms, on the first form I have this in the oncreate method in the first form.


  If SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, P) = S_OK then
      gbSavePath := P +  '\MySubFolder\';
  else
    gbSavePath := GetCurrentDir;

yes I have added SHFolder to the uses clause, and the oncreate method on the second form is blank.

now, i know that gbSavePath gets set with the correct location, but when my secord for is created - an exception occurs.
I comment that code above, and everything runs fine.

Confused!
0
Comment
Question by:wildzero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 10

Author Comment

by:wildzero
ID: 18888564
However doing this is fine
const
   SHGFP_TYPE_CURRENT = 0;
var
  path: array [0..255] of char;
begin
  If SHGetFolderPath(0,CSIDL_APPDATA,0,SHGFP_TYPE_CURRENT,@path[0]) = S_OK then
      gbSavePath := Path +  '\MySubFolder\'
  else
    gbSavePath := GetCurrentDir;

With no worries
hmmm
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18891947
If SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, P) = S_OK then
      gbSavePath := P +  '\MySubFolder\';

what type is variable: P?

ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18891959
...and more important how P is initialized?
ziolko.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Author Comment

by:wildzero
ID: 18895184
var
P : Pchar;
begin
sorry :-)
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18896429
if you use PChar you need to allocate it
so it have enough space to store path and  '\MySubFolder\'
and then remember to deallocate it's a lot easier to use it
like in second sample witch array of char, using array
is very common with Windows API and null terminated strings

ziolko.
0
 
LVL 10

Author Comment

by:wildzero
ID: 18897218
if you use PChar you need to allocate it

Care to show how :-)
Points upped
0
 
LVL 21

Accepted Solution

by:
ziolko earned 800 total points
ID: 18900090
two ways:

GetMem(P, ...)
FreeMem(P)

or

P := StrAlloc(...)
StrDispose(P)

ziolko.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question