Standing COM (07) ---- COM + Characteristic Strings

zhaozj2021-02-08  241

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

Standing COM (07) ---- COM Characteristic Strings

The example of this section is based on how to use "builder string" in COM . The so-called builder string, some books are named "constructed string", which is actually transmitted to the COM component when instantizing the COM component, which is generally used to set the DSN of the connection database.

First, open the COM engineering Step041) Add a function hResult getconstructString (BSTR * BSTRING) to interface isimple04

2) Modify simple04.h file: // simple04.h: Declaration of the csimple04 # include // header file comes from SDK - new

#include // database operation

#ifndef __simple04_h_ # define __simple04_h_

#include "resource.h" // main symbols

/ / Define a new class Cauthors {public: // data-width is derived from the table authors char m_szau_id [11]; char m_szau_lname [40]; char m_szau_fname [20]; // Output binding begin_column_map (cauthors) column_entry (CAUTHHORS) Column_ENTRY 1, m_szau_id) Column_entry (2, m_szau_lname) Column_ENTRY (3, M_SZAU_FNAME) End_Column_map ()

// Parameter Bind Begin_Param_map (Cauthors) Column_Entry (1, M_SZAU_LNAME) end_param_map ()};

/// CSimple04class ATL_NO_VTABLE CSimple04: public CComObjectRootEx , public CComCoClass , public IObjectConstruct, // the new public IDispatchImpl {public: CSimple04 () {strcpy (m_szConstruct, "In CSimple04 "); // new m_hr = e_fail; // initialization}

DECLARE_REGISTRY_RESOURCEID (idR_SIMPLE04)

Declare_protect_final_construct ()

Begin_COM_MAP (CSIMPLE04) COM_INTERFACE_ENTRY (ISIMPLE04) COM_INTERFACE_ENTRY (IDISPATCH) COM_INTERFACE_ENTRY (IOBJECTCONSTRUCT) / / New END_COM_MAP ()

// Add variable public: cDataSource M_Connection; csession m_session; ccommand m_authors; hResult m_hr; char m_szconstruct [30]; // can modify the size as needed - New

// ISimple04public: STDMETHOD (DisConnect) (void); STDMETHOD (MoveNext) (void); STDMETHOD (GetAu_fname) (/ * [out, retval] * / BSTR * bAu_fname); STDMETHOD (GetAu_lname) (/ * [out, retval ] * / Bstr * bau_lname); stdmethod (/ * [out, retval); stdmethod (void); stdmethod (getConstructString) (/ * [out, retval] * / BSTR * BString); // New // IOBJECTCONSTRUCTPUBLIC: STDMETHODIMP Construct (iDispatch * punk); // New

}

#ENDIF / / _ __ simple04_h_

3) Modify Simple04.cpp file // simple04.cpp: importation of csimple04 # include "stdafx.h" #include "step04.h" #include "simple04.h" // Add Comutil support #include # Pragma Comment (Lib, "Comsupp.lib")

#pragma comment (lib, "svccguid.lib") // from SDK - new

// csimple04

/ / Connect to the service SQL, and retrieve the authors data stdmethodimp csimple04 :: connecttOSQL () {// If the connection has been established, then turn off the if (succeeded (m_hr)) {m_session.close (); m_connection.close () ; // Close connection M_hr = 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); m_hr = m_connection.open (_t ("sqloledb.1"), & dbinit); if (failed (m_hr)) return m_hr; m_hr = m_session.Open (m_connection) ; If (failed (m_hr)) {m_connection.close (); Return M_hr;} // Set Query Conditions STRCPY (M_AUTHORS.M_SZAU_LNAME, "% H%"); // au_lname contains "H" author M_Authors.open (m_session, "select au_id, au_lname, au_fname from authors where au_lname like?"); return S_OK;} // get result set STDMETHODIMP CSimple04 :: MoveNext () {if (SUCCEEDED (m_hr)) {m_hr = m_Authors.MoveNext ( } Else {// Close connection disconnect ();

Return m_hr;} // Take data stdmethodimp csimple04 :: getau_id (BSTR * BAU_ID) {i (succeeded (m_hr)) {* bau_id = _com_util :: convertstringtobstr (m_authors.m_szau_id);}

Return S_OK;

STDMETHODIMP CSIMPLE04 :: Getau_LName (BSTR * BAU_LNAME) {i (succeededed (m_hr)) {* bau_lname = _com_util :: convertstringtobstr (m_authors.m_szau_lname);}

Return S_OK;

STDMETHODIMP CSIMPLE04 :: Getau_FName (BSTR * BAU_FNAME) {i (successded (m_hr)) {* bau_fname = _com_util :: convertstringtobstr (m_authors.m_szau_fname);}

Return S_OK;

STDMETHODIMP CSIMPLE04 :: Disconnect () {if (successted (m_hr)) {m_session.close (); m_connection.close (); // Close connection M_hr = E_FAIL;}

return S_OK;} // get a string configuration - New STDMETHODIMP CSimple04 :: Construct (IDispatch * pUnk) {if return E_UNEXPECTED (pUnk!); IObjectConstructString * pString = NULL; HRESULT hr = E_FAIL; hr = pUnk-> QueryInterface ( IID_IOBJECTCONSTRUCTSTSTRING, (void **) & pstring; if (Failed (HR)) Return HR;

IF (pstring) {BSTR BSTR; PString-> Get_ConstructString (& BSTR);

Char * pszstring; pszstring = _com_util :: ConvertBSTRTOSTRING (BSTR); STRCPY (m_szconstruct, pszstring);

/ / Release the memory delete pszstring; sysfreestring (bstr); pstring-> release ();} else struct (m_szconstruct, "error!");

Return S_OK;

// Near the string - new stdmethodimp csimple04 :: getconstructString (BSTR * BSTRING) {* bstring = _com_util :: ConvertStringTobstr (m_szconstruct);

Return S_OK;

4) Compile STEP04 project and configure it as a COM component Step04, please refer to "actual comb (05) ---- Create a COM application"

Second, establish a client 1) The client file is as follows: // client.cpp file - Netherbit string

#include #include #include

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

/ / Contains the definition file of the COM, the following two files are copied from STEP04 #include "../step04/step04.h"#include" ../step04/step04_i.c "

Void main () {isimple04 * pisimple04 = null; // We define interface BSTR BSTRING;

// Initialization HRESULT HR = Coinitialize (NULL); failed (HR)) {Printf ("Coinitialize Failed! HR = 0x% x", HR); Return;}

// Create a COM object HR = COCREATEINSTANCE (CLSID_SIMPLE04, NULL, CLSCTX_ALL, IID_ID_IMPLE04, (Void **) & pisimple04); if (Failed (HR)) {Printf ("Create COM FAILED! HR = 0x% x", HR); Couninitialize (); Return;} // 取 string bstring hr = pisimple04-> getConstructString (& bstring); if (Failed (HR)) {printf ("pisimple04-> getconstructstring () failed! Hr = 0x% x", HR ); pISimple04-> release (); CoUninitialize (); return;} char * pszConstruct; pszConstruct = _com_util :: ConvertBSTRToString (bString); printf ( "the Construct is% s / n", pszConstruct); // release the allocated Memory sysfreestring (bstring); delete pszconstruct;

Pisimple04-> Release (); couninitialize ();

Return;}

Third, the configuration component Step041) Open "Component Service" to expand the components "Step04" that have just been established, as shown below:

2) Right-click the COM object "Step04.SIMPLE04.1", select "Attribute" 3) from the menu "3) Click" STEP04.SIMPLE04.1 Properties "in the" Activation "tab in the Properties", select "Enable Object Structure" In the following editing box, enter "My String", then determine, as shown below

IV. Run 1) Export the client from the server and install it to Windows 98 2) The above-mentioned client client.exe is running on this 98, should appear below: The construct is my string

V. Note 1) In the above document, any "new", indicating that the part of this modification 2) If you are compiling STEP04 COM objects on the same 98, you may need to log out of Step04.dll, then install the export The client, otherwise the results may appear: "The construct is in csimple04" 3) Since the interface "iObjectConstruct" is COM new content, the VC6 does not have the header file and library file mentioned above, you may need to install A Platform SDK, if you don't have SDK, use the following files to replace, replace it to delete the original file #include and #pragma comment (lib, "svccguid.lib")

// my_construct.h is used to replace SDK files, by BlackColor. 2001.08 # ifndef __my_construct_h_ # define __my_construct_h_

Const IID IID_IOBJECTCONSTRUCTSTRING = {0x41C4F8B2, 0X7439, 0X11D2, {0x98, 0xCB, 0x00, 0XC0, 0X4F, 0x8E, 0XE1, 0XC4}}

MIDL_INTERFACE ( "41C4F8B3-7439-11D2-98CB-00C04F8EE1C4") IObjectConstruct: public IUnknown {public: virtual HRESULT STDMETHODCALLTYPE Construct (/ * [in] * / IDispatch __RPC_FAR * pCtorObj) = 0;}; MIDL_INTERFACE ( "41C4F8B2-7439- 11D2-98CB-00C04F8EE1C4 ") IObjectConstructString: public IDispatch {public: virtual / * [helpstring] [id] [propget] * / HRESULT STDMETHODCALLTYPE get_ConstructString (/ * [retval] [out] * / BSTR __RPC_FAR * pVal) = 0; }

#ENDIF / / __MY_CONSTRUCT_H_

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

New Post(0)