C# Dll Question

  • Thread starter Thread starter glenn
  • Start date Start date
It depends on whether the dll that you wish to use with C# is a COM dll
or win32 dll.

It its a COM dll, then you just add a reference to the dll from Visual
Studio.NET project. It will create Runtime Callable Wrappers and
Unwrappers (COM InterOp) for you. Tou do not need to do any extra
coding.

If its a Win32 dll, then you need to use DllImport attribute in C# for
each function in dll that you wish to expose to C# assembly.

e.g.
Say you have a dll named myLib.dll and you have a function called
Print inside it that you want to expose it to C#, then the code for it
is as follows.

using System;
using System.Runtime.InteropServices;

namespace HelloUtil
{
public class Echo
{
//This is how you register dll function with
//DllImport
[DllImport("myLib.dll")]
static extern void Print(string s);

string myString;

public Echo(string aString)
{
myString = aString;
}

public void Tell()
{
//This is actuall call to dll function
Print(myString);
}
}
}
 
There is absolutely no need to use D2005 as D6 is well able to cope
with
using COM objects.

I think that was well understood based on OP's comments.
As the OP has discovered, all that is required is to create a COM interop
assembly which can then be used to create a type library using Delphi's COM
wizard.

I know almost nothing about Delphi than what OP mentioned. *If* Delphi
2005 supports .net and unmanaged code, I see no reason to go the COM
way. Why even bother? There is a *if* involved here and only someone
familiar with Delphi 2005 can answer that. What I am saying is not
different than MC++, where you can have managed wrapper over an
unmanaged object and use it in another managed assembly without using
COM.
 
Ok, I've created a dll in C# and set it up as a COM server. I then go to
delphi and try to import the type library which is generated and get the
following error in the mscorlib_TLB.pas unit...

Type Byte is not yet completely defined.

Any ideas on this?

Thanks,

glenn
 
It does support it, there is no if there, but the problem is I'm not in
Delphi 2005. I'm in Delphi 6 and not until our project is rewritten will it
be in either C# or Delphi 2005 as I have not decided yet which one we will
be using.

I would like to use C#, but the database routines provided in Visual Studio
are weak at best compared to Delphi and I have not determined if we want to
manually code all our database stuff yet. Other than that I love C#, the
Visual Studio IDE though is so weak that it frustrates me. We're using
Delphi 6 and it is so much better than the Visual Studio stuff its
unbelievable. I know VS is much better than anything else Microsoft has
ever produced, but if any Microsoft developer were to ever compare it to
something like Delphi, I don't think they'd be praising it quite so much...

glenn
 
Thank you but as I stated, I'm wanting to call the dll from another
development platform and they work differently. The problem is the dll I
wrote in C# does not act like a normal Win32 dll and I don't know how to get
it to do so. So I am having to write it as a COM object so that I can
import it into Delphi and use it that way. Which is not a problem, just
trying to figure it all out...

glenn
 
Ok, I got all this to work, but my next question is, how do I select a
class/function to export so that the delphi application can see it?

Thanks,

glenn

Sean Hederman said:
In your C# DLL do the following:

AssemblyInfo.cs
----------------
[assembly: ComVisible(true)]

Then, right-click on the project in the Solution Explorer, select
Properties, choose Configuration Properties\Build, and set "Register for COM
Interop" to "True".

Now when you build your project it will make it COM-compatible and register
it on your machine. You should now be able to reference the C# library in
your Delphi code.
 
I see. If Deplhi understands COM, then what you need to do in this case

1. Attach a strong name to C# dll and put it in GAC.
2. Use RegAsm tool to register the C# dll and create Type Library.
3. Reference this Type Library in ur Delphi code.

Cheers
Ravindra Sadaphule
MCSD.NET
 
Back
Top