Actual comb (02) ---- Create a process within a process

zhaozj2021-02-08  221

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

Use ATL to create internal components

First, establish a process within the process of component 1) Open Menu File | New, select the Projects Tag, then select "ATL COM AppWizard" 2) Enter step 01 in the Project Name bar, click the OK button, click the finish button in the next window.

Insert Object: 3) Open Menu Insert | New ATL Object, select Objects in Category on the left, select Simple Object in the Objects on the right, click "Next" button 4) In the Names tab, the Short Name column fills in simple01, not Modify other properties, click the "OK" button.

Join Interface Method: 5) Right-click on the "Isimple01" interface in Workspace, select "Add Method" from the pop-up menu, in the pop-up window, Method Name column fill in the GetOSversion, parameters column Fill in [Out, RetVal] BSTR * BSTRVERSION

Modify the function stdmethodimp csimple01 :: getosversion (BSTR * BSTRVERSION) as follows:

// simple01.cpp: importation of csimple01 # include "stdafx.h" #include "step01.h" #include "simple01.h"

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

/// csimple01

// get information on the operating system STDMETHODIMP CSimple01 :: GetOSVersion (BSTR * bstrVersion) {char szOSVersion [10]; OSVERSIONINFO VersionInformation; VersionInformation.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); // about GetVersionEx refer to MSDN if (GetVersionEx (& VersionInformation)) { switch (VersionInformation.dwPlatformId) {case VER_PLATFORM_WIN32s: strcpy (szOSVersion, "WIN32S"); break; case VER_PLATFORM_WIN32_WINDOWS: strcpy (szOSVersion, "WIN95 / 98"); break; case VER_PLATFORM_WIN32_NT: strcpy (szOSVersion, "WINNT"); break Default: STRCPY (SZOSVERSION, "Unknown"); Break;}} else {structure (szosversion, "error");} * bstrversion = _com_util :: convertstringtobstr (szosversion); return s_ok;}

Compiler, so we have established a process within a process. Below we compile the Proxy / Stub program 6) Enter the "MS DOS" state, find the directory of step01, run NMAKE Step01ps.mk, will create a dynamic connection library named Step01ps.dll, this dynamic connection library will create COM applications later Used. Second, establish a client program 1) Open the menu file | New, select the Projects tab, then select "Win32 Console Application" 2) Enter the client in the Project Name bar, click the OK button, click the finish button in the next window.

Newly built as follows, join the project: // client.cpp file

#include #include // Add Comutil support #include #pragma Comment (lib, "comsupp.lib")

/ / Contains the COM definition file, the following two files are copied from STEP01 #include "../step01/step01.h"#include" ../step01/step01_i.c "

Void main () {isimple01 * pisimple01 = null; // We define interface BSTR BSTROS;

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

// Create a COM object HR = COCREATEINSTANCE (CLSID_SIMPLE01, NULL, CLSCTX_ALL, IID_ID_IMPLE01, (Void **) & Pisimple01); if (Failed (HR)) {Printf ("Create COM FAILED! HR = 0x% x", HR); Couninitialize (); return;} // get operating system hr = pisimple01-> getOSversion (& BSTROS); if (Failed (HR)) {Printf ("Pisimple01-> getosversion () failed! Hr = 0x% x", HR) Pisimple01-> Release (); couninitialize (); return;} char * pszos; pszos = _com_util :: convertBSTRTOSTRING (BSTROS); Printf ("The OS IS% S / N", PSZOS;

// Release the allocated memory sysfreestring (bstros); delete pszos; pisimple01-> release (); counitialize ();

Return;}

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

New Post(0)