Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Attaching to COM from a DLL Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
14-Aug-04
Category
COM+
Language
Delphi 5.x
Views
161
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Mike Heydon

When attaching to a COM object in a DLL an error occurs if CoInitialize() has not 
been called. It is also required to call CoUnInitialize(). The easiest way to do 
this is in the initialization and ExitProc() routines of the DLL. The following 
example demonstrates how to attach to COM from your DLL.

Answer:

1   library TestCom;
2   
3   uses ActiveX, SysUtils, Classes, ComObj;
4   
5   var
6     SaveExit: Pointer; // Required by Exit routine
7   
8     // ============================
9     // Arbitary test code
10    // ============================
11  
12  procedure RunDll(Key: PChar); stdcall;
13  var
14    DMess, WKey: WideString;
15    OServer: OleVariant;
16    KeyName: WideString;
17  begin
18    try
19      // Connect to COM
20      OServer := CreateOleObject('PeekaBooAlerter.TPBbase');
21      KeyName := 'Wakeup Message';
22      OServer.ReadStringKey(KeyName, DMess);
23      WKey := string(Key);
24      OServer.WriteAlert(DMess, WKey);
25    finally
26      OServer := VarNull; // Release the COM attachment
27    end;
28  end;
29  
30  // =================================================
31  // EXIT Routine - Deinitialize COM library and call
32  // original Exit routine
33  // =================================================
34  
35  procedure LibExit;
36  begin
37    // library exit code
38    CoUnInitialize;
39    ExitProc := SaveExit; // restore exit procedure chain
40  end;
41  
42  // ==================================================
43  // Export declarations
44  // ==================================================
45  
46  exports
47    RunDll index 1,
48  
49  // ==================================================
50  // DLL library initialization code
51  // ==================================================
52  
53  begin
54    CoInitialize(nil);
55    SaveExit := ExitProc; // Save exit procedure chain
56    ExitProc := @LibExit; // Install LibExit exit procedure
57  end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC