2006-05-30 16:23:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
unit uPSR_comobj;
|
|
|
|
|
|
|
|
{$I PascalScript.inc}
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
uPSRuntime, uPSUtils;
|
|
|
|
|
|
|
|
|
|
|
|
procedure RIRegister_ComObj(cl: TPSExec);
|
|
|
|
|
|
|
|
implementation
|
2014-05-19 10:11:51 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$IFDEF PS_FPC_HAS_COM}
|
|
|
|
uses SysUtils, ComObj;
|
|
|
|
{$ENDIF}
|
2006-05-30 16:23:25 +02:00
|
|
|
{$ELSE}
|
2014-05-19 10:11:51 +02:00
|
|
|
{$IFDEF DELPHI3UP}
|
|
|
|
uses ComObj;
|
|
|
|
{$ELSE}
|
|
|
|
uses SysUtils, Ole2;
|
|
|
|
{$ENDIF}
|
2006-05-30 16:23:25 +02:00
|
|
|
{$ENDIF}
|
|
|
|
{$IFNDEF DELPHI3UP}
|
|
|
|
|
|
|
|
{$IFDEF DELPHI3UP }
|
|
|
|
resourceString
|
|
|
|
{$ELSE }
|
|
|
|
const
|
|
|
|
{$ENDIF }
|
|
|
|
|
|
|
|
RPS_OLEError = 'OLE error %.8x';
|
|
|
|
function OleErrorMessage(ErrorCode: HResult): String;
|
|
|
|
begin
|
|
|
|
Result := SysErrorMessage(ErrorCode);
|
|
|
|
if Result = '' then
|
|
|
|
Result := Format(RPS_OLEError, [ErrorCode]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure OleError(ErrorCode: HResult);
|
|
|
|
begin
|
|
|
|
raise Exception.Create(OleErrorMessage(ErrorCode));
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure OleCheck(Result: HResult);
|
|
|
|
begin
|
|
|
|
if Result < 0 then OleError(Result);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure CreateOleObject(const ClassName: string; var Disp: IDispatch);
|
|
|
|
var
|
|
|
|
OldDisp: IDispatch;
|
|
|
|
ClassID: TCLSID;
|
|
|
|
WideCharBuf: array[0..127] of WideChar;
|
|
|
|
begin
|
|
|
|
StringToWideChar(ClassName, WideCharBuf, SizeOf(WideCharBuf) div SizeOf(WideCharBuf[0]));
|
|
|
|
OleCheck(CLSIDFromProgID(WideCharBuf, ClassID));
|
|
|
|
if Disp <> nil then
|
|
|
|
begin
|
|
|
|
OldDisp := Disp;
|
|
|
|
Disp := nil;
|
|
|
|
OldDisp.Release;
|
|
|
|
end;
|
|
|
|
OleCheck(CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or
|
|
|
|
CLSCTX_LOCAL_SERVER, IID_IDispatch, Disp));
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure GetActiveOleObject(const ClassName: string; var Disp: IDispatch);
|
|
|
|
var
|
|
|
|
Unknown: IUnknown;
|
|
|
|
OldDisp: IDispatch;
|
|
|
|
ClassID: TCLSID;
|
|
|
|
WideCharBuf: array[0..127] of WideChar;
|
|
|
|
begin
|
|
|
|
StringToWideChar(ClassName, WideCharBuf, SizeOf(WideCharBuf) div SizeOf(WideCharBuf[0]));
|
|
|
|
OleCheck(CLSIDFromProgID(WideCharBuf, ClassID));
|
|
|
|
OleCheck(GetActiveObject(ClassID, nil, Unknown));
|
|
|
|
try
|
|
|
|
if Disp <> nil then
|
|
|
|
begin
|
|
|
|
OldDisp := Disp;
|
|
|
|
Disp := nil;
|
|
|
|
OldDisp.Release;
|
|
|
|
end;
|
|
|
|
OleCheck(Unknown.QueryInterface(IID_IDispatch, Disp));
|
|
|
|
finally
|
|
|
|
Unknown.Release;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
|
|
|
|
|
|
procedure RIRegister_ComObj(cl: TPSExec);
|
|
|
|
begin
|
2014-05-19 10:11:51 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$IFDEF PS_FPC_HAS_COM}
|
|
|
|
cl.RegisterDelphiFunction(@OleCheck, 'OleCheck', cdRegister);
|
|
|
|
cl.RegisterDelphiFunction(@StringToGUID, 'StringToGUID', cdRegister);
|
|
|
|
cl.RegisterDelphiFunction(@CreateComObject, 'CreateComObject', cdRegister);
|
|
|
|
cl.RegisterDelphiFunction(@CreateOleObject, 'CREATEOLEOBJECT', cdRegister);
|
|
|
|
cl.RegisterDelphiFunction(@GetActiveOleObject, 'GETACTIVEOLEOBJECT', cdRegister);
|
|
|
|
{$ENDIF}
|
|
|
|
{$ELSE}
|
2009-09-25 15:47:54 +02:00
|
|
|
cl.RegisterDelphiFunction(@OleCheck, 'OleCheck', cdRegister);
|
2009-09-24 21:13:45 +02:00
|
|
|
{$IFNDEF PS_NOINTERFACES}
|
|
|
|
{$IFDEF DELPHI3UP}
|
|
|
|
cl.RegisterDelphiFunction(@StringToGUID, 'StringToGUID', cdRegister);
|
|
|
|
cl.RegisterDelphiFunction(@CreateComObject, 'CreateComObject', cdRegister);
|
|
|
|
{$ENDIF}
|
|
|
|
{$ENDIF}
|
2006-05-30 16:23:25 +02:00
|
|
|
cl.RegisterDelphiFunction(@CreateOleObject, 'CREATEOLEOBJECT', cdRegister);
|
|
|
|
cl.RegisterDelphiFunction(@GetActiveOleObject, 'GETACTIVEOLEOBJECT', cdRegister);
|
2014-05-19 10:11:51 +02:00
|
|
|
{$ENDIF}
|
2006-05-30 16:23:25 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|