How to invoke ActiveX method in C++

  activex, c++, ocx, windows

My ocx file is KisPosAgent.ocx. Registry added with regsvr32 ./KisPosAgent.cox. (x86)

I’m trying to invoke ocx methods in C++ (or C#)

I tried three different approaches.

1. C++, CoCreateInstance

#include <stdio.h>
#include <windows.h>
#include <objbase.h>

// This is my ocx (Run cl and tlh, til created)
#import "./KisPosAgent.ocx" named_guids no_namespace


int main(void)
{
   CoInitialize(NULL);
   _DKisPosAgent* pUnknown = NULL;
   HRESULT hr = CoCreateInstance(__uuidof(KisPosAgent), NULL, CLSCTX_INPROC, __uuidof(_DKisPosAgent), (void**)&pUnknown);

   
   // ERROR: _com_error 0x7763B5B2
   short result = pUnknown->Init();
   return 0;
}

2. C#, Just referenced

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace KisPosAgentBridge
{
    class Program
    {
        static void Main(string[] args)
        {
            // Refernece ocx file (and auto-generated Interop.KisPosAgentLib)
            var kis = new KisPosAgentLib.KisPosAgent();

            // ERROR: HRESULT: 0x8000FFFF (E_UNEXPECTED) 
            kis.Init();
        }
    }
}

3. C#, Windows Forms

// Form1.Designer.cs
// Reference ocx file
private AxKisPosAgentLib.AxKisPosAgent axKisPosAgent1;

// Form1.cs
this.axKisPosAgent1.Init(); // Worked!

First, second didn’t worked. When method invokes, got errors.

But, third works well.

What’s difference between first/second and third?

  1. Is activex(ocx) works UI provided environment? (such as windows forms?)
  2. Is error caught because IsInvokeAllowed is not provided? (https://github.com/idobatter/node-win32ole/issues/23)

Source: Windows Questions

LEAVE A COMMENT