none
Writing a COM Server callable from VBScript in C# ???

Answers

  • Hi Peter,

    I am not 100% sure because at the moment I do not have any system with Visual C# Express. But from my experience when I played around with the Express Edition I would expect that it is possible.

    Just check if you can create a Class Library Project (a Windows DLL will be the target then).

    Then you get a class1.cs inside. Rename it to TestObject.cs (optional step) and replace the content with:

    using System.Runtime.InteropServices;
    
    namespace MsdnForum
    {
      [ComVisible(true)]
      [Guid("660749aa-febb-4714-ac0f-7316e3beb398")]
      public interface ITestObject
      {
        [ComVisible(true)]
        string GetHelloWorld();
      }
    
      [ComVisible(true)]
      [ProgId("MsdnForum.TestObject")]
      [Guid("660749aa-febb-4714-ac0f-7316e3beb399")]
      public class TestObject : ITestObject 
      {
        [ComVisible(true)]
        public string GetHelloWorld()
        {
          return "Hello World!";
        }
      }
    }
    
    

    When you compile it: Make sure, that you sign it. (Project options, sign tab, create a snk so it is signed with it!)

    Next you have to install it. Just use this for it:

    regasm TestLibrary.dll /codebase
    (TestLibrary.dll is the library I created. It depends on how you created the project!)

    A vbscript to test it can be:

    Dim testObject : set testObject = CreateObject("MsdnForum.TestObject")
    WScript.StdOut.WriteLine(testObject.GetHelloWorld())

    And if you call it you get as result:

    C:\neitzel\Projects\TestLibrary\TestLibrary\bin\Debug>cscript test.vbs
    Microsoft (R) Windows Script Host, Version 5.8
    Copyright (C) Microsoft Corporation 1996-2001. Alle Rechte vorbehalten.
    
    Hello World!
    
    C:\neitzel\Projects\TestLibrary\TestLibrary\bin\Debug>
    I hope that helped a little. One important remark: All the [ComVisible(True)] is not required if you simply set

    [

     

    assembly: ComVisible(true)]

    in AssemblyInfo.cs (can be found inside Properties)

    With kind regards,

    Konrad

    • Marked as answer by Peter Olcott Friday, August 13, 2010 2:34 PM
    Wednesday, August 11, 2010 8:03 PM

All replies

  • Yes, you can create a COM Component, that can be called from VBScript.

    Just check out the COMVisible Flag. Important is, that you set the GUID and ProgId so you can call it.

    And when you register the DLL, you should use the /codebase switch of regasm.exe (Or I think, oyu could put it in the GAC and leave that flag.)

    We did that multiple times already and it worked quite nice. And in VBS you just do the CreateObject("ProgId.You.Provided")

    A good point to start reading could be http://msdn.microsoft.com/en-us/library/aa720072.aspx

    With kind regards,

    Konrad

    Wednesday, August 11, 2010 10:20 AM
  • Will this work using Visual C# 2010 Express ?
    100% Accurate Display Screen OCR http://www.OCR4Screen.com
    Wednesday, August 11, 2010 1:57 PM
  • Hi Peter,

    I am not 100% sure because at the moment I do not have any system with Visual C# Express. But from my experience when I played around with the Express Edition I would expect that it is possible.

    Just check if you can create a Class Library Project (a Windows DLL will be the target then).

    Then you get a class1.cs inside. Rename it to TestObject.cs (optional step) and replace the content with:

    using System.Runtime.InteropServices;
    
    namespace MsdnForum
    {
      [ComVisible(true)]
      [Guid("660749aa-febb-4714-ac0f-7316e3beb398")]
      public interface ITestObject
      {
        [ComVisible(true)]
        string GetHelloWorld();
      }
    
      [ComVisible(true)]
      [ProgId("MsdnForum.TestObject")]
      [Guid("660749aa-febb-4714-ac0f-7316e3beb399")]
      public class TestObject : ITestObject 
      {
        [ComVisible(true)]
        public string GetHelloWorld()
        {
          return "Hello World!";
        }
      }
    }
    
    

    When you compile it: Make sure, that you sign it. (Project options, sign tab, create a snk so it is signed with it!)

    Next you have to install it. Just use this for it:

    regasm TestLibrary.dll /codebase
    (TestLibrary.dll is the library I created. It depends on how you created the project!)

    A vbscript to test it can be:

    Dim testObject : set testObject = CreateObject("MsdnForum.TestObject")
    WScript.StdOut.WriteLine(testObject.GetHelloWorld())

    And if you call it you get as result:

    C:\neitzel\Projects\TestLibrary\TestLibrary\bin\Debug>cscript test.vbs
    Microsoft (R) Windows Script Host, Version 5.8
    Copyright (C) Microsoft Corporation 1996-2001. Alle Rechte vorbehalten.
    
    Hello World!
    
    C:\neitzel\Projects\TestLibrary\TestLibrary\bin\Debug>
    I hope that helped a little. One important remark: All the [ComVisible(True)] is not required if you simply set

    [

     

    assembly: ComVisible(true)]

    in AssemblyInfo.cs (can be found inside Properties)

    With kind regards,

    Konrad

    • Marked as answer by Peter Olcott Friday, August 13, 2010 2:34 PM
    Wednesday, August 11, 2010 8:03 PM
  • Although because of my lack of knowledge implementing the above steps was a little tricky, I did get it all to work.

    Signing the application means to give it a strong name so that it is placed in the Global Assembly Cache (GAC). This can be done through the graphical user interface of Visual C# Express 2008.

    Project(Menu)-->"ProjectName" Properties(MenuItem)-->Signing(Tab)-->Sign the assembly(CheckBox)

    Then you have to select <New>... from the DropDownList a DialogBox pops up, you then give it a "Key file name" such as: "GACKey.snk"   and then the project must be re-built.

    All of the rest works just as stated above. I changed the second line of the VBScript file to this:

    MSGBOX testObject.GetHelloWorld()

    That way you only have to click on the VBScript filename to run it, and it creates a MessageBox instead of outputting to the command line.

    To get this to work under 64-bit Windows you must explicitly run the 32-bit version of cscript.exe:

    C:\Windows\SysWOW64\cscript.exe


    100% Accurate Display Screen OCR http://www.OCR4Screen.com

    Friday, August 13, 2010 2:30 PM