我取出注册表中HKEY_USERS\.DEFAULT\Keyboard Layout\Preload下1的值,为00000804我把这个值作为参数,准备再取HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layouts\00000804中Layout Text的值。可是程序出错了。程序如下:
var
hKey1:HKEY;
size:cardinal;
value,value3:array[0..255]of char;
str:string;
begin
RegOpenKeyEx(HKEY_USERS,'.DEFAULT\Keyboard Layout\Preload',0,KEY_READ,HKey1);
RegQueryValueEx(HKey1,'1',nil,nil,@value,@size);
RegCloseKey(HKey1);
str:='SYSTEM\CurrentControlSet\Control\Keyboard Layouts\'+string(value);
p:=pchar(str);
regOpenKeyEx(HKEY_LOCAL_MACHINE,p,0,KEY_READ,HKey1);
RegQueryValueEx(HKey1,'Layout Text',nil,nil,@value3,@size);
memo7.Lines.Add(value3);
regclosekey(HKey1);
end;
可是当我直接读取HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layouts\00000804的值的时候,又没有什么问题,代码如下:
regOpenKeyEx(HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Control\Keyboard Layouts\00000804',0,KEY_READ,HKey1);
RegQueryValueEx(HKey1,'Layout Text',nil,nil,@value3,@size);
memo7.Lines.Add(value3);
regclosekey(HKey1);
我比较过我取得的value的值,和‘00000804’是一样的,只不过用变量转换了一下,怎么就读取不出来了,太奇怪了!请高手指点。

解决方案 »

  1.   

    其实是因为你的size没有初始化。如果不够大的话,会返回一个错误。
    lpcbDataPoints to a variable that specifies the size, in bytes, of the buffer pointed to by the lpData parameter. When the function returns, this variable contains the size of the data copied to 
    lpData. If the buffer specified by lpData parameter is not large enough to hold the data, the function returns the value ERROR_MORE_DATA, and stores the required buffer size, in bytes, into the variable pointed to by lpcbData. 
    If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS, and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This lets an application determine the best way to allocate a buffer for the value key's data. If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, then lpData will also include the size of the terminating null character. 
    The lpcbData parameter can be NULL only if lpData is NULL. 
    程序修改为下面即可:
    ==========================
    begin
    size:=255;RegOpenKeyEx(HKEY_USERS,'.DEFAULT\Keyboard Layout\Preload',0,KEY_READ,HKey1);
    RegQueryValueEx(HKey1,'1',nil,nil,@value,@size);
    RegCloseKey(HKey1);
    str:='SYSTEM\CurrentControlSet\Control\Keyboard Layouts\'+string(value);
    p:=pchar(str);size:=255;
    regOpenKeyEx(HKEY_LOCAL_MACHINE,p,0,KEY_READ,HKey1);
    RegQueryValueEx(HKey1,'Layout Text',nil,nil,@value3,@size);
    memo7.Lines.Add(value3);
    regclosekey(HKey1);end;
      

  2.   

    没有初始化的时候,往往size是一个很大的值,所以看不出错误!