Battle COM (09) ---- COM + Characteristics

zhaozj2021-02-08  230

This article is welcome to reprint, please specify the source and the author blackcolor@263.net -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------

Battle COM (09) ---- COM Characteristics

In this section, we transform the components of the previous section into a canalized component. Modify the following: 1. Modify the component 1) Header file Simple05.h // simple05.h: Declaration of the csimple05

#ifndef __simple05_h_ # define __simple05_h_

#include "resource.h" // main symbols # include

/// CSimple05class ATL_NO_VTABLE CSimple05: public CComObjectRootEx , public CComCoClass , public IObjectControl, // support object pooling operations public ISimple05 {public: CSimple05 () {}

DECLARE_REGISTRY_RESOURCEID (idR_SIMPLE05)

Declare_protect_final_construct ()

Begin_COM_MAP (CSIMPLE05) COM_INTERFACE_ENTRY (isimple 05) COM_INTERFACE_ENTRY (IOBJECTCONTROL) END_COM_MAP ()

// IOBJECTCONTROLPUBLIC: STDMETHOD (ACTIVATE) (); stdmethod_ (bool, canbepooled) (); stdmethod_ (void, deactivate) ();

CComptr m_spobjectContext;

// isimple05public: stdmethod (modifydata) ();

#ENDIF / / _ __ simple05_h_

2) Application file Simple05.cpp // simple05.cpp: importation of csimple05 # include "stdafx.h" #include "step05.h" #include "simple05.h"

#include // Transaction support #include // Database Operation // Add Comutil Support #include #pragma Comment (Lib, "Comsupp.lib")

/// 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 > Authors; HRESULT HR;

// Create a transaction // 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 (succeededed (hr)) { M_spObjectContext-> setcomplete (); // Submit transaction} else {m_spobjectContext-> setabort ();} //}

Session.close (); connection.close ();

Return HR;} // Object Pool Operation HRESULT CSIMPLE05 :: Activate () {HRESULT HR = GetObjectContext (& M_SPObjectContext); if (succeededed (hr)) Return S_OK; Return HR;}

Bool csimple05 :: canbepooled () {return true;}

Void csimple05 :: deActivate () {m_spobjectContext.release ();

Second, Configuration Object Pool 1) Open "Component Services" from the management tool, delete and reinstall the component "Step05.SIMPLE05.1" property 2) Set "Step05.SIMPLE05.1" properties, as shown below,

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

New Post(0)