The following VBScript code runs smoothly when I use cscript and/or VB console applications:
I plan to call this code into my windows service application, it currently has the following code:
For some odd reason, the VBScript code only works on cscript but when I call in the code to the service, it doesn't work... someone explain why?
Code:
Set wmiServices = GetObject("winmgmts:root/default")
Set colTreeChanges = wmiServices.ExecNotificationQuery _
("SELECT * FROM RegistryTreeChangeEvent " _
& "WHERE Hive='HKEY_LOCAL_MACHINE' " _
& "AND RootPath='SOFTWARE\\Microsoft'")
While (True)
Set TreeChange = colTreeChanges.NextEvent
TreeChange.GetObjectText_()
Wscript.Echo "Hive = " & TreeChange.Hive & VBNewLine _
& "RootPath = "& TreeChange.RootPath _
& TreeChange.GetObjectText_()
Wend
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management;
using System.ServiceProcess;
using System.Text;
using System.Timers;
namespace SoftwareDetectionService
{
public partial class SoftwareDetectionService : ServiceBase
{
public SoftwareDetectionService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("Service has started");
Process scriptproc = new Process();
scriptproc.StartInfo.RedirectStandardOutput = true;
scriptproc.StartInfo.UseShellExecute = false;
scriptproc.StartInfo.FileName = @"cscript";
scriptproc.StartInfo.Arguments = @"c:\Users\081254X\Documents\code11.vbs";
scriptproc.Start();
StreamReader SR = scriptproc.StandardOutput;
string result = SR.ReadToEnd();
eventLog1.WriteEntry(result);
scriptproc.WaitForExit();
SR.Close();
scriptproc.Close();
}
protected override void OnStop()
{
eventLog1.WriteEntry("Service has ended");
}
}
}
Comment