DELPHI Menu:
File/New/Other…
Press the OK [ok] button
Click the File/Save menu to save:
Enter the project name MyCOM
Press the Save button
Open the menu File/New/Other again…
Press the [OK} button;
Enter MyObject in CoClassName
Press OK
Press MOUSE right-click on IMyObject
ImyObject/New/Method
Enter Test at Name
Switch to Parameters page
Press Add twice
Change the Name of the first Parameters to PsParam1, type into BSTR
Change the Name of the second Parameters to PsResult, type into BSTR*
Point MOUSE to the Modifier field of the second Parameters
Press the “…” button
Tick RetVal
Press OK
(The original [in] becomes [out, retval], indicating that this is to be returned, and its type is a string)
Close this window
(You will see it builds a CODE model for you)
File/Save
Enter the file name MyObject
Press Save
Change CODE
Click Ctrl+S to save
PRoject/Build MyCOM
You can see that a MYCOM.DLL file has been generated.
This is the COM component you want. You can use it after registering this component with the Regsvr32 command.
Let’s talk about the method of calling this COM component:
New A new application
Add a Comobj in the uses
Put a button in FORM
Enter the following Code in the button's onClick event:
Code : |
var MyObject: OleVariant; Begin MyObject:=CreateOleObject('MyCOM.MyObject'); try ShowMessage(MyObject.Test('Dawn')); Finally VarClear(MyObject); end; end; |
Execute... and press Button1 button
You will see a Message...that is, the value of the test method of the COM object is executed.