Creation of .Ocx File from a .Net Dll - c#

I have a .Net Com Dll is it possible to use this dll and create .OCX file in c++ or MFC. If yes what are all the steps which needs to be followed. If any sample code is availabe that would be a great help

You could expose the .NET assembly as COM object using the regasm.exe tool. You could use the [ComVisible(true)] to indicate that all classes should be visible by COM clients when registered. This assembly level attribute could also be set in the properties of the project in Visual Studio. You could also apply it only to some classes that need to be exported. Once the assembly registered as COM object you could instantiate any class from unmanaged clients as with any standard COM object.

There is nothing particularly special about an .ocx file, it is just a DLL. Microsoft came up with that filename extension back in the Visual Basic version 4 days to make it obvious to VB programmers that they had a DLL that contains controls. ActiveX controls as opposed to VBX controls from the 16-bit days.
If you made the .NET assembly [ComVisible] then you already have a COM server that's usable in other projects. Provided you registered it properly, .NET assemblies must be registered with Regasm.exe instead of Regsvr32.exe. Done automatically in a .NET project with the Project + Properties, Build tab, "Register for COM interop" option. And at installation time with a Setup and Deployment project. If you need a type library then use Regasm.exe with the /tlb and /codebase options. Or Tlbexp.exe
If this really needs to be a traditional .ocx, in other words have controls, then you can use a Winforms UserControl or your own class derived from a Winforms control. Winforms automatically implements the plumbing to make classes derived from the Control class function properly in an ActiveX host.

If you're wanting to use a .NET library in normal C++, there are ways, mostly involving COM Interop. Microsoft has a whole section of MDSN dedicated to COM Interop: http://msdn.microsoft.com/en-us/library/6bw51z5z%28v=VS.71%29.aspx.
If the .NET DLL supports COM Interop, use that.
Try using VC++'s #import directive to read the .NET DLL in as a COM object. When compiled, VC++ should output a .tlh file and a .thi file (header and implementation respectively) which will be automatically compiled into your project. But this may or may not work depending on the DLL's construction, dependencies, etc.
Look at creating your own COM Interop .NET wrapper library that marshals calls to the base .NET DLL.

Related

What is the difference between <COM Reference> and <Reference> in .csproj?

I have 2 types of refences and each of them are working fine.
I tried to use each one and got the same result in project build .
Please explain to me what is the difference between COM Reference and Reference.
Thank you.
COM references are used to reference "legacy" COM libraries (COM is the framework used to connect components before .NET). "References" are used to reference .NET libraries (assemblies).
.NET assemblies can also be COM libraries (meaning there are COM-visible classes in the assembly, and the builder generates the necessary metadata to make the assembly COM-visible), but if possible they should be referenced directly rather then by using COM.
So it's possible that the library you are referencing is both a .NET library and a COM library, but how you connect it to your app is different based on how you reference it.
COM is a technique that is no longer widely used. Previously, we created COM objects that could then be used by other libraries or programs. Typically, you could have a dll, somewhere on the machine, register it with COM and then others could use it without knowing the location, only the signature. Only one component with the same signature per machine was possible.
Today, direct references are usually used, so I would recommend this over COM

Unable to register my DLL using regsvr32, but via IDE works

I have created a DLL using classlibrary ,but is registering via IDE but it unable to register using regsvr32 .
with reference How do I create an ActiveX control (COM) in C#?
A .NET assembly is not a native DLL. You can only use regsvr32 on native DLLs that support self-registration.
To register a .NET assembly, you can use regasm instead. You can find it at this path or a similar one:
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe"
However, in an installer the recommended way to register a COM server is to install the required registry entries instead of using regsvr32 or regasm. The Windows Installer documentation about the SelfReg table lists some reasons why that is.
Regasm.exe is used to create COM Callable Wrapper (CCW) around .NET assemblies. .NET managed assemblies(EXEs, DLLs) are different from COM DLLs (which are unmanaged, ie. they interact with the OS directly). So to register an unmanaged DLL you use regsvr32.exe.
But if you have a managed .NET assembly and want you COM components to use it as if it were a COM assembly, then you need to use regasm.exe.

Statically link COM DLL in C# (avoid regfree)

I have perhaps a silly question:
We have a VC++ COM DLL (developed internally) and we have ported our main app to C# and used COM Interop, reg-free to access the COM dll. Everything works just fine with internal embedded manifest.
However, the friendly product-dev/marketing/sales want to minimize the package and include the COM dll directly. Somehow, someone became convinced that the app distro should include the exe only (since it's unmanaged we can't just ILMerge it in).
Since we have the tlb/lib of the COM, could we somehow statically link it, without porting the whole COM to C# managed re-work?
Thank you
P.S. Pardon my wording: the company was downsized and I am the Python guy who had to learn everything .NET in the last week or so since now I am doing my job and the job of 2 ex-senior .net developers
It looks like Costura can more or less do this.
https://github.com/Fody/Costura
It specifically has support for merging unmanaged assemblies (ie a C++ DLL) into a .NET assembly.
Note - this is not true static linking but would achieve the aim of packaging everything in the single EXE to be distributed.
It is possible to include the source for the COM DLL into the project for the exe, or you could change the COM DLL project into a static lib project. Once you've accomplished that, you must modify the code to create the COM objects directly. All said, neither options are particularly easy.
Alternatively you could look into products like Spoon Studio that would allow you to wrap your exe and COM DLL into one exe without any code.

How do I consume a COM+ local server from C#?

I have a web application from a company that has gone out of business. We're looking to extend the web app a bit with some asp.net functionality. The web app was written as an ISAPI application in Delphi, and uses COM+ to talk to the SQL Server and handles things like session management and authentication.
So, in order to get the current user and other details, I have to use the undocument COM+ components. I was able to dig out the type library and auto generated IDL, but at this point i'm lost in creating a .NET proxy class for this.
Is there a way to autogenerate the .net COM+ proxy either from the .dll itself (extracting the typelib info) or from the IDL?
Note: These seem to be simple COM style objects hosted in COM+ servers, no subscriptions or transaction monitoring..
You could use tlbimp.exe to generate C# proxy classes from your COM library.
tlbimp.exe myTest.tlb /out:myTest.dll
If you don't have the tlb it works also with COM dlls. Once the COM wrapper assembly generated you can reference it in your project and use the types inside as you would any other .NET class.
Possible location of tlbimp.exe : C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\TlbImp.exe or use the Visual Studio Command Prompt.
Run Tlbimp.exe on the type library to generate the .NET interop assembly for it.
Have you tried going to the references in your project, right clicking, add reference, then browsing to the dll. I think visual studio will generate the Runtime Callable Wrapper for you.

Call a .net dll from VB

whether .net framework required to run a .tlb file generated from c#?
You have to:
make sure the classes that you want to use in your VB app are ComVisible
make sure the classes that you want to use in your VB app have a Guid assigned to it
it is best practice to create an interface for those classes
create a tlb file from the assembly using regasm
The .NET framework (runtime) needs to be installed. It is not that regasm will 'compile' your assembly into another language or something that does not need the .NET runtime. A runtime callable wrapper is created, so that you can invoke the .NET assembly as from VB6, which means the .NET runtime is still required.
In VB.Net you can use it normally. In previous version use Runtime Callable Wrapper. More information is here in this KB article.

Categories