Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WScript.ConnectObject for IMAPI2

212 views
Skip to first unread message

Hertwig

unread,
Oct 20, 2007, 9:40:16 AM10/20/07
to
Hi all,

Based on this sample http://msdn2.microsoft.com/en-us/library/aa366443.aspx
I am building an interface from Delphi7 to the IM-API2 under XP-sp2/Vista.

All goes well, except for the part where I need to catch events for the
object.

----
' Attach event handler to the data writing object.
WScript.ConnectObject dataWriter, "dwBurnEvent_"
----

Does anyone have an idea how I should register/attach an event handler to my
component?

Below is my (almost one on one) port of the Microsoft script to Delphi:

---------------------------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses
ComObj;

var
MediaTypeNames: Array[0..12] of String = ( 'Empty device or an unknown
disc type',
'CD-ROM',
'CD-R',
'CD-RW',
'DVD-ROM; Read-only
DVD drive and/or disc',
'DVD-RAM',
'DVD+R',
'DVD+RW',
'DVD+R Dual Layer
media',
'DVD-R',
'DVD-RW',
'DVD-R Dual Layer
media',
'Randomly-writable,
hardware-defect'
);

procedure TForm1.Button1Click(Sender: TObject);
const
IMAPI_FEATURE_PAGE_TYPE_DVD_DASH_WRITE = 47;
// *** IMAPI2 Data Media States
IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN = 0;
IMAPI_FORMAT2_DATA_MEDIA_STATE_INFORMATIONAL_MASK = 15;
IMAPI_FORMAT2_DATA_MEDIA_STATE_UNSUPPORTED_MASK = 61532; //0xfc00
IMAPI_FORMAT2_DATA_MEDIA_STATE_OVERWRITE_ONLY = 1;
IMAPI_FORMAT2_DATA_MEDIA_STATE_BLANK = 2;
IMAPI_FORMAT2_DATA_MEDIA_STATE_APPENDABLE = 4;
IMAPI_FORMAT2_DATA_MEDIA_STATE_FINAL_SESSION = 8;
IMAPI_FORMAT2_DATA_MEDIA_STATE_DAMAGED = 1024; //0x400
IMAPI_FORMAT2_DATA_MEDIA_STATE_ERASE_REQUIRED = 2048; //0x800
IMAPI_FORMAT2_DATA_MEDIA_STATE_NON_EMPTY_SESSION = 4096; //0x1000
IMAPI_FORMAT2_DATA_MEDIA_STATE_WRITE_PROTECTED = 8192; //0x2000
IMAPI_FORMAT2_DATA_MEDIA_STATE_FINALIZED = 16384; //0x4000
IMAPI_FORMAT2_DATA_MEDIA_STATE_UNSUPPORTED_MEDIA = 32768; //0x8000

var
i: Integer;

Index: Integer; // Index to recording drive.
Path: WideString; // Directory of files to burn
Stream: OleVariant; // Data stream for burning device

// recorder vars
Recorder: OleVariant; // Recorder object
g_DiscMaster: OleVariant;
uniqueId: OleVariant;
mountPoint: WideString;
supportedFeature, currentFeature: Integer;
supportedProfile, currentProfile: Integer;
supportedModePage: Integer;

// media vars
dataWriter: OleVariant;
curMediaStatus: Integer;
mediaType: Integer;
begin
Memo1.Clear;

Index := 0; // 0 = first drive; 1 = second drive on the
system

// Create a DiscMaster2 object to connect to CD/DVD drives.
g_DiscMaster := CreateOleObject('IMAPI2.MsftDiscMaster2');

Memo1.Lines.Add ('Drives: ' + IntToStr(g_DiscMaster.Count));

// Create a DiscRecorder object for the specified burning device.
recorder := CreateOleObject('IMAPI2.MsftDiscRecorder2');
uniqueId := g_DiscMaster.Item[Index];
recorder.InitializeDiscRecorder( uniqueId );

// *** - Formatting to display recorder info
with Memo1.Lines do
begin
Add ('--------------------------------------------------');
Add ('ActiveRecorderId: ' + recorder.ActiveDiscRecorder);
Add ('Vendor Id: ' + recorder.VendorId);
Add ('Product Id: ' + recorder.ProductId);
Add ('Product Revision: ' + recorder.ProductRevision);
Add ('VolumeName: ' + recorder.VolumeName);
Add ('Can Load Media: ' + VarToStr(recorder.DeviceCanLoadMedia));
Add ('Device Number: ' + VarToStr(recorder.LegacyDeviceNumber));
end;

Memo1.Lines.Add('');
for i := VarArrayLowBound(recorder.VolumePathNames, 1) to
VarArrayHighBound(recorder.VolumePathNames, 1) do
begin
mountPoint := VarArrayGet(recorder.VolumePathNames, [i]);
Memo1.Lines.Add ('Mount Point: ' + mountPoint);
end;

Memo1.Lines.Add ('Supported Features in _IMAPI_FEATURE_PAGE_TYPE');
for i := VarArrayLowBound(recorder.SupportedFeaturePages, 1) to
VarArrayHighBound(recorder.SupportedFeaturePages, 1) do
begin
supportedFeature := VarArrayGet(recorder.SupportedFeaturePages, [i]);
if supportedFeature = IMAPI_FEATURE_PAGE_TYPE_DVD_DASH_WRITE then
Memo1.Lines.Add (' Feature: ' + IntToStr(supportedFeature) + ';
Drive supports DVD-RW.')
else
Memo1.Lines.Add (' Feature: ' + IntToStr(supportedFeature));
end;

Memo1.Lines.Add ('Current Features in _IMAPI_FEATURE_PAGE_TYPE');
for i := VarArrayLowBound(recorder.CurrentFeaturePages, 1) to
VarArrayHighBound(recorder.CurrentFeaturePages, 1) do
begin
currentFeature := VarArrayGet(recorder.CurrentFeaturePages, [i]);
Memo1.Lines.Add (' Feature: ' + IntToStr(currentFeature));
end;

Memo1.Lines.Add ('Supported Profiles in _IMAPI_PROFILE_TYPE');
for i := VarArrayLowBound(recorder.SupportedProfiles, 1) to
VarArrayHighBound(recorder.SupportedProfiles, 1) do
begin
supportedProfile := VarArrayGet(recorder.SupportedProfiles, [i]);
Memo1.Lines.Add (' Profile: ' + IntToStr(supportedProfile));
end;

Memo1.Lines.Add ('Current Profiles in _IMAPI_PROFILE_TYPE');
for i := VarArrayLowBound(recorder.CurrentProfiles, 1) to
VarArrayHighBound(recorder.CurrentProfiles, 1) do
begin
currentProfile := VarArrayGet(recorder.CurrentProfiles, [i]);
Memo1.Lines.Add (' Profile: ' + IntToStr(currentProfile));
end;

Memo1.Lines.Add ('Supported Mode Pages in _IMAPI_MODE_PAGE_TYPE');
for i := VarArrayLowBound(recorder.SupportedModePages, 1) to
VarArrayHighBound(recorder.SupportedModePages, 1) do
begin
supportedModePage := VarArrayGet(recorder.SupportedModePages, [i]);
Memo1.Lines.Add (' Profile: ' + IntToStr(supportedModePage));
end;

Memo1.Lines.Add('');
Memo1.Lines.Add('----- Finished content -----');
Memo1.Lines.Add('');

// check media
dataWriter := CreateOleObject ('IMAPI2.MsftDiscFormat2Data');

// todo: register event here....but how?? Disable the line below for a
quick run without events
WScript.ConnectObject dataWriter, "dwBurnEvent_"

if dataWriter.IsRecorderSupported(recorder) then
Memo1.Lines.Add ('--- Current recorder IS supported. ---')
else
Memo1.Lines.Add ('Current recorder IS NOT supported.');

dataWriter.recorder := recorder;

if dataWriter.IsCurrentMediaSupported(recorder) then
Memo1.Lines.Add ('--- Current media IS supported. ---')
else
Memo1.Lines.Add ('Current media IS NOT supported.');

dataWriter.ClientName := 'MyClient';
Memo1.Lines.Add ('ClientName = ' + dataWriter.ClientName);

// Check a few CurrentMediaStatus possibilities. Each status is
associated with a bit and some combinations are legal.
Memo1.Lines.Add ('Checking Current Media Status');

try
curMediaStatus := dataWriter.CurrentMediaStatus;
except
curMediaStatus := IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN;
end;

if IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN AND curMediaStatus =
curMediaStatus then
Memo1.Lines.Add (' Media state is unknown.');
if IMAPI_FORMAT2_DATA_MEDIA_STATE_OVERWRITE_ONLY AND curMediaStatus =
curMediaStatus then
Memo1.Lines.Add (' Currently, only overwriting is supported.');
if IMAPI_FORMAT2_DATA_MEDIA_STATE_APPENDABLE AND curMediaStatus =
curMediaStatus then
Memo1.Lines.Add (' Media is currently appendable.');
if IMAPI_FORMAT2_DATA_MEDIA_STATE_FINAL_SESSION AND curMediaStatus =
curMediaStatus then
Memo1.Lines.Add (' Media is in final writing session.');
if IMAPI_FORMAT2_DATA_MEDIA_STATE_DAMAGED AND curMediaStatus =
curMediaStatus then
Memo1.Lines.Add (' Media is damaged.');

if not (IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN AND curMediaStatus =
curMediaStatus) then
begin
mediaType := dataWriter.CurrentPhysicalMediaType;
Memo1.Lines.Add ('Current Media Type: ' + MediaTypeNames[mediaType]);
end;

Memo1.Lines.Add('');
Memo1.Lines.Add ('Total sectors: ' +
IntToStr(dataWriter.TotalSectorsOnMedia));
Memo1.Lines.Add ('Free sectors: ' +
IntToStr(dataWriter.FreeSectorsOnMedia));

Memo1.Lines.Add('');
Memo1.Lines.Add('----- Finished Media Check -----');
Memo1.Lines.Add('');

dataWriter := Unassigned;
recorder := Unassigned;
g_DiscMaster := Unassigned;
end;

end.
---------------------------

I certainly hope that someonecan help me here.

Hert

Hertwig

unread,
Oct 22, 2007, 4:17:26 AM10/22/07
to
none an idea how to do this?

"Hertwig" <bo...@hotmail.com> wrote in message
news:471a...@newsgroups.borland.com...

Ken White

unread,
Oct 22, 2007, 1:48:42 PM10/22/07
to

"Hertwig" <bo...@hotmail.com> wrote:
>none an idea how to do this?

How about simply importing the type library? (In Delphi 2007, Component->Import Component. Check the "Import Type Library" option, and then on the next page click the "Add" button. Browse to your <WindowsDir>\System32, and choose "IMAPI2.DLL". Continue to click "Next" through the rest of the import dialog, making note of where Delphi will save the IMAPI2_TLB.PAS file.)

You can then add the IMAPI2_TLB to your uses clause, and simply create an instance of it in your code:

procedure TYourForm.OnDiskUpdate(Sender: TObject;
const IObject: IDispatch; const Progress: IDispatch);
begin
// Your code here
end;

procedure TYourForm.WriteSomethingToDisk;
var
Writer: TMsftDiscFormat2Data;
begin
Writer := TMsftDiscFormat2Data.Create(nil);
Writer.OnUpdate := OnDiskUpdate;
end;

Much easier, don't you think?


Hertwig

unread,
Oct 23, 2007, 7:19:55 PM10/23/07
to
Thanks Ken,

I am using D7 and I can not import the IMAPI2 dll. Then I installed the
D2007 trial to imported the file and use the files in D7. That works great.

Thank you so much

Hert

"Ken White" <delph...@gmail.com> wrote in message
news:471ce27a$1...@newsgroups.borland.com...

0 new messages