WMI Parameters Problem

R

ranjeet

I'm having some problems trying to change a windows service logon account
using WMI through C#.
The Invoke call near the end keeps giving me "Specified cast is not valid"
exceptions.
I can't see which parameter this could be since all params tie up to the
Win32_Class definition provided by Microsoft ?
(http://msdn.microsoft.com/library/d.../wmi/change_method_in_class_win32_service.asp)

Any help on making this work (or on how to debug this) would be greatly
appreciated!

Ranjeet
ps apologies for the large paste - the code is there if you want to
cut-n-paste into VS
ps2 serviceName exists, userName and password are valid details (have also
tried to stop the service before executing this)

public static void Main()
{
SetServiceLogon("MSSQLSERVER", "DOMAIN_NAME\\DOMAIN_ACCOUNT",
"SOME_PASSWORD") ;
}

public static ServiceStartErrorCodes SetServiceLogon(string serviceName,
string userName, string password)
{
ServiceStartErrorCodes ret = ServiceStartErrorCodes.UnknownFailure ;
string objectQueryString = "select * from Win32_Service where
DisplayName='" + serviceName + "'" ;
ManagementScope scope = new
ManagementScope(System.Management.ManagementPath.DefaultPath) ;
ObjectQuery query = new ObjectQuery(objectQueryString) ;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,
query) ;
ManagementObjectCollection objectCollection = searcher.Get() ;
foreach(ManagementObject managementObject in objectCollection)
{
string displayName = managementObject["DisplayName"].ToString() ;
string pathName = managementObject["PathName"].ToString() ;
uint serviceType =
(uint)(ServiceTypeCodes)Enum.Parse(typeof(ServiceTypeCodes),
managementObject["ServiceType"].ToString().Replace(" ", "").Trim(), true) ;
uint errorControl =
(uint)(ServiceErrorControlCodes)Enum.Parse(typeof(ServiceErrorControlCodes),
managementObject["ErrorControl"].ToString().Replace(" ", "").Trim(), true)
;
string startMode = managementObject["StartMode"].ToString() ;
bool desktopInteract = (bool)managementObject["DesktopInteract"] ;
string loadOrderGroup = "" ;
string loadOrderGroupDependencies = "" ;
string serviceDependencies = "" ;

object[] methodParams = new object[]{ displayName,
pathName,
serviceType,
errorControl,
startMode,
desktopInteract,
userName,
password,
loadOrderGroup,
loadOrderGroupDependencies,
serviceDependencies};
try
{
ret =
(ServiceStartErrorCodes)(uint)managementObject.InvokeMethod("Change",
methodParams) ;
}
catch(Exception exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message) ;
}
}
return ret ;
}

#region ENUM JUNK
public enum ServiceTypeCodes
{
KernelDriver = (1 << 0),
FileSystemDriver = (1 << 1),
Adapter = (1 << 2),
RecognizerDriver = (1 << 3),
OwnProcess = (1 << 4),
ShareProcess = (1 << 5),
InteractiveProcess = (1 << 6)
}

public enum ServiceStartErrorCodes
{
Success = 0 ,
NotSupported = 1 ,
AccessDenied = 2 ,
DependentServicesRunning = 3 ,
InvalidServiceControl = 4 ,
ServiceCannotAcceptControl = 5 ,
ServiceNotActive = 6 ,
ServiceRequestTimeout = 7 ,
UnknownFailure = 8 ,
PathNotFound = 9 ,
ServiceAlreadyRunning = 10,
ServiceDatabaseLocked = 11,
ServiceDependencyDeleted = 12,
ServiceDependencyFailure = 13,
ServiceDisabled = 14,
ServiceLogonFailure = 15,
ServiceMarkedForDeletion = 16,
ServiceNoThread = 17,
StatusCircularDependency = 18,
StatusDuplicateName = 19,
StatusInvalidName = 20,
StatusInvalidParameter = 21,
StatusInvalidServiceAccount = 22,
StatusServiceExists = 23,
ServiceAlreadyPaused = 24
}
#endregion
 
W

Willy Denoyette [MVP]

ranjeet said:
I'm having some problems trying to change a windows service logon account
using WMI through C#.
The Invoke call near the end keeps giving me "Specified cast is not valid"
exceptions.
I can't see which parameter this could be since all params tie up to the
Win32_Class definition provided by Microsoft ?
(http://msdn.microsoft.com/library/d.../wmi/change_method_in_class_win32_service.asp)

Any help on making this work (or on how to debug this) would be greatly
appreciated!

ret =
(ServiceStartErrorCodes)(uint)managementObject.InvokeMethod("Change",
methodParams) ;

InvokeMethod returns a ManagementBaseObject, you can't cast this to a uint,
you need to get the returnValue property from this object and cast this to
uint.


ManagementBaseObject outParams = managementObject.InvokeMethod("Change",
, methodParams);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top