Solved

SHGetFolderPath Causes exception

Posted on 2007-04-11
7
850 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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