Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:26:31+00:00 2026-05-28T21:26:31+00:00

I am trying to instantiate an ActiveX control without registration in a C# project.

  • 0

I am trying to instantiate an ActiveX control without registration in a C# project. I’m doing the following:

Guid guid = new Guid("(guid of my control here)");

var classFactory = ComHelper.GetClassFactoryFromDll("mycontrol.ocx", guid);
if (classFactory != null)
{
    object obj;
    classFactory.CreateInstance(null, ref guid, out obj); // getting the exception here
    if (obj != null) ocx = (obj as IMyControl);
}

The ComHelper code is mostly based on this article and appears to work fine with the IFilter example.

This is my interop code for IClassFactory:

    [ComVisible(false)]
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000001-0000-0000-C000-000000000046")]
    public interface IClassFactory
    {
        void CreateInstance([MarshalAs(UnmanagedType.Interface)] object pUnkOuter, ref Guid refiid,
                            [MarshalAs(UnmanagedType.Interface)] out object ppunk);

        void LockServer(bool fLock);
    }

The IMyControl interface is the interface the aximp.exe /source generated. I want to avoid using satellite assemblies because I can’t make them use reg-free COM and our deployment requires that. What throws the exception? I’m using the same guid I used for the traditional, satellite-assembly way. Is the guid not marshalled correctly? How can I make it work?


For completeness’ sake: I managed to accomplish this. My method was to look at the AxHost source code (used ILSpy) and see what it does when creating an instance:

private object GetOcxCreate()
{
    if (this.instance == null)
    {
        this.CreateInstance();
        this.RealizeStyles();
        this.AttachInterfaces();
        this.oleSite.OnOcxCreate();
    }
    return this.instance;
}

Then I create an instance of the generated AxMyControl class and replaced the instance private field via reflection. That field originally gets a value by calling CoCreateInstance from CreateInstance. Then I called the other methods to fix up other connections (all of them are private, so again, reflection). If the class is not registered, an exception is thrown in the constructor (CoCreateInstance fails), so I do this machination in a finally block.

So far, it seems to work fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:26:31+00:00Added an answer on May 28, 2026 at 9:26 pm

    You are getting the Guid wrong here. The CreateInstance call requires you to pass an interface IID, not a coclass CLSID. Use typeof(IMyControl).GUID instead.

    Next big possible failure mode is the apartment state of the thread, it better be STA or COM is going to hunting for a proxy that probably doesn’t exist. You are bypassing the safety guards that the CLR has in place, marshaling the interface pointer when required. Never use the object from the wrong thread. Not so sure this is worth the risk.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First question here. I am trying to instantiate a generic class using the type
I am trying to instantiate a PDO object like this: $this->pdo['pdo'] = new PDO('mysql:host=127.0.0.1;dbname=mydb;charset=UTF-8',
I'm trying to instantiate a generic class in Spring, but I get following exception:
I'm trying to figure out how to instantiate a case class object with reflection.
I'm trying to create a class that can instantiate arrays at runtime by giving
Title: Eclipse plug-in development Unable to instantiate class due to java.lang.NoClassDefFoundError: Trying to build
Trying to make a make generic select control that I can dynamically add elements
Trying to keep all the presentation stuff in the xhtml on this project and
Trying to make a MySQL-based application support MS SQL, I ran into the following
I'm trying to instantiate a class within a class, so that the outer class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.