Dynamically loading unmanaged OCX in C #

Source: Internet
Author: User

You'll have an perform a number of steps that is normally taken of automatically when you use the toolbox.

First and foremost, you had to run the Aximp.exe utility to generate the. NET Interop Assemblies. Use the Visual Studio Command Prompt to run the tool. You'll get the assemblies, Axinterop.foo.dll contains a wrapper class that's derived from AxHost and allows you put the Control on a form. And Interop.foo.dll, the interop assembly that makes the COM interfaces implemented by the control callable from a. NET PR Ogram.

Next you has to ensure these DLLs is present in the build directory. Best thing to does is to add them to your project and set their copy to Output Directory to "copy if newer".

Now you can use Assembly.Load ("Axinterop.foo.dll") in your code to dynamically Load the interop Assembly.

Next you has to create a instance of the control, use Assembly.createinstance () and pass the type name of the AxHost WRA Pper class. If you had no idea about its name might is, it isn ' t obvious, then use Ildasm.exe to look at the Axinterop.foo.dll ASSEMBL Y. Cast the returned object to AxHost.

Next you had to add the control to the form's Controls collection so it was visible and usable. You cannot call any interface methods until the control instance are created, that doesn ' t happen until you ' ve added the CO Ntrol and the form ' s Load event has fired.

Next you has to use reflection or the dynamic keyword to obtain a reference to the interfaces implemented by the control, In the need to set properties or call methods. That's difficult to get right, and you'll want to write this code first with the control added from the toolbox so you don ' t h Ave to guess too hard at the proper names.

Dynamically loading unmanaged OCX in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.