Delphi COM Programming Learning Notes

Source: Internet
Author: User

Neither call Myobj.free, nor myobj.release;
The correct way to destroy objects is to set them to nil:
MyInterface: = nil;

An interface cannot live independently from the object that implements it. This interface is just an abstraction, it's just an idea.
The interface has Plug and Play functionality that establishes the specifications that the vendor must follow.

Interfaces can be run on only one layer of depth, which is relatively straightforward. Unlike OOP, a layer of layers, once changed the middle layer, that is prone to lower-level errors.
In essence, interfaces do not encourage the creation of deep hierarchies, while encouraging relatively simple programs with better performance characteristics.

The GUID is a 128-bit number (16 bytes), which is statistically unique.
In order to generate its own GUID, you must first call the WINAPI function CoInitialize to initialize, and then call CoCreateGuid to get a unique instance:
CoInitialize (nil);
CoCreateGuid (FGUID);
In 32-bit Windows, anything other than passing nil to CoInitialize is wrong.

In the case of COM objects, the registry is a simple database that has only one task: to associate a numeric value with each of the available COM objects in the system.
These values are stored in the CLSID of the HKEY_CLASSES_ROOT
The registry is a database that establishes associations between the CLSID and the name of the program and other relevant information, such as the version number and location where the file is stored.

Microsoft white Send several API
function Stringfromclsid; External ole32 name ' Stringfromclsid ';
function clsidfromstring; External ole32 name ' clsidfromstring ';
function stringfromiid; External ole32 name ' stringfromiid ';
function iidfromstring; External ole32 name ' iidfromstring ';
function ProgIDFromCLSID; External ole32 name ' ProgIDFromCLSID ';
function CLSIDFromProgID; External ole32 name ' CLSIDFromProgID ';
function StringFromGUID2; External ole32 name ' StringFromGUID2 ';
function CoCreateGuid; External ole32 name ' CoCreateGuid ';
There are several other common:
function CoInitialize; External ole32 name ' CoInitialize ';
function CoInitializeEx; External ole32 name ' CoInitializeEx ';
Procedure CoUninitialize; External ole32 name ' CoUninitialize ';
function cogetcurrentprocess; External ole32 name ' cogetcurrentprocess ';
function CoGetClassObject; External ole32 name ' CoGetClassObject ';
function CoRegisterClassObject; External ole32 name ' CoRegisterClassObject ';
function coloadlibrary; External ole32 name ' coloadlibrary ';
Procedure Cofreelibrary; External ole32 name ' cofreelibrary ';

The most interesting thing is to achieve IUnknown yourself:
function tuserunknown.queryinterface (const IID:TGUID; out OBJ): HResult;
Const
E_nointerface = $80004002
Begin
GetInterface is the method of Tojbect. It can also be written like this: Obj:=pointer (self);
GetInterface a wonderful way to return a small vmt, which contains only the method of the interface that the user needs, and only self as a pointer to all the methods in the object
If GetInterface (IID, obj) then result: = 0;
else Reslut: = E_nointerface;
End

function Tuserunknown._addref:integer;
Begin
INC (Frefcount);
Result: = Frefcount;
End

function Tuserunknown._release:integer;
Begin
Dec (Frefcount);
Result: = Frefcount;
If Result = 0 then Destroy;
End

Adds a GUID to the specified interface
Iname = Interface (IUnknown)
[' {C25A86F1-579A-419A-8CB1-D1B5C6BCB8CB} ']
function getname:string;
End
Its advantage is that it allows you to specify a class name when you need to use the GUID in your program.
For example, now that you know this call to QueryInterface, you must pass a GUID in the first argument, just write the following code:
Var
Unknown:iunknown;
Name:iname;
Begin
Fname.queryinterface (IUnknown, Unknown);
Fname.queryinterface (Iname, Name);
End

Delphi interface programming is simpler than standard Delphi object-oriented programming because it does not have to worry about destroying objects, it is usually automatically destroyed.

Reference: "DELPHI4 Programming Technology Insider" chapter 13th

Delphi COM Programming Learning Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.