C # Programming Control COM + Service

xiaoxiao2021-04-08  370

C

# 编 编 控制 控制

COM

service

Create Date: 2006-6-23

Applicable to: .NET Framework, C #

Author: Dogvane

Because when designing a local upgrade program, you need to upgrade a COM service DLL, there is a need to stop the COM service before reading the DLL. After reading the MSDN help, find a COM 1.0 Admin Type library, all COM operations You can do this COM object.

Implementation:

1. Export COM object

2. Create an object of a user controls the COM application

Comadmin

.Comadmincatalogclass com = new comadmin.comAdmincatalogClass ();

Comadmin

.Comadmincatalogcollection list = (Comadmin.comAdmincatalogCollection) com.getcollection; "Applications");

Here is a list of COM applications through getCollectoin.

At this time, the list of List is 1, and there is no thing inside. At first, I thought it was a keyword, I tried N other methods without having a value in the list, the dead MSDN to be available for this method without any sample code. Finally, I finally know when I check the N kinds of control COM , the resulting list needs to call the Populate method to get the application ^ _ ^!

3. Disable Com app

List.populate (); //

Acquire application

System.collections.ienumerator i = list.GeteNumerator ();

While (I.MOVENEXT ())

{

Comadmin.icatalogObject App = (Comadmin.icatalogObject) i.current;

IF (app.get_value ("name"). Tostring () == _name)

{

///

Disable Com service

App.Set_Value ("ISENABLED", FALSE;

List.savechanges (); //

Save this operation, COM service will not stop

}

}

After acquiring all COM applications, it is a traversal process, which takes the COM program you want to stop (with get_value ("name") method). Then generate the iSenable to false, if you want to enable COM

4. Although the COM application is disabled, the DLL is still unable to serve, the system prompts the DLL file or occupied, the reason is to be used after the COM application has been using, the service is disabled, but the DLL program is still running. Finally, the application is ended via the ShutDownApplication mode (equivalent to killing the process)

COM

. SHUTDOWNAPPLICATION (_Name);

5. If you just want to update one component in an application (

Components, do not necessarily disable the entire program, just disable the component. Disabled methods and methods for disabling COM applications, the difference is that we need to call when we get a list of components.

GetCollectionByQuery2, after all, all components are existing below the COM application. Object

Key = app.key; //

Specified COM application

Comadmin.comAdmincatalogCollection Components = (Comadmin.comAdmincatalogCollection) com.getcollectionbyquery2 ("Components", Ref key)

Components.Populate (); //

Like the COM list, there is no thing in the newly created list.

6. Other ways, all objects in the COM service are obtained by getCollectionByQuery, and then the settings of the various COM attributes through these objects, and the modification is complete, and the setting is updated.

note:

Because the operating system after XP, the interface used by COM is

GetCollectionByQuery2, and the interface used under Win2k is getCollectionByQuery. Although the operation of the interface can be changed to getCollectionByQuery in the code, the program compiled under XP cannot run in Win2K. The reason why the DLL exported time-exported DLL will pour it out, and the object interface is created under the system Win2k system. If you can't find this function, it will be wrong.

The correct approach is that the project is issued, and it needs to be compiled under the Win2K system.

Win9x, Win NT system has not been tested without cleaning whether there is such a problem.

Appendix: COM Service Control class code

///

/// COM Service

Control class

///

Internal Class Complusservice: IServiceControl

{

Private INT _timeout = 10;

Public int Timeout

{

get

{

Return_timeout;

}

set

{

IF (Value> 0)

_Timeout = Value;

Else

_Timeout = 10;

}

}

String _name = string.empty;

Comadmin.comAdmincatalogClass COM = New Comadmin.comAdminCatalogClass ();

Public ComplusService (String Name)

{

Name = name;

}

#REGION

IServiceControl MEMBERS

Public String Name

{

get

{

Return_name;

}

set

{

_Name = value;

}

}

Public void stop ()

{

Try

{

LCFTLOG.LOGGING.INFO ("STOP COM Service: _name);

Comadmin.comAdmincatalogCollection List = (Comadmin.comAdmincatalogCollection) com.getcollection; "Applications");

List.populate ();

System.collections.ienumerator i = list.getenumerator (); ///

Stop COM Server (uninstall from memory)

com.shutdownapplication (_name);

While (I.MOVENEXT ())

{

Comadmin.icatalogObject App = (Comadmin.icatalogObject) i.current;

IF (app.get_value ("name"). Tostring () == _name)

{

///

Disable Com service

App.Set_Value ("ISENABLED", FALSE;

List.savechanges ();

///

The following operation is mainly to obtain component information in the COM service (DLL file path)

///

Then determine if the components have stopped (uninstall from the COM application, there is no write protection)

Object key = app.key;

Comadmin.comAdmincatalogCollection Components = (Comadmin.comAdmincatalogCollection) com.getcollectionbyquery2 ("Components", Ref key)

Components.Populate ();

System.collections.ienumerator i2 = components.getenumerator ();

While (I2.MOVENEXT ())

{

Comadmin.icatalogObject Obj2 = (Comadmin.icatalogObject) i2.current;

String DLL = Obj2.get_value ("dll"). TOSTRING ();

INT WAIT = TIMEOUT;

LCFTLOG.LOGGING.DEBUG (DLL);

While (! canwritefile (dll))

{

IF (Wait - <0)

Break;

System.threading.Thread.sleep (500);

}

}

}

}

}

Catch (Exception EX)

{

LCFTLOG.LOGGING.ERROR ("{0} stop fail" .Replace ("{0}", _name);

Throw EX;

}

}

Private bool canwritefile (String filename)

{

System.IO.FileStream Stream = NULL;

BOOL Error = false;

Try

{

Stream = system.io.file.open (filename, system.io.filemode.open, system.io.fileAccess.readwrite);

}

Catch

{

Error = True;

}

IF (stream! = null)

stream.close ();

Return! Error;

}

Public void start ()

{

Try

{

LCFTLOG.LOGGGING.INFO ("START COM Service:" _name);

Comadmin.comAdmincatalogCollection List = (Comadmin.comAdmincatalogCollection) com.getcollection; list.populate ();

System.collections.ienumerator i = list.GeteNumerator ();

While (I.MOVENEXT ())

{

Comadmin.icatalogObject App = (Comadmin.icatalogObject) i.current;

IF (app.get_value ("name"). Tostring () == _name)

{

App.Set_Value ("ISENABLED", TRUE;

List.savechanges ();

}

}

com.startApplication (_name);

}

Catch (Exception EX)

{

LCFTLOG.LOGGING.ERROR ("{0} start fail" .Replace ("{0}", _name));

Throw EX;

}

}

#ndregion

}

转载请注明原文地址:https://www.9cbs.com/read-132797.html

New Post(0)