?
Solved

SHGetFolderPath Causes exception

Posted on 2007-04-11
7
Medium Priority
?
852 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
Independent Software Vendors: 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 800 total points
ID: 18900090
two ways:

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

or

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

ziolko.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

762 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