1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-22 17:22:21 -05:00
Simba/Units/PascalScript/uPSR_comobj.pas
Raymond 6f80c64d9c PS Added
git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@38 3f818213-9676-44b0-a9b4-5e4c4e03d09d
2009-09-12 06:24:59 +00:00

97 lines
2.1 KiB
ObjectPascal

unit uPSR_comobj;
{$I PascalScript.inc}
interface
uses
uPSRuntime, uPSUtils;
procedure RIRegister_ComObj(cl: TPSExec);
implementation
uses
{$IFDEF DELPHI3UP}
ComObj;
{$ELSE}
SysUtils, Ole2;
{$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
cl.RegisterDelphiFunction(@CreateOleObject, 'CREATEOLEOBJECT', cdRegister);
cl.RegisterDelphiFunction(@GetActiveOleObject, 'GETACTIVEOLEOBJECT', cdRegister);
end;
end.