Windows开机自动运行的应用程序(10分)

  • 主题发起人 主题发起人 pllink_qyd
  • 开始时间 开始时间
P

pllink_qyd

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject); <br>begin <br>SetRegistryData(HKEY_LOCAL_MACHINE, <br>'Software/Microsoft/Windows/CurrentVersion/Run', <br>Application.Title, rdString, Application.ExeName); <br>end; <br>誰能告訴我,SetRegistryData要引用那個unit阿?我編譯不過。。THS
 
還有rdstring是什麼參數阿。。大家幫幫忙阿。。別隻看不說話阿。。
 
不是哪个单元的,是自己写的,封装了Registry单元的函数.
 
uses Registry, Windows, SysUtils;<br><br>procedure SetRegistryData(RootKey: HKEY; Key, Value: string;<br> &nbsp;RegDataType: TRegDataType; Data: variant);<br>var<br> &nbsp;Reg: TRegistry;<br> &nbsp;s: string;<br>begin<br> &nbsp;Reg := TRegistry.Create(KEY_WRITE);<br> &nbsp;try<br> &nbsp; &nbsp;Reg.RootKey := RootKey;<br> &nbsp; &nbsp;if Reg.OpenKey(Key, True) then begin<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;if RegDataType = rdUnknown then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RegDataType := Reg.GetDataType(Value);<br> &nbsp; &nbsp; &nbsp; &nbsp;if RegDataType = rdString then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reg.WriteString(Value, Data)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if RegDataType = rdExpandString then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reg.WriteExpandString(Value, Data)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if RegDataType = rdInteger then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reg.WriteInteger(Value, Data)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if RegDataType = rdBinary then begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s := Data;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reg.WriteBinaryData(Value, PChar(s)^, Length(s));<br> &nbsp; &nbsp; &nbsp; &nbsp;end else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise Exception.Create(SysErrorMessage(ERROR_CANTWRITE));<br> &nbsp; &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp; &nbsp;Reg.CloseKey;<br> &nbsp; &nbsp; &nbsp; &nbsp;raise;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;Reg.CloseKey;<br> &nbsp; &nbsp;end else<br> &nbsp; &nbsp; &nbsp;raise Exception.Create(SysErrorMessage(GetLastError));<br> &nbsp;finally<br> &nbsp; &nbsp;Reg.Free;<br> &nbsp;end;<br>end;
 
哦。。暈,害我找半天。。那其不是我沒辦法用。誰知道怎麼寫setregistrydata嗎?或者有其他實現的方法也可以阿。。操作注冊表,得到最後的效果就行了。
 
To japhe,<br> &nbsp; THS..
 
{GetRegistryData}<br><br>uses Registry, Windows, SysUtils;<br><br>function GetRegistryData(RootKey: HKEY; Key, Value: string): variant;<br>var<br> &nbsp;Reg: TRegistry;<br> &nbsp;RegDataType: TRegDataType;<br> &nbsp;DataSize, Len: integer;<br> &nbsp;s: string;<br>label cantread;<br>begin<br> &nbsp;Reg := nil;<br> &nbsp;try<br> &nbsp; &nbsp;Reg := TRegistry.Create(KEY_QUERY_VALUE);<br> &nbsp; &nbsp;Reg.RootKey := RootKey;<br> &nbsp; &nbsp;if Reg.OpenKeyReadOnly(Key) then begin<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;RegDataType := Reg.GetDataType(Value);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (RegDataType = rdString) or<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (RegDataType = rdExpandString) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := Reg.ReadString(Value)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if RegDataType = rdInteger then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := Reg.ReadInteger(Value)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if RegDataType = rdBinary then begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataSize := Reg.GetDataSize(Value);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if DataSize = -1 then goto cantread;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetLength(s, DataSize);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Len := Reg.ReadBinaryData(Value, PChar(s)^, DataSize);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Len &lt;&gt; DataSize then goto cantread;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := s;<br> &nbsp; &nbsp; &nbsp; &nbsp;end else<br>cantread:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise Exception.Create(SysErrorMessage(ERROR_CANTREAD));<br> &nbsp; &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp; &nbsp;s := ''; // Deallocates memory if allocated<br> &nbsp; &nbsp; &nbsp; &nbsp;Reg.CloseKey;<br> &nbsp; &nbsp; &nbsp; &nbsp;raise;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;Reg.CloseKey;<br> &nbsp; &nbsp;end else<br> &nbsp; &nbsp; &nbsp;raise Exception.Create(SysErrorMessage(GetLastError));<br> &nbsp;except<br> &nbsp; &nbsp;Reg.Free;<br> &nbsp; &nbsp;raise;<br> &nbsp;end;<br> &nbsp;Reg.Free;<br>end;<br>Llamada de ejemplo<br><br>ShowMessage(GetRegistryData(HKEY_LOCAL_MACHINE,<br> &nbsp;'/Hardware/Description/System/CentralProcessor/0', 'Identifier'));<br><br>{SetRegistryData}<br><br>uses Registry, Windows, SysUtils;<br><br>procedure SetRegistryData(RootKey: HKEY; Key, Value: string;<br> &nbsp;RegDataType: TRegDataType; Data: variant);<br>var<br> &nbsp;Reg: TRegistry;<br> &nbsp;s: string;<br>begin<br> &nbsp;Reg := TRegistry.Create(KEY_WRITE);<br> &nbsp;try<br> &nbsp; &nbsp;Reg.RootKey := RootKey;<br> &nbsp; &nbsp;if Reg.OpenKey(Key, True) then begin<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;if RegDataType = rdUnknown then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RegDataType := Reg.GetDataType(Value);<br> &nbsp; &nbsp; &nbsp; &nbsp;if RegDataType = rdString then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reg.WriteString(Value, Data)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if RegDataType = rdExpandString then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reg.WriteExpandString(Value, Data)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if RegDataType = rdInteger then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reg.WriteInteger(Value, Data)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if RegDataType = rdBinary then begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s := Data;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Reg.WriteBinaryData(Value, PChar(s)^, Length(s));<br> &nbsp; &nbsp; &nbsp; &nbsp;end else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise Exception.Create(SysErrorMessage(ERROR_CANTWRITE));<br> &nbsp; &nbsp; &nbsp;except<br> &nbsp; &nbsp; &nbsp; &nbsp;Reg.CloseKey;<br> &nbsp; &nbsp; &nbsp; &nbsp;raise;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;Reg.CloseKey;<br> &nbsp; &nbsp;end else<br> &nbsp; &nbsp; &nbsp;raise Exception.Create(SysErrorMessage(GetLastError));<br> &nbsp;finally<br> &nbsp; &nbsp;Reg.Free;<br> &nbsp;end;<br>end;
 
后退
顶部