请教下各位前辈,如何判断注册表中的某个键值存在,如果不存在就建立一个???拿这个注册表为例子:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters]
"AutoShareServer"=dword:00000000
"AutoSharewks"=dword:00000000
利用winexec调用CMD来执行命令,如何让结果显示在MEMO1上?
如,NET USER 所返回的用户的结果显示在MEMO1请教前辈了....谢谢

解决方案 »

  1.   

    delphi有TRegistry类比如var
       reg:TRegistry;
    begin
    reg:=TRegistry.Create;
    reg.RootKey:=HKEY_LOCAL_MACHINE;
    if reg.OpenKey('SOFTWARE\ODBC\ODBCINST.INI\Sybase System 11\',false) then
       begin
       str:=reg.ReadString('Driver');
       end
    else
       begin
       result:=false;
       reg.Free;
       exit;
       end;reg.CloseKey;
    end;
      

  2.   

        try
          Reg.RootKey :=HKEY_LOCAL_MACHINE;
          if Reg.OpenKey('SYSTEM\CurrentControlSet\Services\lanmanserver\parameters',True) then
          begin
            Reg.WriteInteger('AutoShareServer',00000000);
            Reg.WriteInteger('AutoSharewks',00000000);
          end;
        finally
          Reg.CloseKey;
          Reg.Free;
        end;
      

  3.   

    谢谢楼上两位~~
    但是楼上两位都只是判断,而不存在就建立的操作偶杂没看到......
    还有关于调用CMD显示在MEMO的期待高手回答...100分~~~如果都回答的这么好,就平均了......期待更好的
      

  4.   

    delphi的help里面搜索TRegistry
    下面会列出所有的方法,比如createkey,也有例子可以参考
      

  5.   

    2楼的OpenKey方法第二个参数为True就是不存在建立。
      

  6.   

    CMD输出到Memo的函数function GetDosOutput(const ACommandLine: string; const AWorkDir: string): string;
    var
      SA: TSecurityAttributes;
      SI: TStartupInfo;
      PI: TProcessInformation;
      StdOutPipeRead, StdOutPipeWrite: THandle;
      WasOK: Boolean;
      Buffer: array[0..255] of Char;
      BytesRead: Cardinal;
      WorkDir, Line: String;
    begin
      with SA do
      begin
        nLength := SizeOf(SA);
        bInheritHandle := True;
        lpSecurityDescriptor := nil;
      end;
      // create pipe for standard output redirection
      CreatePipe(StdOutPipeRead,  // read handle
                 StdOutPipeWrite, // write handle
                 @SA,             // security attributes
                 0                // number of bytes reserved for pipe - 0 default
                 );
      try
        // Make child process use StdOutPipeWrite as standard out,
        // and make sure it does not show on screen.
        with SI do
        begin
          FillChar(SI, SizeOf(SI), 0);
          cb := SizeOf(SI);
          dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
          wShowWindow := SW_HIDE;
          hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdinput
          hStdOutput := StdOutPipeWrite;
          hStdError := StdOutPipeWrite;
        end;
        // launch the command line compiler
        WorkDir := AWorkDir;
        WasOK := CreateProcess(nil, PChar(ACommandLine), nil, nil, True, 0, nil,
                               nil, SI, PI);
        // Now that the handle has been inherited, close write to be safe.
        // We don't want to read or write to it accidentally.
        CloseHandle(StdOutPipeWrite);
        // if process could be created then handle its output
        if not WasOK then
          raise Exception.Create('Could not execute command line!')
        else
          try
            // get all output until dos app finishes
            Line := '';
            repeat
              // read block of characters (might contain carriage returns and line feeds)
              WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);          // has anything been read?
              if BytesRead > 0 then
              begin
                // finish buffer to PChar
                Buffer[BytesRead] := #0;
                // combine the buffer with the rest of the last run
                Line := Line + Buffer;
              end;
            until not WasOK or (BytesRead = 0);
            // wait for console app to finish (should be already at this point)
            WaitForSingleObject(PI.hProcess, INFINITE);
          finally
            // Close all remaining handles
            CloseHandle(PI.hThread);
            CloseHandle(PI.hProcess);
          end;
      finally
        Result := Line;
        CloseHandle(StdOutPipeRead);
      end;
    end;
    调用时procedure FillMemo;
    var
      Command: string;
    begin
      Command := 'Net User';
      Memo1.text := GetDosOutput(Command, '');
    end;
      

  7.   

    创建注册表项procedure CreateParam;
    var
     regTemp: TRegistry;
    begin
      regTemp := TRegistry.Create;
      with regTemp do
      begin
        RootKey := HKEY_LOCAL_MACHINE;
        if not OpenKey('SYSTEM\CurrentControlSet\Services\lanmanserver\parameters', False) then
        begin
          CloseKey;
          if CreateKey('\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters') then
          begin
            CloseKey;
            if OpenKey('SYSTEM\CurrentControlSet\Services\lanmanserver\parameters', True) then
            begin
              Reg.WriteInteger('AutoShareServer',00000000); 
              Reg.WriteInteger('AutoSharewks',00000000); 
              CloseKey;
            end;
          end;
        end;
      end;
      regTemp.Free;
    end;
      

  8.   

    用OpenKey('',Boolean),這裡的Boolean設為true或false就可以了
    Lz可以用看看幫助的啊,就很清楚了