|
| 1 | +unit uWmiVBNetCode; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + uWmiGenCode, |
| 7 | + Classes; |
| 8 | + |
| 9 | + |
| 10 | +type |
| 11 | + TCVBNetWmiClassCodeGenerator=class(TWmiClassCodeGenerator) |
| 12 | + public |
| 13 | + procedure GenerateCode(Props: TStrings);override; |
| 14 | + end; |
| 15 | + |
| 16 | + TCVBNetWmiEventCodeGenerator=class(TWmiEventCodeGenerator) |
| 17 | + public |
| 18 | + procedure GenerateCode(ParamsIn, Values, Conds, PropsOut: TStrings);override; |
| 19 | + end; |
| 20 | + |
| 21 | + TCVBNetWmiMethodCodeGenerator=class(TWmiMethodCodeGenerator) |
| 22 | + private |
| 23 | + function GetWmiClassDescription: string; |
| 24 | + public |
| 25 | + procedure GenerateCode(ParamsIn, Values: TStrings);override; |
| 26 | + end; |
| 27 | + |
| 28 | +implementation |
| 29 | + |
| 30 | +uses |
| 31 | + uSelectCompilerVersion, |
| 32 | + StrUtils, |
| 33 | + IOUtils, |
| 34 | + ComObj, |
| 35 | + uWmi_Metadata, |
| 36 | + SysUtils; |
| 37 | + |
| 38 | + |
| 39 | +const |
| 40 | + sTagVBNetCode = '[VBNETCODE]'; |
| 41 | + sTagVBNetEventsWql = '[VBNETEVENTSWQL]'; |
| 42 | + sTagVBNetEventsOut = '[VBNETEVENTSOUT]'; |
| 43 | + sTagVBNetCodeParamsIn = '[VBNETCODEINPARAMS]'; |
| 44 | + sTagVBNetCodeParamsOut = '[VBNETCODEOUTPARAMS]'; |
| 45 | + |
| 46 | +function EscapeVBNetStr(const Value:string) : string; |
| 47 | +const |
| 48 | + ArrChr : Array [0..1] of Char = ('\','"'); |
| 49 | + ArrChrEscape : Array [0..1] of Char = ('\','\'); |
| 50 | +Var |
| 51 | + i : integer; |
| 52 | +begin |
| 53 | + Result:=Value; |
| 54 | + for i:= Low(ArrChr) to High(ArrChr) do |
| 55 | + Result:=StringReplace(Result,ArrChr[i],ArrChrEscape[i]+ArrChr[i], [rfReplaceAll]); |
| 56 | +end; |
| 57 | + |
| 58 | + |
| 59 | +{ TCVBNetWmiClassCodeGenerator } |
| 60 | + |
| 61 | +procedure TCVBNetWmiClassCodeGenerator.GenerateCode(Props: TStrings); |
| 62 | +var |
| 63 | + StrCode: string; |
| 64 | + Descr: string; |
| 65 | + DynCode: TStrings; |
| 66 | + i: integer; |
| 67 | + TemplateCode : string; |
| 68 | + Padding : string; |
| 69 | + CimType:Integer; |
| 70 | + |
| 71 | +begin |
| 72 | + Descr := GetWmiClassDescription; |
| 73 | + |
| 74 | + OutPutCode.BeginUpdate; |
| 75 | + DynCode := TStringList.Create; |
| 76 | + try |
| 77 | + OutPutCode.Clear; |
| 78 | + StrCode := TFile.ReadAllText(GetTemplateLocation(Lng_VBNet, ModeCodeGeneration, TWmiGenCode.WmiClasses)); |
| 79 | + |
| 80 | + StrCode := StringReplace(StrCode, sTagVersionApp, FileVersionStr, [rfReplaceAll]); |
| 81 | + StrCode := StringReplace(StrCode, sTagWmiClassName, WmiClass, [rfReplaceAll]); |
| 82 | + StrCode := StringReplace(StrCode, sTagWmiNameSpace, EscapeVBNetStr(WmiNamespace), [rfReplaceAll]); |
| 83 | + StrCode := StringReplace(StrCode, sTagHelperTemplate, TemplateCode, [rfReplaceAll]); |
| 84 | + Padding:=StringOfChar(' ',20); |
| 85 | + if Props.Count > 0 then |
| 86 | + for i := 0 to Props.Count - 1 do |
| 87 | + begin |
| 88 | + CimType:=Integer(Props.Objects[i]); |
| 89 | + case CimType of |
| 90 | + wbemCimtypeDatetime : |
| 91 | + DynCode.Add(Format( |
| 92 | + Padding+'Console.WriteLine("{0,-35} {1,-40}","%s",ManagementDateTimeConverter.ToDateTime((string)WmiObject("%s")))'' %s', |
| 93 | + [Props.Names[i], Props.Names[i], Props.ValueFromIndex[i]])) |
| 94 | + else |
| 95 | + DynCode.Add(Format( |
| 96 | + Padding+'Console.WriteLine("{0,-35} {1,-40}","%s",WmiObject("%s"))'' %s', |
| 97 | + [Props.Names[i], Props.Names[i], Props.ValueFromIndex[i]])); |
| 98 | + end; |
| 99 | + end; |
| 100 | + |
| 101 | + StrCode := StringReplace(StrCode, sTagVBNetCode, DynCode.Text, [rfReplaceAll]); |
| 102 | + StrCode := StringReplace(StrCode, sTagWmiClassDescr, Descr, [rfReplaceAll]); |
| 103 | + OutPutCode.Text := StrCode; |
| 104 | + finally |
| 105 | + OutPutCode.EndUpdate; |
| 106 | + DynCode.Free; |
| 107 | + end; |
| 108 | +end; |
| 109 | + |
| 110 | + |
| 111 | +{ TCVBNetWmiEventCodeGenerator } |
| 112 | + |
| 113 | +procedure TCVBNetWmiEventCodeGenerator.GenerateCode(ParamsIn, Values, Conds, |
| 114 | + PropsOut: TStrings); |
| 115 | +var |
| 116 | + StrCode: string; |
| 117 | + sValue: string; |
| 118 | + Wql: string; |
| 119 | + i, Len: integer; |
| 120 | + Props: TStrings; |
| 121 | + Padding : string; |
| 122 | + CimType:Integer; |
| 123 | +begin |
| 124 | + Padding := StringOfChar(' ',10); |
| 125 | + StrCode := TFile.ReadAllText(GetTemplateLocation(Lng_VBNet, ModeCodeGeneration, TWmiGenCode.WmiEvents)); |
| 126 | + |
| 127 | + WQL := Format('Select * From %s Within %d ', [WmiClass, PollSeconds]); |
| 128 | + WQL := Format(Padding+StringOfChar(' ',6)+'WmiQuery ="%s"+%s', [WQL, sLineBreak]); |
| 129 | + |
| 130 | + if WmiTargetInstance <> '' then |
| 131 | + begin |
| 132 | + sValue := Format('"Where TargetInstance ISA %s "', [QuotedStr(WmiTargetInstance)]); |
| 133 | + WQL := WQL + Padding+StringOfChar(' ',6) + sValue + '+' + sLineBreak; |
| 134 | + end; |
| 135 | + |
| 136 | + for i := 0 to Conds.Count - 1 do |
| 137 | + begin |
| 138 | + sValue := ''; |
| 139 | + if (i > 0) or ((i = 0) and (WmiTargetInstance <> '')) then |
| 140 | + sValue := 'AND '; |
| 141 | + sValue := sValue + ' ' + ParamsIn.Names[i] + Conds[i] + Values[i] + ' '; |
| 142 | + WQL := WQL + StringOfChar(' ', 8) + QuotedStr(sValue) + '+' + sLineBreak; |
| 143 | + end; |
| 144 | + |
| 145 | + i := LastDelimiter('+', Wql); |
| 146 | + if i > 0 then |
| 147 | + Wql[i] := ';'; |
| 148 | + |
| 149 | + |
| 150 | + Len := GetMaxLengthItem(PropsOut) + 6; |
| 151 | + Props := TStringList.Create; |
| 152 | + try |
| 153 | + for i := 0 to PropsOut.Count - 1 do |
| 154 | + begin |
| 155 | + CimType:=Integer(PropsOut.Objects[i]); |
| 156 | + case CimType of |
| 157 | + wbemCimtypeDatetime : |
| 158 | + if StartsText(wbemTargetInstance,PropsOut[i]) then |
| 159 | + Props.Add(Format(Padding+' Console.WriteLine("%-'+IntToStr(Len)+'s" & ManagementDateTimeConverter.ToDateTime((string)((ManagementBaseObject)e.NewEvent.Properties("%s").Value)("%s"))', |
| 160 | + [PropsOut[i]+' : ',wbemTargetInstance, StringReplace(PropsOut[i],wbemTargetInstance+'.','',[rfReplaceAll])])) |
| 161 | + else |
| 162 | + Props.Add(Format(Padding+' Console.WriteLine("%-'+IntToStr(Len)+'s" & ManagementDateTimeConverter.ToDateTime(e.NewEvent.Properties("%s").Value.ToString()))', [PropsOut[i]+' : ', PropsOut[i]])); |
| 163 | + else |
| 164 | + begin |
| 165 | + if StartsText(wbemTargetInstance,PropsOut[i]) then |
| 166 | + Props.Add(Format(Padding+' Console.WriteLine("%-'+IntToStr(Len)+'s" & ((ManagementBaseObject)e.NewEvent.Properties("%s").Value)("%s")', |
| 167 | + [PropsOut[i]+' : ',wbemTargetInstance, StringReplace(PropsOut[i],wbemTargetInstance+'.','',[rfReplaceAll])])) |
| 168 | + else |
| 169 | + Props.Add(Format(Padding+' Console.WriteLine("%-'+IntToStr(Len)+'s" & e.NewEvent.Properties("%s").Value.ToString())', [PropsOut[i]+' : ', PropsOut[i]])); |
| 170 | + end; |
| 171 | + |
| 172 | + end; |
| 173 | + end; |
| 174 | + |
| 175 | + StrCode := StringReplace(StrCode, sTagVBNetEventsOut, Props.Text, [rfReplaceAll]); |
| 176 | + finally |
| 177 | + props.Free; |
| 178 | + end; |
| 179 | + |
| 180 | + StrCode := StringReplace(StrCode, sTagVersionApp, FileVersionStr, [rfReplaceAll]); |
| 181 | + StrCode := StringReplace(StrCode, sTagVBNetEventsWql, WQL, [rfReplaceAll]); |
| 182 | + StrCode := StringReplace(StrCode, sTagWmiClassName, WmiClass, [rfReplaceAll]); |
| 183 | + StrCode := StringReplace(StrCode, sTagWmiNameSpace, EscapeVBNetStr(WmiNamespace), [rfReplaceAll]); |
| 184 | + OutPutCode.Text := StrCode; |
| 185 | +end; |
| 186 | + |
| 187 | + |
| 188 | +{ TCVBNetWmiMethodCodeGenerator } |
| 189 | + |
| 190 | +procedure TCVBNetWmiMethodCodeGenerator.GenerateCode(ParamsIn, |
| 191 | + Values: TStrings); |
| 192 | +var |
| 193 | + StrCode: string; |
| 194 | + Descr: string; |
| 195 | + DynCodeInParams: TStrings; |
| 196 | + DynCodeOutParams: TStrings; |
| 197 | + i: integer; |
| 198 | + |
| 199 | + IsStatic: boolean; |
| 200 | + TemplateCode : string; |
| 201 | + Padding : string; |
| 202 | +begin |
| 203 | + Padding := stringofchar(' ', 14); |
| 204 | + Descr := GetWmiClassDescription; |
| 205 | + OutPutCode.BeginUpdate; |
| 206 | + DynCodeInParams := TStringList.Create; |
| 207 | + DynCodeOutParams := TStringList.Create; |
| 208 | + try |
| 209 | + IsStatic := WMiClassMetaData.MethodByName[WmiMethod].IsStatic; |
| 210 | + |
| 211 | + OutPutCode.Clear; |
| 212 | + if IsStatic then |
| 213 | + begin |
| 214 | + { |
| 215 | + if UseHelperFunctions then |
| 216 | + TemplateCode := TFile.ReadAllText(GetTemplateLocation(sTemplateTemplateFuncts)) |
| 217 | + else |
| 218 | + TemplateCode:=''; |
| 219 | + } |
| 220 | + StrCode := TFile.ReadAllText(GetTemplateLocation(Lng_VBNet, ModeCodeGeneration, TWmiGenCode.WmiMethodStatic)); |
| 221 | + end |
| 222 | + else |
| 223 | + begin |
| 224 | + { |
| 225 | + if UseHelperFunctions then |
| 226 | + TemplateCode := TFile.ReadAllText(GetTemplateLocation(sTemplateTemplateFuncts)) |
| 227 | + else |
| 228 | + TemplateCode:=''; |
| 229 | + } |
| 230 | + StrCode := TFile.ReadAllText(GetTemplateLocation(Lng_VBNet, ModeCodeGeneration, TWmiGenCode.WmiMethodNonStatic)); |
| 231 | + end; |
| 232 | + |
| 233 | + StrCode := StringReplace(StrCode, sTagVersionApp, FileVersionStr, [rfReplaceAll]); |
| 234 | + StrCode := StringReplace(StrCode, sTagWmiClassName, WmiClass, [rfReplaceAll]); |
| 235 | + StrCode := StringReplace(StrCode, sTagWmiNameSpace, EscapeVBNetStr(WmiNamespace), [rfReplaceAll]); |
| 236 | + StrCode := StringReplace(StrCode, sTagWmiMethodName, WmiMethod, [rfReplaceAll]); |
| 237 | + StrCode := StringReplace(StrCode, sTagWmiPath, EscapeVBNetStr(WmiPath), [rfReplaceAll]); |
| 238 | + StrCode := StringReplace(StrCode, sTagHelperTemplate, TemplateCode, [rfReplaceAll]); |
| 239 | + |
| 240 | + |
| 241 | + //In Params |
| 242 | + if ParamsIn.Count > 0 then |
| 243 | + for i := 0 to ParamsIn.Count - 1 do |
| 244 | + if Values[i] <> WbemEmptyParam then |
| 245 | + if ParamsIn.ValueFromIndex[i] = wbemtypeString then |
| 246 | + DynCodeInParams.Add( |
| 247 | + Format(Padding+' inParams("%s")="%s"', [ParamsIn.Names[i], Values[i]])) |
| 248 | + else |
| 249 | + DynCodeInParams.Add( |
| 250 | + Format(Padding+' inParams("%s")=%s', [ParamsIn.Names[i], Values[i]])); |
| 251 | + StrCode := StringReplace(StrCode, sTagVBNetCodeParamsIn, DynCodeInParams.Text, [rfReplaceAll]); |
| 252 | + |
| 253 | + //Out Params |
| 254 | + if WMiClassMetaData.MethodByName[WmiMethod].OutParameters.Count > 1 then |
| 255 | + begin |
| 256 | + for i := 0 to WMiClassMetaData.MethodByName[WmiMethod].OutParameters.Count - 1 do |
| 257 | + DynCodeOutParams.Add( |
| 258 | + Format(Padding+' Console.WriteLine("{0,-35} {1,-40}","%s",outParams("%s")', |
| 259 | + [WMiClassMetaData.MethodByName[WmiMethod].OutParameters[i].Name, WMiClassMetaData.MethodByName[WmiMethod].OutParameters[i].Name])); |
| 260 | + end |
| 261 | + else |
| 262 | + if WMiClassMetaData.MethodByName[WmiMethod].OutParameters.Count = 1 then |
| 263 | + begin |
| 264 | + DynCodeOutParams.Add( |
| 265 | + Format(Padding+' Console.WriteLine("{0,-35} {1,-40}","Return Value",%s)',['outParams("ReturnValue")'])); |
| 266 | + end; |
| 267 | + |
| 268 | + StrCode := StringReplace(StrCode, sTagVBNetCodeParamsOut, |
| 269 | + DynCodeOutParams.Text, [rfReplaceAll]); |
| 270 | + |
| 271 | + StrCode := StringReplace(StrCode, sTagWmiMethodDescr, Descr, [rfReplaceAll]); |
| 272 | + OutPutCode.Text := StrCode; |
| 273 | + finally |
| 274 | + OutPutCode.EndUpdate; |
| 275 | + DynCodeInParams.Free; |
| 276 | + DynCodeOutParams.Free; |
| 277 | + end; |
| 278 | +end; |
| 279 | + |
| 280 | + |
| 281 | +function TCVBNetWmiMethodCodeGenerator.GetWmiClassDescription: string; |
| 282 | +var |
| 283 | + ClassDescr : TStringList; |
| 284 | + Index : Integer; |
| 285 | +begin |
| 286 | + ClassDescr:=TStringList.Create; |
| 287 | + try |
| 288 | + Result := WMiClassMetaData.MethodByName[WmiMethod].Description; |
| 289 | + |
| 290 | + if Pos(#10, Result) = 0 then //check if the description has format |
| 291 | + ClassDescr.Text := WrapText(Result, 80) |
| 292 | + else |
| 293 | + ClassDescr.Text := Result;//WrapText(Summary,sLineBreak,[#10],80); |
| 294 | + |
| 295 | + for Index := 0 to ClassDescr.Count - 1 do |
| 296 | + ClassDescr[Index] := Format(''' %s', [ClassDescr[Index]]); |
| 297 | + |
| 298 | + Result:=ClassDescr.Text; |
| 299 | + finally |
| 300 | + ClassDescr.Free; |
| 301 | + end; |
| 302 | +end; |
| 303 | + |
| 304 | +end. |
0 commit comments