Software & Finance





VC++ ATL/COM - Step 5 COM Client Test Application in C#





 

6. COM Client Test Application in C#

There is couple of steps before you use the COM DLL into your C# application:

 

 

  1. Create a strong name file as shown below:

                   C:\kathir\SFTComServer>sn -k key.snk

 

  1. Create the DLL that is compatible with C# using tlbimp

                   C:\kathir\SFTComServer>tlbimp release\SFTComServer.tlb /keyf

                   ile:key.snk /out:SFTComServer_vsnet.dll /namespace:SFTCOMServer

 

                   Microsoft (R) .NET Framework Type Library to Assembly Converter                          2.0.50727.42

                   Copyright (C) Microsoft Corporation.  All rights reserved.

 

                   Type library imported to C:\kathir\SFTComServer\SFTComServer

                   _vsnet.dll

 

  1. Register the newly created .DLL

 

                   C:\kathir\SFTComServer>regasm SFTComServer_vsnet.dll

                   Microsoft (R) .NET Framework Assembly Registration Utility                                    2.0.50727.3053

                   Copyright (C) Microsoft Corporation 1998-2004.  All rights reserved.

 

                   Types registered successfully

 

  1. Register the COM DLL if you have not done so far.

         

 

Now is the time to write C# test application by adding a reference to the newly created SFTComServer_vsnet.dll.

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace SFTCSClientConsole

{

      static void Main(string[] args)

      {

          try

          {

            SFTComServer.MathControl ctrl = new SFTComServer.MathControl();

            uint result = 0;

            ctrl.AddTwoNumbers(10, 15, out result);

            Console.WriteLine(result.ToString());

            ctrl.AddTwoNumbers(10, 25, out result);

            Console.WriteLine(result.ToString());

            ctrl.AddTwoNumbers(10, 35, out result);

            Console.WriteLine(result.ToString());

          }

          catch (Exception err)

          {

            Console.WriteLine(err.Message);

          }

        }

}

 

Make sure you compile with X86 platform to avoid the Error 0x80040154 Retrieving COM class factory for component with CLSID failed

 

 

Click here to download the complete source code of COM DLL Server, C++ Client and CSharp Client