Can not get "StartMode" of a Windows Service. VB.NET

0

I want to know if a service startup mode is set on "automatic", and if yes, to set it to "Manual"

I have this code:

        If objService("StartMode").ToString = "Automatic" Then
            objService.ChangeStartMode("Manual")
        End If

But when i compile my project, Visual Studio reports that error:

COMException was unhandled : Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

in If objService("StartMode").ToString = "Automatic" Then

Please help me how to solve that? It's very important for me. Thanks.

I'm using Visual Studio 2008, VB.NET, Windows XP Sp3, on Administrator account.

vb.net
windows-services
comexception
asked on Stack Overflow Dec 20, 2012 by Andrei20193 • edited Dec 20, 2012 by Andrei20193

1 Answer

0
    If objService("StartMode").ToString = "Automatic" Then

You are using late binding, a way to use COM objects that tends to slap you with runtime errors when you use their properties and methods incorrectly. You can avoid these kind of problems by using the classes in the System.Management namespace.

The statement doesn't do what you think it does, it calls the default property of the interface and passes "StartMode" as an argument to the property getter for that default property. That's not correct usage of that interface, StartMode is a property by itself and it is not the default property. Fix:

    If objService.StartMode = "Automatic" Then
answered on Stack Overflow Dec 20, 2012 by Hans Passant

User contributions licensed under CC BY-SA 3.0