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 2007I 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...
|
Is possible to use it in C ? Written by Guest on 2009-09-11 11:49:12 Is possible to use it in C ? | will this work on linux Written by Guest on 2009-10-28 23:29:30 will this work on linux | Great post Written by Guest on 2009-12-01 05:52:57 I will try!!!! | Entrümpelungen, Wohnungsauflösungen Written by Guest on 2010-07-05 20:36:54 Da haste dir aber viel mühe gegeben Den Eiger in 2,47 Stunden zu besteigen währe wohl vor 70Jahren nicht moeglich gewesen Respekt Ueli Steck!
|
Only registered users can write comments. Please login or register. Powered by AkoComment 1.0 beta 2! |