Solved

SHGetFolderPath Causes exception

Posted on 2007-04-11
7
832 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
  • 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 200 total points
ID: 18900090
two ways:

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

or

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

ziolko.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now