呼叫Regsavekey終於可儲存指定之subkey為一檔案而不是空檔 |
答題得分者是:qoo1234
|
ry_lee
高階會員 發表:368 回覆:251 積分:123 註冊:2002-03-19 發送簡訊給我 |
在win 2000下以前呼叫Regsavekey總是存一空檔,即什麼也沒有之零byte之檔案,在大陸大富翁網站非常多人提到這問題,也有非常多人想掌握自己windows registry被寫入什麼,將他們程式碼拿來測試也總是失敗(http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20251935.html),直到參考微軟網站之範例(是c語言寫的,http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q128731&)後稍微修改終於可儲存指定之subkey為一檔案,只是執行時會有一訊息(project1.exe raised exception class Eacess violation with message 'Access violation at address 00403706 in module ' project1.exe '.Read of address FFFFFFD1'. process stopped. Use step or Run to contiune.),下面是我的程式碼,希望知道這問題能予指教?
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,registry; type TForm1 = class(TForm) Button2: TButton; procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button2Click(Sender: TObject); var tpNew: TTokenPrivileges; tpPrev: PTokenPrivileges; wReturn: DWord; //tp:TOKEN_PRIVILEGES; hToken:THANDLE ; //aluid: luid; aluid:TLargeInteger; vret:integer; keycode:hkey; disposition:integer; begin wReturn := sizeof(PTOKENPRIVILEGES); if not (OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken )) then begin showmessage('OpenProcessToken fail'); exit; end; if not LookupPrivilegeValue(nil, 'SeBackupPrivilege',aluid) then begin showmessage(' *** LookupPrivilegeValue fail'); exit; end; tpnew.PrivilegeCount:= 1; tpnew.Privileges[0].Luid:= aluid; tpnew.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; if not AdjustTokenPrivileges(hToken, FALSE, tpnew, sizeof(TTokenPrivileges),tpPrev, wReturn ) then begin //有兩個 AdjustTokenPrivileges(TokenHandle: THandle; DisableAllPrivileges: BOOL;const NewState: TTokenPrivileges; BufferLength: DWORD;var PreviousState: TTokenPrivileges; var ReturnLength: DWORD): BOOL; stdcall; overload; showmessage(' *** AdjustTokenPrivileges fail'); exit; end; vret:=regcreatekeyex(HKEY_LOCAL_MACHINE,'system',0,nil,REG_OPTION_BACKUP_RESTORE,KEY_QUERY_VALUE,nil,keycode,@disposition); //ps:這裡不用regcreatekeyex用RegopenkeyEx會失敗,即無存一檔案 if vret <> error_success then begin showmessage('regcreatekeyex fail '); exit; end; vret:=RegSaveKey(Keycode, 'c:\wutemp\system.reg', nil); if vret <> error_success then begin showmessage('RegSaveKey fail '); //exit; end; RegCloseKey(Keycode); // Revoke all privileges this process holds (including backup) AdjustTokenPrivileges( hToken, TRUE,tpNew, sizeof(TTOKENPRIVILEGES), tpPrev, wReturn); // close handle to process token CloseHandle(hToken); end; end. |
qoo1234
版主 發表:256 回覆:1167 積分:659 註冊:2003-02-24 發送簡訊給我 |
|
ry_lee
高階會員 發表:368 回覆:251 積分:123 註冊:2002-03-19 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |