Step by STEP Connectable Objects and Acceptors Implement

zhaozj2021-02-17  81

First, create source objects

1, new ATL project

2, select EXE, determine

3. Right-click project in Workspace, select New ATL Object in the pop-up menu

4, category selection OJBect, Objects Select SimpleObject

5. Enter Math in ShortName in the Names tag.

6, the Attributes tab is selected in Support Connection Points

7. Right click on Imath in Workspace, select Add Mathod

8, press as follows

9. Open SourceObj.idl Join Dispinterface _imathevents {Properties: Methods: [ID (1), HELPSTRING ("Method Finished")] HRESULT ONSPEAK ([in] long lresult;};

10, first build, then right-click CMATH, select Implement Connection Point

Talk again in the dialog box, choose _imathevents, then press the default action.

12, then

STDMETHODIMP CMath :: Speak (long time) {// TODO: Add your implementation code here Fire_OnSpeak (time); return S_OK;} math.h then modified BEGIN_CONNECTION_POINT_MAP (CMath) CONNECTION_POINT_ENTRY (DIID__IMathEvents) END_CONNECTION_POINT_MAP ()

Then recompile

Second, create a proxy stub DLL1, right-click in Workspace and select Insert Project Into Workspace

2, choose SourceObj.mk,

3. Select the project setting of the SourceObj1 project as follows

Set it to compile.

4. Select the register control of the menu

Third, create an acceptor

1. Right-click Workspace to select Add New Project

2, next to Figure 1, Figure 2 is set to generate new items, the project is called SINK

3, modify sink.cpp, code as follows

// sink.cpp: Implementation of WinMain

// Note: Proxy / Stub Information // To Build A Separate Proxy / Stub DLL, // Run Nmake -f Sinkps.mk in The Project Directory.

#include "stdafx.h" #include "resource.h" #include #include "sink.h"

#include "sink_i.c"

#include "../sourceObj/sourceObj_i.c"#include" ../sourceobj/sourceobj.h"#include

Static _tl_func_info onspeakinfo = {cc_stdcall, vt_empty, 1, {vt_i4}}; static const Int Dispid_onspeak = 1; static const INT SPEAK_EVENT_ID = 1; // Arbitrary

Class CsinkObj: Public IDispeventsImpleImpl {public: csinkobj () {}

BEGIN_SINK_MAP (CSinkObj) // Make sure the Event Handlers have __stdcall calling convention SINK_ENTRY_INFO (SPEAK_EVENT_ID, __uuidof (_IMathEvents), DISPID_ONSPEAK, OnSpeak, & OnSpeakInfo) END_SINK_MAP ()

// Event Handler HRESULT __STDCALL ONSPEAK (long LRESULT) {beep (5000, LRESULT); RETURN S_OK;}};

Const dword dwtimeout = 5000; // Time for Exe to be my IDle before shutting downconst dword dwpause = 1000; // time to wait for threads to finish Up

// passed to createthread to Monitor The Shutdown EventStatic DWORD WINAPI MONITORPROC (VOID * PV) {CEXEMODULE * P = (Cexemodule *) PV; P-> MonitorShutdown (); return 0;}

Long Cexemodule :: unlock () {long l = ccommodule :: unlock (); if (l == 0) {bactivity = true; setEvent (hEventShutdown); // Tell Monitor That We transition to zero} Return L;}

// Monitors the shutdown eventvoid CExeModule :: MonitorShutdown () {while (1) {WaitForSingleObject (hEventShutdown, INFINITE); DWORD dwWait = 0; do {bActivity = false; dwWait = WaitForSingleObject (hEventShutdown, dwTimeOut);} while (dwWait = = WAIT_OBJECT_0);! // timed out if (bActivity && m_nLockCnt == 0) // if no activity let's really bail {#if _WIN32_WINNT> = 0x0400 & defined (_ATL_FREE_THREADED) CoSuspendClassObjects ();! if (bActivity && m_nLockCnt == 0) #ENDIF BREAK;}} closehandle (HEVENTSHUTDOWN); PostthreadMessage (DWTHREADID, WM_QUIT, 0, 0);}

bool CExeModule :: StartMonitor () {hEventShutdown = CreateEvent (NULL, false, false, NULL); if (hEventShutdown == NULL) return false; DWORD dwThreadID; HANDLE h = CreateThread (NULL, 0, MonitorProc, this, 0, & dwThreadID Return (H! = Null);} CEXEMODULE _MODULE

Begin_Object_map (ObjectMap) end_Object_map ()

LPCTSTSTR FINDONEOF (LPCTSTSTSTSTSTSTSTSTSTST) {while (p1! = Null && * p1! = Null) {lpctstr p = p2; while (p! = Null && * p! = Null) {if (* p1 == * p) Return CharNext (P1); P = CharNext (P);} p1 = charNext (p1);} Return NULL;}

/// extern "C" int WINAPI _tWinMain (HINSTANCE hInstance, HINSTANCE / * hPrevInstance * /, LPTSTR lpCmdLine, int / * nShowCmd * /) {lpCmdLine = GetCommandLine (); // this line necessary for _ATL_MIN_CRT

#if _WIN32_WINNT> = 0x0400 & defined (_ATL_FREE_THREADED) HRESULT hRes = CoInitializeEx (NULL, COINIT_MULTITHREADED); # else HRESULT hRes = CoInitialize (NULL); # endif _ASSERTE (SUCCEEDED (hRes)); _Module.Init (ObjectMap, hInstance, & LIBID_SINKLib ); _Module.dwthreadId = getCurrentThreadId (); tchar sztokens [] = _t ("- /");

/ * Int nret = 0; bool brun = true; lpctstr lpsztoken = Findoneof (lpcmdline, sztokens); while (lpsztoken! = Null) {if (LSTRCMPI (LPSZTOKEN, _T ("unregserver)) == 0) {_module. UpdateRegistryFromResource (IDR_Sink, FALSE); nRet = _Module.UnregisterServer (TRUE); bRun = FALSE; break;} if (lstrcmpi (lpszToken, _T ( "RegServer")) == 0) {_Module.UpdateRegistryFromResource (IDR_Sink, TRUE); nRet = _Module.RegisterServer (TRUE); bRun = FALSE; break;} lpszToken = FindOneOf (lpszToken, szTokens);} if (bRun) {_Module.StartMonitor (); # if _WIN32_WINNT> = 0x0400 & defined (_ATL_FREE_THREADED) hRes = _Module.RegisterClassObjects (CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED); _ASSERTE (SUCCEEDED (hRes)); hRes = CoResumeClassObjects (); # else hRes = _Module.RegisterClassObjects (CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE); # ENDIF _SSERTE

MSG Msg; While (GetMessage (& MSG, 0, 0, 0)) DISPATCHMESSAGE (& MSG);

_Module.RevokeClassObjects (); Sleep (dwPause); // wait for any threads to finish} * / IMath * pMath; HRESULT hr = CoCreateInstance (CLSID_Math, NULL, CLSCTX_LOCAL_SERVER, __ uuidof (IMath), (void **) & pMath);

IF (succeeded (hr)) {csinkobj * psink = null; psink = new csinkobj; psink-> dispeiventadvise (pmath); pmath-> speak (2000);

IF (psink) {if (psink-> m_dweventcookie! = 0xfefefefe) psink-> dispeiventunadvise (pmath); delete psink;}}

_Module.Term (); counitialize (); return 0;} four, program flow

SINK calls Source's SPEAK interface, SINK interface notifies SINK, "I am On Speak"

BLUECREST.NEASE.NET To send me an email

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

New Post(0)