Actual combat COM (03) ---- Create a processes

zhaozj2021-02-08  221

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

Use ATL to create process external components

First, establish a process external component 1) Open the menu file | New, select the Projects tab, then select "ATL COM AppWizard" 2) Enter STEP02 in the Project Name bar, click the OK button, select "Server Type column in the next window" After executable (exe), click the Finish button

Then insert the object (Similar to the creation process): 3) Open the menu INSERT | New ATL OBJECT, select Objects in the Category on the left, select Simple Object in the Objects on the right, click "Next" button 4) Tag on the Names In the SHORT NAME column, fill in Simple02, do not modify other properties, click the "OK" button.

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

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

// simple02.cpp: importation of csimple02 # include "stdafx.h" #include "step02.h" #include "simple02.h"

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

/// csimple02

// get information on the operating system STDMETHODIMP CSimple02 :: GetOSVersion (BSTR * bstrVersion) {char szOSVersion [10]; OSVERSIONINFO VersionInformation; VersionInformation.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); 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;}

The compiler, so we have established a process external component. We compile the proxy / stub program 6) Enter the "MS DOS" state, find the directory of step02, run NMAKE Step02ps.mk, will create a dynamic connection library named Step02ps.dll . 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 COM definition file, the following two files are copied from STEP02 #include "../step02/step02.h"#include" ../step02/step02_i.c "

Void main () {isimple02 * pisimple02 = 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 = COCREATE0E02 (CLSID_SIMPLE02, NULL, CLSCTX_ALL, IID_ID_IMPLE02, (Void **) & pisimple02); if (FAILED (HR)) {Printf ("Create COM FAILED! HR = 0x% x", HR); Couninitialize (); return;} // get operating system hr = pisimple02-> getOSversion (& BSTROS); if (Failed (HR)) {printf ("pisimple02-> getosversion () failed! Hr = 0x% x", HR) Pisimple02-> Release (); couninitialize (); return;} char * pszos; pszos = _com_util :: convertBSTRTOSTRING (BSTROS); Printf ("The OS IS% S / N", PSZOS;

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

Return;}

Third, Description 1) Registration of processes outside the process: Step02.exe / regserver Logout: Step02.exe / UnregServer

Run our Client.exe under Windows 98, you can get the OS IS WIN95 / 98 running our client.exe in Windows 2000, you can get the OS Winnt

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

New Post(0)