0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

COM (ocx)の呼び出し方

Last updated at Posted at 2020-05-08

ocxのインストール

結果 言語 version
hta mshta.exe
ExcelVBA Excel 2010
HSP 3.5
ruby rubyinstaller-2.7.1-1-x86.exe (32bit)
go go1.14.2.windows-amd64.msi
x go go1.14.2.windows-386.msi
(compile.exeがDefenderに誤検知される)
php 7.4.5 (32bit)
vbs cscript.exe (32bit)
VB.NET vbc.exe (/platform:x86)
JScript jsc.exe (/platform:x86)
python3 python-3.8.3rc1.exe (32bit)

成功したもの

○ hta

ocx.hta
<object ID="Sample" CLASSID="clsid:00000000-0000-0000-0000-000000000000"></object>
<script>
alert(Sample.Version());
</script>

○ VBA

Excel
Sub aa()
    Set t = CreateObject("Sample.SampleCtrl.1")
    MsgBox t.Version
End Sub

○ HSP

ocx.hsp
axobj ocx, "Sample.SampleCtrl.1", 300, 300
mes ocx("Version")

○ ruby

  • 今回、MSYSはインストール不要。
ocx.rb
require 'win32ole'

ocx = WIN32OLE.new("Sample.SampleCtrl.1")
ocx.ole_activex_initialize
p ocx.Version()
show_methods.rb
# メソッド一覧を表示
require 'win32ole'

ocx = WIN32OLE.new("Sample.SampleCtrl.1")
p ocx.ole_methods

失敗したもの (致命的エラー)

☓ go

go get github.com/go-ole/go-ole

ocx.go
package main

import (
	ole "github.com/go-ole/go-ole"
	"github.com/go-ole/go-ole/oleutil"
)

func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Sample.SampleCtrl.1")
	ocx, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.MustCallMethod(ocx, "Version", nil).ToIDispatch()
}

☓ php

ocx.php
<?php
//$ocx = new COM("{00000000-0000-0000-0000-000000000000}");
$ocx = new COM("Sample.SampleCtrl.1");

# false
var_dump(method_exists($ocx, "Version"));

echo "<pre>";

# 一覧は表示されるが..
com_print_typeinfo($ocx);

# 致命的エラー
//$ocx->Version;

☓ vbs

ocx.vbs
Set ocx = CreateObject("Sample.SampleCtrl.1")
MsgBox ocx.Version

☓ VB.NET

ocx.vb
Public Module HelloWorld
  Public Sub Main()

    Dim ocx As Object
    ocx = CreateObject("Sample.SampleCtrl.1")
    Console.WriteLine(ocx.Version())

  End Sub
End Module
vbc.exe /platform:x86 ocx.vb

ocx.exe
# ハンドルされていない例外: System.Runtime.InteropServices.COMException: 致命的なエラーです。 (HRESULT からの例外:0x8000FFFF (E_UNEXPECTED))
#    場所 Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
#    場所 Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
#    場所 HelloWorld.Main() 

☓ JScript

ocx.js
var ocx = CreateObject("Sample.SampleCtrl.1")
print(ocx.Version())
jsc.exe /platform:x86 ocx.js

ocx.exe
# ハンドルされていない例外: System.Runtime.InteropServices.COMException: 致命的なエラーです。 (HRESULT からの例外:0x8000FFFF (E_UNEXPECTED))
#    場所 Microsoft.JScript.LateBinding.Call(Object[] arguments, Boolean construct, Boolean brackets, VsaEngine engine)
#    場所 JScript 0.Global Code()
#    場所 JScript Main.Main(String[] )

☓ python3

pip install pywin32
import win32com.client
app = win32com.client.Dispatch("Sample.SampleCtrl.1")

# <bound method Version of <COMObject Sample.SampleCtrl.1>>
print (app.Version)

# 致命的エラー
print (app.Version())

その他

  • ocx は VB6.0 の時代に作られたもの。通常32bit専用。
  • IEのアドオンの管理から該当の拡張機能を有効 → 無効にしても、hta から利用ができた。この設定は見ていない様子。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?