Google
 
 
Home arrow Other Articles arrow Creating a C# dll to use with COM

Main Menu
 Home
 Linux Articles
 FreeBSD Articles
 Apache Articles
 Perl Articles
 Other Articles
 Program Downloads
 Free Books
 News
 The Web Links
 Contact Us

Most Read
Automating SFTP using expect
FreeBSD PPTP VPN
Shorewall Router on Linux
SnortShorwall - Using Snort And Shorewall Together
Shorewall Stand Alone Firewall

Polls
Favorite Linux/BSD
Fedora
Mandrake
Debian
Slackware
Gentoo
Suse
FreeBSD
Other
  

Syndicate
Latest news direct to your desktop
RSS

Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one

Members Online
 Linux-BSD-Central Has a Total of 701 Members   Members (701) # Online
 We have 5 Guests Online. Guests 5
 We have 0 Users Online. Users 0

Online Users
No Users Online

Statistics
OS: Linux w
PHP: 5.2.9
MySQL: 5.0.91-community
Time: 14:53
Members: 701
Hits: 1324417
News: 277
WebLinks: 15



-->

Creating a C# dll to use with COM   PDF  Print  E-mail 
Contributed by Chad Brandt  
Friday, 27 February 2009
If you are developing with .NET and C# you may come across a time when you want to use your C# dll in unmanaged (native C++ code). Unfortunately it is not a straight forward problem and the best way to accomplish it is exposing your C# dll as a COM object. In a recent project I ran across this problem and was able to successfully use the C# dll in c++ code in Visual Studio and C++ code in Borland Builder 2007

I usually do java code but came across a project where they needed a windows dll on the client to interface with our java server code. I created a C# dll that talked to our java server over https and xml. The C# code was easy, the difficult part was getting their client app to use it because they were using Borland C++ builder. I got it to work by exposing the C# dll to COM.

Lets say we have a C# interface and implementation like this

namespace Test
{

    public interface IClient
    {
         int SendMessage(string messageToSend);
         string GetMessage(int id);

    }

    public class Client: IClient
    {
         public int SendMessage(string messageToSend){

             // Do some stuff here to send the message
         }

         public string GetMessage(int id){
            // Do some stuff to get a message by id
         }
        
    }
}

To Make this COM visible you need to set these options in Visual Studio
Go to Project Properties
Under build check Register for COM interop and generate serialization assembly On (this creates the .tbl file you will need to register the dll with COM on the target machine)
Under signing you will need to check sign the assembly and create a strong name key
Under Application click Assembly Information and check 'make assembly COM visible'

Update the code to add the COM attributes

namespace Test
{
    [Guid("A66356CF-7408-4bf5-B02E-17158FE30DA3")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IClient
    {
         [DispId(1)]
         int SendMessage(string messageToSend);
         [DispId(2)]
         string GetMessage(int id);

    }

    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Test.Client")]
    [ComSourceInterfaces(typeof(IClient))]
    public class Client: IClient
    {
         public int SendMessage(string messageToSend){

             // Do some stuff here to send the message
         }

         public string GetMessage(int id){
            // Do some stuff to get a message by id
         }
        
    }
}

Now to use this dll on a client you will need to supply the .dll and use regasm.exe to register the dll for COM. regasm.exe /codebase filename.dll

For visual studio native c++ use this code to call the dll

#include "stdafx.h"
#import "C:filename.tlb" raw_interfaces_only

using namespace Test;
using namespace std;
CWinApp theApp;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

	 // Initialize COM.
    HRESULT hr = CoInitialize(NULL);

    // Create the interface pointer.
    IClientPtr pIClient(__uuidof(Client));
    int sendResult;
    BSTR getResult;

    // Call the DLL
    pIClient->SendMessage( CComBSTR(L"Some message to send"), &sendResult);


    pIClient->GetMessage( 23, &getResult);    

    return 0;
}

In Borland you will need to go to component -> import type library and import the .tlb file. The _TLB.h and _OCX.h file will be generated when you import the .tlb file. Add the .dll to the project. Now you can use this code to call the dll

#include <vcl.h>
#include <stdio.h>
#include <string.h>

#pragma hdrstop

#include "Filename_TLB.h"
#include "Filename_OCX.h"
#pragma argsused

int main(int argc, char* argv[])
{
	// Initialize COM.
   HRESULT hr = CoInitialize(NULL);

   // Create the interface pointer.
   IClientPtr pIClient =  pIClient.CreateInstance(CLSID_Client);
   
   BSTR result;
   pIClient->SendMessage(WideString(L"this is a message to send"),   &result);
   return 0;
}

I hope this helps somebody, it is not the easiest information to find for Borland...

Comments
Is possible to use it in C ?
Written by Guest on 2009-09-11 11:49:12
Is possible to use it in C ?

Write Comment
Name:Guest
Title:
BBCode:Web AddressEmail AddressBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:



Powered by AkoComment 1.0 beta 2!




 
Google Ads



 

Check out TwistByte - The best mobile apps available For awesome Android and IPhone applications!!