File: AdminService.pas

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (273 lines) | stat: -rw-r--r-- 9,084 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
unit AdminService;

interface

uses Classes, ComCtrls, Registry, Windows, SysUtils, StrUtils, WinSvc,
  myx_admin_public_interface, AuxFuncs, Contnrs, TntRegistry, TntComCtrls,
  TntClasses;

const
  ServicesImageIndex = 25;

type
  TMySQLService = class(TObject)
  public
    ExistingService: Boolean;
    StartType: integer;
    ServiceName: WideString;
    DisplayName: WideString;
    Description: WideString;
    ConfigFile: WideString;
    ConfigFileSection: WideString;
    Binary: WideString;
    PathToBinary: WideString;
    Status: Integer;
    ErrorLog: WideString;
    logfile_size: Longword;

    constructor Create; virtual;
    destructor Destroy; override;
  end;

procedure ScanForServices(TreeView: TTntTreeView; Icon: Integer; ObjectList: TObjectList = nil);
function SelectService(TreeView: TTntTreeView; const ServiceName: WideString): Integer;

//----------------------------------------------------------------------------------------------------------------------

implementation

//----------------------------------------------------------------------------------------------------------------------

procedure ScanForServices(TreeView: TTntTreeView; Icon: integer; ObjectList: TObjectList);

var
  ServiceReg: TTntRegistry;
  KeyInfo: TRegKeyInfo;
  SubKeyNames: TTntStringList;
  i,j: integer;
  ImagePath, s, s1: WideString;
  tmp: WideString;
  tokenList: TTntStringList;
  MySQLService: TMySQLService;
  errornr: MYX_ADMIN_LIB_ERROR;
  errorlog: WideString;

begin
  ServiceReg:=TTntRegistry.Create;
  SubKeyNames:=TTntStringList.Create;
  try
    ServiceReg.RootKey := HKEY_LOCAL_MACHINE;
    ServiceReg.Access := KEY_ENUMERATE_SUB_KEYS + KEY_QUERY_VALUE;

    ServiceReg.OpenKey('SYSTEM\CurrentControlSet\Services\', False);

    ServiceReg.GetKeyInfo(KeyInfo);
    ServiceReg.GetKeyNames(SubKeyNames);

    ServiceReg.CloseKey;

    for i:=0 to SubKeyNames.Count-1 do
    begin
      ServiceReg.OpenKey('SYSTEM\'+
        'CurrentControlSet\Services\'+SubKeyNames[i], False);

      try
        ImagePath:=ServiceReg.ReadString('ImagePath');
      except
        ImagePath:='';
      end;

      //Check if mysqld is in ImagePath
      if(Pos('mysqld', LowerCase(ImagePath))>0)then
      begin
        MySQLService:=TMySQLService.Create;
        MySQLService.ExistingService:=True;
        //ServiceList.Add(MySQLService);

        MySQLService.ServiceName:=SubKeyNames[i];
        //ReadString returns an exception if  the registry entry contains
        //something other than a string
        try
          MySQLService.DisplayName:=ServiceReg.ReadString('DisplayName');
        except
          MySQLService.DisplayName:= '';
        end;
        try
          MySQLService.Description:=ServiceReg.ReadString('Description');
        except
          MySQLService.Description:= '';
        end;

        try
          MySQLService.StartType:=ServiceReg.ReadInteger('Start');
        except
          MySQLService.StartType:=3;
        end;

        // Typical ImagePath:
        // c:\programme\mysql\bin\mysqld-max-nt --defaults-file="C:\Programme\MySQL\my.cnf" MySQL
        // c:\program files\mysql\bin\mysqld-max-nt --defaults-file="C:\Programme\MySQL\my.cnf" MySQL

        // Parse the image path.
        MySQLService.ConfigFile := '';
        MySQLService.ConfigFileSection := '';

        tokenList  :=  TTntStringList.Create();
        try
          ParseCommandLine(ImagePath, tokenList);

          for j := 0 to tokenList.count-1 do
          begin
            if (j=0) then
            begin
              tmp :=  AnsiDequotedStr(tokenList[j], '"');
              MySQLService.PathToBinary := ExtractFilePath(tmp);
              MySQLService.Binary := Trim(ExtractFileName(tmp));
              if(CompareText(Copy(MySQLService.Binary, Length(MySQLService.Binary)-3, 4),
                '.exe')=0)then
                MySQLService.Binary := Copy(MySQLService.Binary, 1, Length(MySQLService.Binary)-4);
            end
            else if (AnsiStartsStr('--defaults-file=', tokenList[j])) then
            begin
              tmp :=  Copy(tokenList[j],Length('--defaults-file=')+1,
                         Length(tokenList[j])-Length('--defaults-file='));
              MySQLService.ConfigFile :=  AnsiDequotedStr(tmp, '"');
            end
            else
              if not AnsiStartsStr('--', tokenList[j]) then
              with MySQLService do
              begin
                ConfigFileSection :=  AnsiDequotedStr(tokenList[j], '"');

                // Sanity check: make sure we don't read from [client] or [mysql].
                if WideSameText(ConfigFileSection, 'mysql') or WideSameText(ConfigFileSection, 'client') then
                  ConfigFileSection := 'mysqld';
              end;
          end;
        finally
          tokenList.Free();
        end;

        //if the ConfigFile isn't specified in the Registry
        //it can be c:\my.cnf or windows-directory\my.ini
        if(MySQLService.ConfigFile='')then
        begin
          s := GetWindowsDir;

          if(FileExists(s+'my.ini'))then
            MySQLService.ConfigFile := s+'my.ini'
          else if(FileExists('c:\my.cnf'))then
            MySQLService.ConfigFile := 'c:\my.cnf'
          else
          begin
            //If there is no default cnf file yet,
            //use PathToBinaries\my.cnf
            s1 := IncludeTrailingPathDelimiter(
              Copy(MySQLService.PathToBinary, 1, Pos('\bin', MySQLService.PathToBinary)-1));
            if(s<>'')then
              MySQLService.ConfigFile := s1+'my.cnf'
            else
              MySQLService.ConfigFile := s+'my.ini';
          end;
        end;

        //if the ConfigFileSection isn't specified in the Registry
        //it is mysqld
        if(MySQLService.ConfigFileSection='')then
          MySQLService.ConfigFileSection := 'mysqld';


        //-----------------------------------------------------------------
        //Try to find errorfile
        MySQLService.ErrorLog := '';

        //first see if error-log is specified in the configfile
        if ( (MySQLService.ConfigFile<> '') and fileExists(Mysqlservice.ConfigFile) ) then
        begin
          ErrorLog := myx_get_cnf_value(MySQLService.ConfigFile,
                                       mysqlservice.configfilesection,
                                       'log-error', @errornr);
          if (errorlog <> '') then
            MySQLService.ErrorLog :=  errorlog;
        end;

        //secondly make some guesses
        if (MySQLService.ConfigFile = '') then
        begin
          s := GetLocalHostName;


          if(FileExists(GetProgramFilesDir+'MySQL\data\'+s+'.err'))then
            MySQLService.ErrorLog := GetProgramFilesDir+'MySQL\data\'+s+'.err'
          else if(FileExists('C:\MySQL\data\'+s+'.err'))then
            MySQLService.ErrorLog := 'C:\MySQL\data\'+s+'.err'
          else if(FileExists('D:\MySQL\data\'+s+'.err'))then
            MySQLService.ErrorLog := 'D:\MySQL\data\'+s+'.err';
        end;
        MySQLService.logfile_size := 0;

        //-----------------------------------------------------------------
        //Get Service Status
        MySQLService.Status := ServiceStatus('', MySQLService.ServiceName);

        if(TreeView<>nil)then
          AddTreeViewChildNode(TreeView, nil, MySQLService.ServiceName,
            Icon+Ord(MySQLService.Status<>SERVICE_RUNNING), MySQLService);

        if(ObjectList<>nil)then
          ObjectList.Add(MySQLService);
      end;

      ServiceReg.CloseKey;
    end;

  finally
    SubKeyNames.Free;
    ServiceReg.Free;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

function SelectService(TreeView: TTntTreeView; const ServiceName: WideString): Integer;

// Select the node in the given treeview which corresponds to the given service name.
// Result is the index of the service entry or -1 if not found.
 
var
  I: Integer;

begin
  Result := -1;
  with TreeView do
  begin
    for I := 0 to Items.Count - 1 do
      if Assigned(Items[I].Data) then
        if WideSameText(ServiceName, TMySQLService(Items[I].Data).ServiceName) then
        begin
          Result := I;
          Selected := Items[I];
          Break;
        end;
  end;
end;

//----------------- TMySQLService --------------------------------------------------------------------------------------

constructor TMySQLService.Create;

begin
  // For debugging.
end;

//----------------------------------------------------------------------------------------------------------------------

destructor TMySQLService.Destroy;

begin
  inherited;
end;

//----------------------------------------------------------------------------------------------------------------------

end.