通过delphi(IED为delphiXE10)封装ocx,实现火狐js调用本地文件(.ini)数据交互

ocx代码,下面代码为整个ocx实现体,其中CallInt函数为本文真正实现

unit MyActiveForm;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ActiveX, AxCtrls, MyYBActiveX_TLB,IniFiles, ShellAPI, StdVcl;

type
  TMyActiveX = class(TActiveForm, IMyActiveX)
  private
    iniFile: TIniFile;
    { Private declarations }
    FEvents: IMyActiveXEvents;
    procedure ActivateEvent(Sender: TObject);
    procedure ClickEvent(Sender: TObject);
    procedure CreateEvent(Sender: TObject);
    procedure DblClickEvent(Sender: TObject);
    procedure DeactivateEvent(Sender: TObject);
    procedure DestroyEvent(Sender: TObject);
    procedure KeyPressEvent(Sender: TObject; var Key: Char);
    procedure MouseEnterEvent(Sender: TObject);
    procedure MouseLeaveEvent(Sender: TObject);
    procedure PaintEvent(Sender: TObject);
  protected
    { Protected declarations }
    procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
    procedure EventSinkChanged(const EventSink: IUnknown); override;
    function Get_Active: WordBool; safecall;
    function Get_AlignDisabled: WordBool; safecall;
    function Get_AlignWithMargins: WordBool; safecall;
    function Get_AutoScroll: WordBool; safecall;
    function Get_AutoSize: WordBool; safecall;
    function Get_AxBorderStyle: TxActiveFormBorderStyle; safecall;
    function Get_Caption: WideString; safecall;
    function Get_Color: OLE_COLOR; safecall;
    function Get_DockSite: WordBool; safecall;
    function Get_DoubleBuffered: WordBool; safecall;
    function Get_DropTarget: WordBool; safecall;
    function Get_Enabled: WordBool; safecall;
    function Get_ExplicitHeight: Integer; safecall;
    function Get_ExplicitLeft: Integer; safecall;
    function Get_ExplicitTop: Integer; safecall;
    function Get_ExplicitWidth: Integer; safecall;
    function Get_Font: IFontDisp; safecall;
    function Get_HelpFile: WideString; safecall;
    function Get_KeyPreview: WordBool; safecall;
    function Get_MouseInClient: WordBool; safecall;
    function Get_ParentCustomHint: WordBool; safecall;
    function Get_ParentDoubleBuffered: WordBool; safecall;
    function Get_PixelsPerInch: Integer; safecall;
    function Get_PopupMode: TxPopupMode; safecall;
    function Get_PrintScale: TxPrintScale; safecall;
    function Get_Scaled: WordBool; safecall;
    function Get_ScreenSnap: WordBool; safecall;
    function Get_SnapBuffer: Integer; safecall;
    function Get_UseDockManager: WordBool; safecall;
    function Get_Visible: WordBool; safecall;
    function Get_VisibleDockClientCount: Integer; safecall;
    procedure _Set_Font(var Value: IFontDisp); safecall;
    procedure Set_AlignWithMargins(Value: WordBool); safecall;
    procedure Set_AutoScroll(Value: WordBool); safecall;
    procedure Set_AutoSize(Value: WordBool); safecall;
    procedure Set_AxBorderStyle(Value: TxActiveFormBorderStyle); safecall;
    procedure Set_Caption(const Value: WideString); safecall;
    procedure Set_Color(Value: OLE_COLOR); safecall;
    procedure Set_DockSite(Value: WordBool); safecall;
    procedure Set_DoubleBuffered(Value: WordBool); safecall;
    procedure Set_DropTarget(Value: WordBool); safecall;
    procedure Set_Enabled(Value: WordBool); safecall;
    procedure Set_Font(const Value: IFontDisp); safecall;
    procedure Set_HelpFile(const Value: WideString); safecall;
    procedure Set_KeyPreview(Value: WordBool); safecall;
    procedure Set_ParentCustomHint(Value: WordBool); safecall;
    procedure Set_ParentDoubleBuffered(Value: WordBool); safecall;
    procedure Set_PixelsPerInch(Value: Integer); safecall;
    procedure Set_PopupMode(Value: TxPopupMode); safecall;
    procedure Set_PrintScale(Value: TxPrintScale); safecall;
    procedure Set_Scaled(Value: WordBool); safecall;
    procedure Set_ScreenSnap(Value: WordBool); safecall;
    procedure Set_SnapBuffer(Value: Integer); safecall;
    procedure Set_UseDockManager(Value: WordBool); safecall;
    procedure Set_Visible(Value: WordBool); safecall;
    function CallInt(const Param, Param2: WideString): WideString; safecall;

  public
    { Public declarations }
    procedure Initialize; override;
  end;

implementation

uses ComObj, ComServ;

{$R *.DFM}

{ TMyActiveX }

procedure TMyActiveX.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
begin
  { Define property pages here.  Property pages are defined by calling
    DefinePropertyPage with the class id of the page.  For example,
      DefinePropertyPage(Class_MyActiveXPage); }
end;

procedure TMyActiveX.EventSinkChanged(const EventSink: IUnknown);
begin
  FEvents := EventSink as IMyActiveXEvents;
  inherited EventSinkChanged(EventSink);
end;

procedure TMyActiveX.Initialize;
begin
  inherited Initialize;
  OnActivate := ActivateEvent;
  OnClick := ClickEvent;
  OnCreate := CreateEvent;
  OnDblClick := DblClickEvent;
  OnDeactivate := DeactivateEvent;
  OnDestroy := DestroyEvent;
  OnKeyPress := KeyPressEvent;
  OnMouseEnter := MouseEnterEvent;
  OnMouseLeave := MouseLeaveEvent;
  OnPaint := PaintEvent;
end;

function TMyActiveX.Get_Active: WordBool;
begin
  Result := Active;
end;

function TMyActiveX.Get_AlignDisabled: WordBool;
begin
  Result := AlignDisabled;
end;

function TMyActiveX.Get_AlignWithMargins: WordBool;
begin
  Result := AlignWithMargins;
end;

function TMyActiveX.Get_AutoScroll: WordBool;
begin
  Result := AutoScroll;
end;

function TMyActiveX.Get_AutoSize: WordBool;
begin
  Result := AutoSize;
end;

function TMyActiveX.Get_AxBorderStyle: TxActiveFormBorderStyle;
begin
  Result := Ord(AxBorderStyle);
end;

function TMyActiveX.Get_Caption: WideString;
begin
  Result := WideString(Caption);
end;

function TMyActiveX.Get_Color: OLE_COLOR;
begin
  Result := OLE_COLOR(Color);
end;

function TMyActiveX.Get_DockSite: WordBool;
begin
  Result := DockSite;
end;

function TMyActiveX.Get_DoubleBuffered: WordBool;
begin
  Result := DoubleBuffered;
end;

function TMyActiveX.Get_DropTarget: WordBool;
begin
  Result := DropTarget;
end;

function TMyActiveX.Get_Enabled: WordBool;
begin
  Result := Enabled;
end;

function TMyActiveX.Get_ExplicitHeight: Integer;
begin
  Result := ExplicitHeight;
end;

function TMyActiveX.Get_ExplicitLeft: Integer;
begin
  Result := ExplicitLeft;
end;

function TMyActiveX.Get_ExplicitTop: Integer;
begin
  Result := ExplicitTop;
end;

function TMyActiveX.Get_ExplicitWidth: Integer;
begin
  Result := ExplicitWidth;
end;

function TMyActiveX.Get_Font: IFontDisp;
begin
  GetOleFont(Font, Result);
end;

function TMyActiveX.Get_HelpFile: WideString;
begin
  Result := WideString(HelpFile);
end;

function TMyActiveX.Get_KeyPreview: WordBool;
begin
  Result := KeyPreview;
end;

function TMyActiveX.Get_MouseInClient: WordBool;
begin
  Result := MouseInClient;
end;

function TMyActiveX.Get_ParentCustomHint: WordBool;
begin
  Result := ParentCustomHint;
end;

function TMyActiveX.Get_ParentDoubleBuffered: WordBool;
begin
  Result := ParentDoubleBuffered;
end;

function TMyActiveX.Get_PixelsPerInch: Integer;
begin
  Result := PixelsPerInch;
end;

function TMyActiveX.Get_PopupMode: TxPopupMode;
begin
  Result := Ord(PopupMode);
end;

function TMyActiveX.Get_PrintScale: TxPrintScale;
begin
  Result := Ord(PrintScale);
end;

function TMyActiveX.Get_Scaled: WordBool;
begin
  Result := Scaled;
end;

function TMyActiveX.Get_ScreenSnap: WordBool;
begin
  Result := ScreenSnap;
end;

function TMyActiveX.Get_SnapBuffer: Integer;
begin
  Result := SnapBuffer;
end;

function TMyActiveX.Get_UseDockManager: WordBool;
begin
  Result := UseDockManager;
end;

function TMyActiveX.Get_Visible: WordBool;
begin
  Result := Visible;
end;

function TMyActiveX.Get_VisibleDockClientCount: Integer;
begin
  Result := VisibleDockClientCount;
end;

procedure TMyActiveX._Set_Font(var Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure TMyActiveX.ActivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnActivate;
end;

procedure TMyActiveX.ClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnClick;
end;

procedure TMyActiveX.CreateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnCreate;
end;

procedure TMyActiveX.DblClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDblClick;
end;

procedure TMyActiveX.DeactivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDeactivate;
end;

procedure TMyActiveX.DestroyEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDestroy;
end;

procedure TMyActiveX.KeyPressEvent(Sender: TObject; var Key: Char);
var
  TempKey: Smallint;
begin
  TempKey := Smallint(Key);
  if FEvents <> nil then FEvents.OnKeyPress(TempKey);
  Key := Char(TempKey);
end;

procedure TMyActiveX.MouseEnterEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnMouseEnter;
end;

procedure TMyActiveX.MouseLeaveEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnMouseLeave;
end;

procedure TMyActiveX.PaintEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnPaint;
end;

procedure TMyActiveX.Set_AlignWithMargins(Value: WordBool);
begin
  AlignWithMargins := Value;
end;

procedure TMyActiveX.Set_AutoScroll(Value: WordBool);
begin
  AutoScroll := Value;
end;

procedure TMyActiveX.Set_AutoSize(Value: WordBool);
begin
  AutoSize := Value;
end;

procedure TMyActiveX.Set_AxBorderStyle(Value: TxActiveFormBorderStyle);
begin
  AxBorderStyle := TActiveFormBorderStyle(Value);
end;

procedure TMyActiveX.Set_Caption(const Value: WideString);
begin
  Caption := TCaption(Value);
end;

procedure TMyActiveX.Set_Color(Value: OLE_COLOR);
begin
  Color := TColor(Value);
end;

procedure TMyActiveX.Set_DockSite(Value: WordBool);
begin
  DockSite := Value;
end;

procedure TMyActiveX.Set_DoubleBuffered(Value: WordBool);
begin
  DoubleBuffered := Value;
end;

procedure TMyActiveX.Set_DropTarget(Value: WordBool);
begin
  DropTarget := Value;
end;

procedure TMyActiveX.Set_Enabled(Value: WordBool);
begin
  Enabled := Value;
end;

procedure TMyActiveX.Set_Font(const Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure TMyActiveX.Set_HelpFile(const Value: WideString);
begin
  HelpFile := string(Value);
end;

procedure TMyActiveX.Set_KeyPreview(Value: WordBool);
begin
  KeyPreview := Value;
end;

procedure TMyActiveX.Set_ParentCustomHint(Value: WordBool);
begin
  ParentCustomHint := Value;
end;

procedure TMyActiveX.Set_ParentDoubleBuffered(Value: WordBool);
begin
  ParentDoubleBuffered := Value;
end;

procedure TMyActiveX.Set_PixelsPerInch(Value: Integer);
begin
  PixelsPerInch := Value;
end;

procedure TMyActiveX.Set_PopupMode(Value: TxPopupMode);
begin
  PopupMode := TPopupMode(Value);
end;

procedure TMyActiveX.Set_PrintScale(Value: TxPrintScale);
begin
  PrintScale := TPrintScale(Value);
end;

procedure TMyActiveX.Set_Scaled(Value: WordBool);
begin
  Scaled := Value;
end;

procedure TMyActiveX.Set_ScreenSnap(Value: WordBool);
begin
  ScreenSnap := Value;
end;

procedure TMyActiveX.Set_SnapBuffer(Value: Integer);
begin
  SnapBuffer := Value;
end;

procedure TMyActiveX.Set_UseDockManager(Value: WordBool);
begin
  UseDockManager := Value;
end;

procedure TMyActiveX.Set_Visible(Value: WordBool);
begin
  Visible := Value;
end;

function TMyActiveX.CallInt(const Param, Param2: WideString): WideString;
var
  FileIni:TIniFile;//定义一个ini文件
  JHadd:string;//用来存放ini文件地址
  JHExeAdd:string;//用来存放ini文件地址
  par,temp,fileName,xtjm,wjjm: WideString;
  strs:TStrings;
  i,j :Integer;
begin
  fileName:='D:/callIntinterface.ini';
  try
      if not FileExists(fileName) then
      begin
        Result := '-1|没有找到'+ fileName + '|';
        Exit;
      end;
      iniFile := TIniFile.Create(fileName);
      try
        wjjm:= iniFile.ReadString('wym','wymStr','-1');//文件加密
        JHadd:= iniFile.ReadString('JHadd','JHaddS','-1'); //取叫号地址
        JHExeAdd:= iniFile.ReadString('JHadd','JHExeAdd','-1'); //取叫号地址
        xtjm:=Param2;
      finally
         FileIni.free();
      end;
        //判断文件加密和系统加密  CompareStr(wjjm)
        if trim(wjjm) <> trim(xtjm) then
        begin
          Result :='-1|错误,请及时联系开发商!'+'|';
          Exit;
        end;
        if  JHadd = '-1' then
        begin
          Result :='-1|错误,请维护接口data.ini文件路径地址'+'|';
          Exit;
        end;
        //分解param参数
        strs := TStringList.Create;
        strs.Delimiter:='|';
        strs.DelimitedText:=Param;
        //获取data.ini的文件并进行操作
        try
          FileIni:=TIniFile.Create(JHadd);
          for i := 0 to strs.Count-1 do
          begin
            case i of
              0: FileIni.WriteString('Send','Name',Strs[i]);//姓名。
              1: FileIni.WriteString('Send','Money',Strs[i]); //金额;
              2: FileIni.WriteString('Sound','Coding',Strs[i]); //呼叫类别;
            end;
          end;
        finally
          FileIni.free();
        end;
        ShellExecute(handle, nil, pchar(JHExeAdd), nil, nil, SW_SHOWNORMAL);
        FileIni:=TIniFile.Create(JHadd);
        try
          temp:= FileIni.ReadString('Return','Return','-1');//读取是否发送成功
        finally
         FileIni.free();
        end;
  except
      on E:Exception do
      begin
        Result := '-1|发生异常' + E.Message + '|';
        Exit;
      end;
  end;
  Result:=temp;
end;

initialization
  TActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    TMyActiveX,
    Class_MyActiveX,
    1,
    '',
    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
    tmApartment);
end.

 delphi生成ocx后,如果是winxp系统,则将ocx放入C:WindowsSystem32下,执行,否则放入放入C:WindowsSystem执行

regavr32 C:WindowsSystem32ocx名.ocx
注意:此命令如果是win7 家庭版或者win8/win10,则需要打开windows PowerShell(管理员命令窗口)执行

 js调用ocx的demo

<HTML>
<HEAD>
<TITLE>OCX读写文件测试</TITLE> 


<SCRIPT LANGUAGE="JavaScript">

	function readocxfile()
	{
	debugger;
		var StringInput = document.getElementById("StringInput");
		if (!StringInput)
		{
			
			var param = [];
			var ret =this.getObject();
			ret = test.CallInt("王八蛋|2.0|3","445381099");
			alert("返回值:" + ret);
		}
	}

	function writeocxfile()
	{
		if (StringInput.value != " ")
		{
			var ret;
			ret = test.WriteFile("mzghsk",StringInput.value);
			alert("返回值:" + ret);
		}
	}
	
	
		function getObject(){
		var id = "test";
			if (window.document[id]) {
				return window.document[id];
			}
			if (isIE) {
				if (document.embeds && document.embeds[id])
				  return document.embeds[id]; 
			} 
			else {
				return document.getElementById(id);
			}
		}
	
</SCRIPT>


</HEAD>
<BODY>
<center>
   调用OCX示例
<p></p> 
<object id="test" TYPE="application/x-itst-activex" ALIGN="baseline" BORDER="0" WIDTH="0" HEIGHT="0" clsid="{87049477-31D9-4414-8583-26E5B62F9EB7}" ></object>
<p></p> 
输入参数: <INPUT TYPE ="text" NAME="StringInput" VALUE="">
<p></p>
<INPUT TYPE="button" NAME="Submit" VALUE="读文件" ONCLICK=readocxfile()>
<INPUT TYPE="button" NAME="writefile" VALUE="写文件" ONCLICK=writeocxfile()><br>
</SCRIPT>
</center>
</BODY>
原文地址:https://www.cnblogs.com/js2ja/p/11672245.html