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
Solved

SHGetFolderPath Causes exception

Posted on 2007-04-11
7
841 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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