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? ;-) ). |
Class Identity Unknown
A blog about simple C# and VB.NET code samples and general sharing with the professional development community at large.
Monday, August 20, 2018
Thursday, January 19, 2012
Banks... Ugh!
Friday, December 2, 2011
Wells Fargo v. PayPal
I received a call shortly after each client setup their PayPal account telling me that they had been contacted by Wells Fargo stating that they had noticed the 'test transactions' posted by PayPal and wanted them to switch to using their merchant account with CyberSource. Interesting to hear that Wells Fargo has clearly programmed some sort of alerts into their system that notifies sales people of someone's intent to use PayPal for something. So if you setup a business account with PayPal and you have a Wells Fargo bank account, be prepared to receive a phone call!!
I would really like someone to convince me how having a monthly merchant account fee + bank transaction fees + annual SSL certificate costs + programming costs, etc., really is less expensive than a free PayPal account with a very nominal fee structure and a small wait time to withdraw monies is going to benefit small businesses that are trying to make ends meat and survive in this economy.
Wednesday, September 22, 2010
Similac Recall good site to ask about your numbers
If you’re having a problem with http://similac.com/recall/lookup.aspx then you can lookup your numbers on this site that other people are posting to http://community.babycenter.com/post/a24404619/similac_recall.
Similac Recall, Lot #91542T20 Powder 1.45 lbs
My wife and I have confirmed that the above lot number HAS been recalled.
Monday, July 26, 2010
Looping through Content Controls in a Word Document
- For Each contControl As Word.ContentControl In ThisAddin.MyAddIn.Application.ActiveDocument.ContentControls
- 'Do something with contcontrol
- Next
He will probably be bored with this job!
Lately I had an opportunity to interview with a great company. The Vice President of IT was tremendously impressed with my credentials and seemed eager to have me join their team. During the interview he stated that I would probably be bored with the job at hand. I told him that for several years now I have been working jobs which were ‘heads down’ and required no critical thinking, problem solving or creative thought of any kind and that the job they were offering would be a pleasant change from the jobs I have been working in for years. Needless to say, I have only heard back from the recruiter once claiming again the company thought I would be bored with the position and the recruiter told me the company was wavering on the contract to perm price. That’s the last I heard of it. I sure wish more people would have the balls to pick up the phone and call someone to let them know they’ve been rejected rather than just not calling. It would have been nice to work for them but oh well, I digress.
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 ...