How can I dynamically instantiate an ActiveX control from an .ocx file? - c#

I would like to create an instance of an ActiveX control. Given the .ocx file and interface definitions in C#, how can I do this? I don't mean the usual way of adding a reference to the project because I want to avoid the need for registering the control globally on the target system.

This looks like you should check into Registration-free COM. Beware that this does not work on older systems IIRC.
For some details/samples etc. see:
http://msdn.microsoft.com/en-us/library/fh1h056h.aspx
http://blogs.msdn.com/b/junfeng/archive/2006/04/20/579748.aspx
http://apocryph.org/2010/05/23/getting-reg-free-com-activation-working-between-managed-and-unmanaged-dlls/
http://dotnetforum.net/topic/21006-manifest-resource-refference-in-c-net-application-com-reg-free-instantiation/
http://www.codeproject.com/Articles/13391/Using-IFilter-in-C

Related

Add-ons for your C# app

I have a C# program and I want to make it able to load addons. Addons are like that: You pass a control or form to it and it makes some changes to them.
Is there anyway to have dll addons and load them dynamically at runtime and pass controls??
If not Any Idea? What should I do?
Look into MEF to load and import the assemblies and the controls.
Look at Management Extensibility Framework.
Yes that's possible and it works similar to other programming languages: Load the library and call methods to get names and stuff. One possible solution can be found in this question.

using [interopformproperty] prevents wrapper.cs file generation

I have a problem/question with some basic stuff.
I have interoptoolkit v2.1 installed and I am trying to create this form in vs 2008 with C# so this form can be accessed from a vb6 application.
I am trying to add a public property on this form, so I am thinking I am supposed to add this
[interopformproperty] above the property.
But when I do this and try to generate the wrapper using the option under Tools menu, it doesn't generate the wrapper.
I obviously searched google for a while but I couldn't find something particularly helpful.
So if you know anything about this, please give us hand.
Thanks a lot in advance. Cheers.
If the com visible type implements an interace, all the public methods, properties are com visible, provided that the types uses in their signatures are com visible. If the com visible type implements multiple interfaces, the first one (in the class definition) is used as the com interface. This behavior can be changed using the ComDefaultInterfaceAttibute.
If an interface is not implemented, all the public properties (that expose a com visible type) are automatically com visible.
regasm automatically generates a com inteface, containing all public methods/propties.
"Generating a wrapper" as you say... I don't understand this.
After compiling an assembly with com visible types register it using regasm (if the option in project properties "Register for COM interop is not active).
I always end up answering my own question :D
I am doing this just in case someone else get on this.
Go to this link and download the 2nd file.
C_Interop_Form.aspx">http://www.codeproject.com/KB/dotnet/VB6_-C_Interop_Form.aspx
Assuming you are using vs2008. Get the dll file, and the add in file.
and copy this file into My documents -> visual studio 2008>addins
Open up the addin file with notepad, and change the version number 8.0 -> *
Now save it. and reload vs2008. It should generate the wrapper file in C# instead of vb.
P.S, reading the comment of the post always helps :D

.NET Custom Control Resources.resx

I have a C# custom control that loads images from Resources.resx. I was loading this resources into the Project's Resources and then accessing them like:
ProjectNamespace.Properties.Resources.resourcename;
This works for one project but now I want to use my control in multiple projects.
What's the best way to handle this? Load the resources into the controls .resx? How can I access them from there? Or should I approach this completely differently?
It should work as is, even if your control is used from other projects.
The code generated by VS is a wrapper around the ResourceManager class, and it gives the assembly of your control as a constructor parameter. So, the ResourceManager always knows where to look for resources.

How do I serialize a .NET control to CODE?

I want to create a .NET Form at runtime, add buttons and other controls to that (also at runtime), and then be able to generate a something.designer.cs file from that form (which can then be added to a C# solution and compiled).
What I want to do is very similar to what the WinForm designer does. But instead of having a drag/drop interface for the user, I want to dynamically build the Form/Controls myself at runtime.
I was thinking I could just reuse what the WinForm designer is doing.
Is that possible?
This MSDN magazine article should have everything you need.
It's really not as simple as it was pre-.NET as the visual version of the form you see in Visual Studio is actually the result of multiple files.
But in the simplest form you could simply just mirror what .NET does at the start of creating a new form:
Create three files Form.cs, Form.Designer.cs and Form.resx (which is an XML file).
Place the same default content in them that VS does
Mimic the code generated when adding controls, code-behind and resources
It will be a tedious task, but it can be done. Adding resources however will be burdensome.
Yes, you can do achieve this using Compiler Services (compiling c# code) or Emit class if you know building correct MSIL.

Creating new C# version of existing COM object to use in Delphi program

We had a bit of a problem where we have lost some source code for a very old ActiveX component we had. We've got a Delphi7 program that calls the Active X component. Thats a bit of a moster and can't be changed too much. However, some of the functionality in the Active X component needs to be updated. Since we've been moving our apps to C#, its been decided to redo the ActiveX as a C# class library.
I've got a basic C# class library working to prove the concept. I can hack the GUIDs and registry so it just looks liek the old one. If I look at the type lib for the old one and the new one, they are basically the same. Couple of slight differences but not anything i would have thought would be a problem.
Now as far as I can see there are three ways to use the new class library:
Late binding by looking up the ProgId. This works ok.
Import the type library. (Project| Import Type Library) This works ok.
Import as an ActiveX component. This generates a tlb file. This doesn't work. When I go to create the component I get 'No such interface exists'. The class is loading to a point but not completely.
Now unfortunately, the Delphi program is using option 3. So I have to try to get my class library working in this circumstance. Any ideas how I get past that error? I googled and found some references but nothing that says what the problem is.
Second question. I should know how to do this since I've done it once but I seemed to have fluked it. When I am in Delphi I do an Import ActiveX component to generate a TLB file. Now the first C# class library generated is showing up in the list of ActiveX controls and I can Create Unit for it. But I've created another simplier class library for testign purposes and damned if I can get the library to show in the list of ActiveX controls and so I can't import it.
The Create Unit for the Active X component generates a different format of TLB.pas to the Import Type Library format and its the ActiveX one that I require.
An ActiveX Control does way more then just implementing your interfaces. It is more like an TComponent or TWinControl. It requires you to implement a lot more ActiveX Control interfaces. Your C# object exposed through COM/ActiveX is not a Control, but a simple class.
It does not show up in Import ActiveX Control since it is not an ActiveX Control. I do not think it is feasible to make your C# class an Active Control.
Another possibility, although probably not the most elegant, would be to create a new ActiveX com object in Delphi which in turn calls your C# class by either method which you already have working.
Since you stated that this is for an existing Delphi 7 application, you might want to just use Com Interop on the C# side, expose the object as com visible, and the consume it directly from your Delphi 7 application.
The first option would probably allow you to get by with minimal changes to your existing application, but does add an additional layer that most likely is not necessary.

Categories