This article is welcome to reprint, please specify the source and the author blackcolor@263.net -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------
Practical COM (08) ---- COM characteristics, timely activation
I. Creating a component 1) Create a process within the process, add a "Simple Object", named "Simple05", modify the "Threading Model" property is "Both", "interface" property is "Custom". 2) Add a method for interface isimple05 HRESULT ModifyData () 3) Modify file Simple05.cpp
// Simple05.cpp: importation of csimple05 # include "stdafx.h" #include "step05.h" #include "simple05.h"
#include
// Add Comutil Support #include
/// csimple05
/ / Define a new class CAUTHORS {public: // data-width is derived from the table authors char m_szau_id [12]; char m_szau_lname [41]; // Parameter binding begin_param_map (cauthors) Column_entry (1, m_szau_lname) Column_entry ( 2, m_szau_id) end_param_map ()};
STDMETHODIMP CSIMPLE05 :: MODIFYDATA () {CDataSource Connection; CCommand
// Take the transaction environment interface IObjectContext * pObjectContext = null; hr = getObjectContext (& POBJECTCONTEXT); if (Failed (HR) ||! POBJECTCONTEXT) RETURN HR;
/ / Check if there is a transaction environment if (! PObjectContext-> isintransaction ()) Return E_FAIL;
// open the database CDBPropSet dbinit (DBPROPSET_DBINIT); dbinit.AddProperty (DBPROP_INIT_DATASOURCE, OLESTR ( "GP2000")); // server name dbinit.AddProperty (DBPROP_AUTH_USERID, OLESTR ( "sa")); // username dbinit.AddProperty ( DBPROP_AUTH_PASSWORD, OLESTR ( "123")); // password dbinit.AddProperty (DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false); dbinit.AddProperty (DBPROP_INIT_CATALOG, OLESTR ( "pubs")); // database name dbinit.AddProperty (DBPROP_INIT_LCID, (long) 2052 ); Dbinit.addproperty (dbprop_init_prompt, (short) 4); hr = connection.open (_t ("sqloledb.1"), & dbinit); if (Failed (HR)) Return HR; HR = session.open (connection) IF (FAILED (HR)) {Connection.Close (); Return HR;} // Setting Update Conditions STRCPY (Authors.m_Szau_ID, "172-32-1176"); // 172-32-1176 STRCPY (Authors. m_szAu_lName, "MY name"); // White hr = Authors.Open (Session,) "Update Authors Set au_lName = Where au_Id =??"; if (pObjectContext) {if (SUCCEEDED (hr)) {pObjectContext-> SetComplete (); // Submit a transaction} else {pObjectContext-> setAbort ();}} session.close (); connecti On.Close ();
Return hr;}
Second, create a client 1) The client file is as follows: // client.cpp file - Com transaction features
#include
/ / Contains the COM definition file, the following two files are copied from STEP05 #include "../step05/step05.h"#include" ../step05/step05_i.c "
Void main () {isimple05 * pisimple05 = null; // We define the interface
// Initialization HRESULT HR = Coinitialize (NULL); failed (HR)) {Printf ("Coinitialize Failed! HR = 0x% x", HR); Return;}
// Create a COM object hr = COCREATEINSTANCE (CLSID_SIMPLE05, NULL, CLSCTX_ALL, IID_ID_IMPLE05, (Void **) & pisimple05); if (FAILED (HR)) {Printf ("Create COM FAILED! HR = 0x% x", HR); Couninitialize (); return;} // modified data hr = pisimple05-> modifydata (); if ("Modify OK!"); Else Printf ("Modify Failed HR = 0x% x / n" Pisimple05-> Release (); couninitialize ();
Return;}
Third, Configuring COM Components 1) Open "Component Services" from the management tool, create a COM component Step05, add dynamic connection library Step05.dll, and operate with the previous section 2) Set "Step05.SIMPLE05.1" property, as shown below , Set transaction support to "need"
3) Export and configure COM client, run customer software client.exe
Fourth, this section briefly introduces the use of Com transactions, you can find that the transaction is still relatively simple in Com , but if the transaction is complicated, it will give a lot of difficulties to the program, for example, if a transaction Need a long time to complete, it will lock the resources of the system for a long time, and prevent other concurrent users from accessing this resource. How to solve the sharing of resources and deadlocks Always is a big problem for network software design. 2) This section does not mention the "timely activation" characteristics, in fact, when we set the component's transaction to "need", it automatically supports the "timely activation" characteristics, as shown below: