[PYTHON] Learn softly about COM (Component Object Model) Part 2 Is it really language-independent?

Last time

Part 1 Introduction Since it is an introductory edition, let's set aside the theory and try using COM. I did that.

This time, focusing on the fact that COM is "** independent of a specific language **" I would like to call COM in various languages.

All you have to do is start IE, the same as last time.

PHP

ComTest.php


<?php
	$ie = new COM("InternetExplorer.Application");
	$ie->Visible = true;
?>

Only this. The calling method is almost the same as VBS.

Since COM class is prepared in advance in PHP, no additional package etc. is required. http://php.net/manual/ja/class.com.php

Python

ComTest.py


import win32com.client
ie = win32com.client.Dispatch("InternetExplorer.Application")
ie.Visible = True

This is almost the same. However, the COM call function is not included by default, so you need to install it additionally. https://sourceforge.net/projects/pywin32/

C#

Program.cs


using SHDocVw;
namespace ComTestByCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            InternetExplorer ie = new InternetExplorer();
            ie.Visible = true;
        }
    }
}

To use SHDDocVw namespace classes, interfaces You need to add "Microsoft Internet Controls" to the reference.

You can add it from Reference Manager => COM => Type Library.

Some Microsoft documentation about type libraries I couldn't find it, but as a result of googled various places, In short, it seems to be a file that contains the COM interface definition.

By using this, the interface published on COM You can use it as the "type" of the caller.

In the implementation example of VBS, PHP, Python, the object is generated from the character string, so The mechanism around here seems to be different.

VB.NET

vb.net:Module1.vb


Imports SHDocVw
Module Module1
    Sub Main()
        Dim ie As InternetExplorer = New InternetExplorer
        ie.Visible = True
    End Sub
End Module

The implementation method does not change even in VB.NET, a sibling language of C #.

C++

ComTest.cpp


#include "stdafx.h"
#include <atlbase.h>
#include <exdisp.h>
#import "C:\WINDOWS\system32\shdocvw.dll"
int _tmain(int argc, _TCHAR* argv[])
{
	CoInitialize(NULL);
	{
		IWebBrowser2Ptr ie;
		CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (VOID**)&ie);
		ie->put_Visible(VARIANT_TRUE);
	}
	CoUninitialize();
	return 0;
}

I'm loading the type library using the \ #import directive. https://msdn.microsoft.com/ja-jp/library/8etzzkb6.aspx

This allows you to use unique classes and interfaces, just like C # and VB.NET. However, the code is a bit more complicated than in C # or VB.NET.

First, you need to initialize COM before using it, after using it, and to uninitialize it. (CoInitialize(), CoUninitialize())

IWebBrowser2Ptr is a smart pointer for the IWebBrowser interface. It seems to use smart pointers instead of using IWebBrowser directly. (It seems that you can do it without using it, but it has not been investigated)

Also, if CoUninitialize () is executed with the COM object unopened, an error will occur, so By digging one step in the local scope, I am trying to free IWebBrowser2Ptr before calling CoUninitialize ().

CoCreateInstance () creates an actual object, but it has many arguments. https://msdn.microsoft.com/ja-jp/library/windows/desktop/ms686615(v=vs.85).aspx

rclsid => class ID pUnkOuter => It seems that there is no problem with NULL for the time being (uninvestigated) dwClsContext => CLSCTX_LOCAL_SERVER seems to be fine for the time being (not investigated) riid => Interface ID

I'm not sure about the second and third arguments, If I can afford it in the future, I may investigate it properly.

There are many things that I don't understand well. I'm guessing that the reason why I don't understand much in C ++ is that it has a more COM-like implementation. (It wraps well in other languages, so you have to implement the invisible part properly)

The purpose of this time is "to use it for the time being", so I will go through the difficult parts once.

Summary

There were some patterns in the implementation method, I was able to confirm that it can be implemented in various languages.

next time

Part 3 COM component implementation Next time, I've been focusing on the "calling side", I would like to finally make a "caller".

Recommended Posts

Learn softly about COM (Component Object Model) Part 2 Is it really language-independent?
Is it okay with such a patch? (Memo about mock ~ Part 2 ~)