德尔福漫游文件夹,注册表访问



我开发了一个应用程序,我想将其部署在客户端网络的一台机器上。这台机器在Win7 64位下转动,需要管理员授权(他们使用Active Directory,GPO,...(,到目前为止没有问题。我正在使用漫游文件夹来存储一些文件。问题是当我启动应用程序时,它似乎找不到正确的当前用户漫游文件夹路径,我认为这被重定向到管理员漫游文件夹。我的代码如下

Function GetRoamingFolderPath():String;
var
 OsVersion: integer;
 Path: String;
begin
 OsVersion:=(TOSVersion.Major);
if OsVersion < 6 then
 Path:= GetSpecialFolderPath(CSIDL_COMMON_APPDATA)
else
 path:= GetSpecialFolderPath(CSIDL_APPDATA);
end;

其中 GetSpecialFolderPath 定义为:

function GetSpecialFolderPath(folder : integer) : string;
 const   SHGFP_TYPE_CURRENT = 0;
 var path: array [0..MAX_PATH] of char;
begin
 if SUCCEEDED(SHGetFolderPath(0,folder,0,SHGFP_TYPE_CURRENT,@path[0]))      
  then Result := path
else
 Result := '';
end;

我还需要在注册表下注册一些值 HKEY_CURRENT_USER ,它已经完成,但我的应用程序无法访问它们!

关于如何解决这两个问题的任何想法。谢谢。

Function GetRoamingFolderPath():String;
var
 OsVersion: integer;
 Path: String;
begin
 OsVersion:=(TOSVersion.Major);
if OsVersion < 6 then
 Path:= GetSpecialFolderPath(CSIDL_COMMON_APPDATA)
else
 path:= GetSpecialFolderPath(CSIDL_APPDATA);
end;

此函数分配给局部变量 path ,但不分配返回值。因此,它的返回值是未定义的。删除变量path并改为分配给Result

function GetRoamingFolderPath: string;
begin
  if TOSVersion.Major < 6 then
    Result := GetSpecialFolderPath(CSIDL_COMMON_APPDATA)
  else
    Result := GetSpecialFolderPath(CSIDL_APPDATA);
end;

如果您在调试器下单步执行代码并检查中间值,这将很明显。您会观察到GetSpecialFolderPath返回了所需的值,但它在GetRoamingFolderPath中丢失了。一旦你做了这个观察,就会很明显错误是什么。我敦促您将来遇到此类问题时进行这样的调试。

最新更新