2

I need to create a COM object on server.

In VB you can do this: (tutorial)

xlApp = CreateObject("Excel.Application", "\\MyServer")

But how to do same thing on C#?

I know how to do it locally:

 var infsrv = new InfoServ.TInfoServerClass();

But don't know how to do it on server..

3
  • What kind of server, where is the software running and do you have office installed on the server. Commented Mar 5, 2012 at 12:33
  • 1
    @CodingBarfield as far as I know, it's just another PC with win 2003 server. "Excel.Application" - it's a example, in reality it will be a custom COM object Commented Mar 5, 2012 at 12:38
  • I'm glad its just an example, it's probably a bad idea to have Excel on a server. Commented Mar 5, 2012 at 13:12

3 Answers 3

3

There's nothing wrong with adding a reference to Microsoft.VisualBasic so that you can simply use the same method. This is very much a nicety of .NET, it would be a waste not to use it. It is however simple to do, just two lines of code:

    public static object CreateObject(string progid, string server) {
        var t = Type.GetTypeFromProgID(progid, server, true);
        return Activator.CreateInstance(t);
    }

Well, one line if you push it. In this specific case you definitely should consider adding a reference to Microsoft.Office.Interop.Excel. You can then simply use the new operator, get speedier code because you are not late-binding and get IntelliSense. If you want to stick with late binding then be sure to use the C# version 4 support for the dynamic keyword. Writing late bound code in earlier C# versions is quite painful.

Sign up to request clarification or add additional context in comments.

4 Comments

+1. BTW the OP has now revealed they aren't really using Excel, that was just an example. In reality it will apparently be a custom COM object
Thank you. It kind of works, but return error: Unable to cast COM object of type 'System.__ComObject' to class type 'InfoServ.TInfoServerClass'. COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the __ComObject type. Instances of this type cannot be cast to any other class; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
my code: InfoServ.TInfoServerClass infsrv = (InfoServ.TInfoServerClass)Activator.CreateInstance(myType);
These are the hazards of late binding. Cast to an interface that's implemented by the COM server, might be named similar to "TInfoServer" but with a leading I or underscore. Use Object Browser to see it. The server also needs to support out-of-process activation, it is not automatic. Ask the vendor or author for help if you are not sure if it does.
2

You can add a reference to the Microsoft.VisualBasic.dll to your C# project and use CreateObject directly. This might be the easiest solution if you have an existing codebase that uses a lot of VB.NET specific features:

using Microsoft.VisualBasic;

...

dynamic xlApp = Interaction.CreateObject("Excel.Application", "MyServer");

Note: Since the backslash is a special character in C#, either remove the \\ from the server name (see above) or escape the string @"\\MyServer".

Comments

1

Use this overload of Type.GetTypeFromCLSID.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.