Calling .NET From COM Originally posted by Mikec276 on C Sharp Friends | ||||||
It might be hard to convince your IT manager to let you build your entire system from scratch in C#, just because you think it is cool. A better trick is to start building new bits with .NET, and slowly integrate them into the old system before anyone has a chance to discover what you are up to ;-). But for that you have to be able to call them from your existing COM systems. It turns out that this is very easy. Here is a simple example of calling a C# object from a COM technology platform (vbscript). General Steps 1. Build a simple C# class, inside say a Library assembly (DLL). 2. Use the Framework-provided REGASM tool to add the registry entries necessary for it to be called via COM. 3. Call your object from the 'legacy' code. The C# Class A simple class to demonstrate this functionality, with a couple of simple string and int properties, is as follows:
Registering the .NET Assembly The .net framework provides a tool called REGASM.EXE, that you can use to create the necessary registry entries for your new assembly. It is simple to use, from the command prompt simply type:
RegAsm warning: No types were registered Then it may be because your potential COM-accessible classes do not have no-args constructors required to make them COM-able (yes I discovered this the hard way ;-) ). Call the Object with COM For this example we will call the Customer object(s) from Vbscript. For this to work, we need to copy the DLL into the path of the wscript/cscript applications, which are found in C:\WINNT\System32. I will explain later how we can avoid this step. Then create a VBScript file, something like the following:
Customer exists: True The other saying Customer Name: Frank Smith Hat Size: [some number] You can of course alter the code to suit your requirements, calling the C# code from VB, C++, Jscript or any other COM-enabled 'client'. Changing the ProgID You can give your class any ProgID you like, if you don't like the default of [namespace].[classname]. To do this, simply add
Making the Assembly Globally Accessible In the above example we had to copy the assembly into the path of the 'client', which was the scripting host (wscript or cscript). To avoid this step, and make the assembly globally accessible, we simply give the assembly a strong name, and register it with the .net Global Assembly Cache (GAC) in the usual manner. I'm going to assume you know how to do that, as it is a general .NET detail and not specific to COM interoperability. Using the GAC works because the .NET CLR is, at runtime, creating what is know as a COM Callable Wrapper (CCW) for your assembly. If your assembly is not in the GAC, and not in the path of the client, the CCW simply won't find your assembly, just as for standard multi-assembly .NET programming. Incidently the error message you get back is not overly helpful (yes I found this out the hard way too ;-) ), the VB Error object has the following data in it: Error Number 0x80131522 (-2146233054) Source: (null) If you ever see this, then what it means is that the CCW can't find your assembly for some reason - it is not in the path of the client, nor was it found in the GAC. or any other COM-enabled 'client'. Take it to the Streets So now you have the knowledge you need to infiltrate your existing legacy systems with shiny new C# objects (which is what we REALLY want to write things in, right? ;-) ). |
A blog about simple C# and VB.NET code samples and general sharing with the professional development community at large.
Monday, August 20, 2018
Subscribe to:
Post Comments (Atom)
Calling .NET From COM Originally posted by Mikec276 on C Sharp Friends It might be hard to convince your IT manager to let you build y...
-
Dependencies: DocumentFormat.OpenXml.dll (version 2.0.3930.0) Imports: DocumentFormat.OpenXml.WordProcessing DocumentFormat.OpenXml.Packagin...
-
Code Snippet For Each contControl As Word.ContentControl In ThisAddin.MyAddIn.Application.ActiveDocument.ContentControls ...
-
Lately I had an opportunity to interview with a great company. The Vice President of IT was tremendously impressed with my credentials and ...
No comments:
Post a Comment