C# Class called from Delphi using COM

G

Guest

Hi!

I am trying to make use of the new Enterprise Library application blocks in
the re-write of a legacy system. The rewrite involves being able to access
new C# modules from Delphi and as a result, we have decided to use COM to
allow Delphi to access the C# objects, which are registered as COM objects.

Here is the code for a C# class that 'wraps' the Enterprise Library
Cryptography application block:

/// <summary>
/// COM wrapper for Enterprise Library Cryptography Application Block.
/// </summary>

[ClassInterface(ClassInterfaceType.AutoDual)]
public class CorpITCryption
{
public string Encrypt(string aPlainText)
{
return Cryptographer.EncryptSymmetric("symprovider", aPlainText);
//return "Hello!";
}

public string Decrypt(string aCipherText)
{
return Cryptographer.DecryptSymmetric("symprovider", aCipherText);
}
}

This library class references the
Microsoft.Practises.EnterpiseLibrary.Security.Cryptography .DLL and has
Register For COM Interop set to True.

Here is the Delphi code that I am using:

procedure TForm1.Button1Click(Sender: TObject);
var
V: Variant;
begin
V:= CreateOleObject('CorpITCOMCryption.CorpITCryption');
edCipher.Text := V.Encrypt(edPlain.Text);
end;

I find that if I try to use other ways of accessing the COM object (ie by
calling Co...Create() ) then I get a class not registered error in Delphi.
The above method seems to work most regularly.

My actual problem is that I get an error in Delphi with the above version of
the C# class library.

The error text is as follows:
There was an error reflecting type
'Microsoft.Practises.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings'.

If I swap the comments on the Encrypt method and return a basic string,
there is no error at all.

Has it got to do with strong names?

What am I doing wrong? What do I need to do to fix this?

Any help would be most appreciated.

Thanks

Andrew
 
M

Marc Scheuner [MVP ADSI]

I am trying to make use of the new Enterprise Library application blocks in
the re-write of a legacy system. The rewrite involves being able to access
new C# modules from Delphi and as a result, we have decided to use COM to
allow Delphi to access the C# objects, which are registered as COM objects.

What VERSION of Delphi ?? If you're using a Win32 version of Delphi
(e.g. D7, or the Win32 parts of D2005), the most likely suspect would
be the "string" type, which is very different in Delphi (Win32) than
in .NET (C#).

You might need to modify your Delphi calling code to deal with the
"special" type of string (probably something like a COM BSTR), rather
than a standard Delphi string.E.g. when you assign the return value
directly to edCipher.Text, you're assuming it's a Delphi string - it's
just not.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
T

Tim Jarvis

Marc said:
What VERSION of Delphi ?? If you're using a Win32 version of Delphi
(e.g. D7, or the Win32 parts of D2005), the most likely suspect would
be the "string" type, which is very different in Delphi (Win32) than
in .NET (C#).
Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch

Hi Andrew and Marc,

Marc is correct, you need to pass an OLE String (a widestring in delphi
is compatible with a BSTR) to the method, one of the problems with
using late binding like this is that you don't get an error at compile
time, it's deferred to run time.

It would seem though that you have more issues than just this string
passing one, as you should be able to early bind to the com class as
well, how has the c# class been registered with COM ? if you check the
registry is it all there present and correct ?

Cheers Tim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top