Delphi client / VS 2003 COM Server

G

glenn

I've gotten a COM server written in C# and I can call it from a VBScript
just fine and it works perfectly. However, I import it into Delphi 6 or
Delphi 2005 and I can not get access to any of the functions that are in the
server. Is there something specific that I need to do in order to get
Delphi to see the functions that are a part of the server?

Thanks,

glenn
 
W

Willy Denoyette [MVP]

glenn said:
I've gotten a COM server written in C# and I can call it from a VBScript
just fine and it works perfectly. However, I import it into Delphi 6 or
Delphi 2005 and I can not get access to any of the functions that are in
the
server. Is there something specific that I need to do in order to get
Delphi to see the functions that are a part of the server?

Thanks,

glenn

Don't know what the problem is or what should be done when using Delphi, but
I suggest you go through the tutorials in MSDN first,

- COM Interop Part 1: C# Client Tutorial
- COM Interop Part 2: C# Server Tutorial

and import the Server built in part 2 in Delphi.

Willy.
 
P

Philip Hristov

Hi,

How do you call the COM server from Delphi (I assume Delphi for Win32)?
In Delphi a COM server is called like this:

Uses ComObj;

Var
MyServer: Variant;
Begin
MyServer := CreateOleObject('ServerName');
MyServer.YourMethod(Param1, Param2);
End;

Call your server like this and tell us if this work. I am not so sure
(I stopped coding for Win32 before year and a half) but I think the
only way to call COM server (OLE Automation) is this way). I made one
OLE server and then I called its methods from other program, but that
was before years in Delphi 3...

Greetings,

Philip.
 
G

glenn

Actually that is correct, however, you can also just drop the component that
is created when you import the type library onto a form and it handles the
create for you. I do this all the time so I know this method works just as
well. The problem however, is that when I create the component and then
attempt to access its properties, I can not see the properties and get
errors when I compile. This is not normal for delphi and COM objects.

I am trying a little test now and will let you know how it goes shortly...
I am attempting to manually specify that the functions be visible to COm to
see if it fixes my problem...

Be back in a second... :)

glenn
 
G

glenn

nope, didn't change anything at all. From delphi's perspective it sees the
type library, it imports it just fine and adds a component to the pallet for
the item. I then choose the component and drop to the form just like with
any of my other COM objects. However, with other COM components I can view
the properties and methods of the COM object just as though it were a class
inside Delphi, but with the C# VS 2003 COM object, I can not see any methods
or properties listed.

its like I've created a COM object that has not methods exported and
therefore they can not be seen. In all other languages I've written COM
objects in, I've had to select specific functions that I wanted to export so
that other programs could use and see just those items I selected. The way
this looks, the entire program along with all its functions are suppose to
be visible, but in fact I can't use them from Delphi. I can from VBScript
but as that is the only NON VS program type language available on my
computer I really can't test with anything else.

Any ideas?

Thanks,

glenn
 
G

glenn

Willy,
- COM Interop Part 1: C# Client Tutorial
- COM Interop Part 2: C# Server Tutorial

I have already reviewed and tried the sample programs that you suggest here
and they don't work either. In fact they just add to the confusion for me
as they are completely different than what the book on COM gives as
tutorials for this subject. I am convienced that something has to be done
here to expose these functions to other program languages that run in
unmangaged code, however, I do not see how this is to be done yet...

Any help would be appreciated...

glenn
 
P

Philip Hristov

I saw you mentioned Delphi 2005? Why do not to try to use the COM
component in Delphi For .NET? Import it to the project like in VS, it
will create an interop library. At least you will know if the problem
is in Delphi or in the COM component.

And one more thing, as you say COM component component you mean .NET
Control which you registered with regasm.exe? Or you have created just
COM Interface (no component only class with methods)?

Philip.
 
G

glenn

No, I used regasm to register and create the type library and then I
imported into Delphi. Once Delphi imports the library, it creates the
component on the pallet for the object. From there everything is very
simple to use in Delphi, usually.

And I hate to say it, but I have not tried to do it with Delphi 2005 because
that won't solve my problem. I do not like Delphi 2005 and as a result I am
having to determine if I can write some of our solutions with Visual Studio.
However, this is a must to figure out in order to be able to do this. If I
can not get COM to work properly with .NET in VS then I will be forced to
continue to use Delphi 6 for all our solutions.

Thanks,

glenn
 
D

Doug Forster

Hi Glenn,

Well I have no experience with writing managed COM servers but I had no real
problem getting a variation of the sample to work via Delphi 6.
Firstly if you import the type library there is a dependency on mscoree that
does not wrap correctly due to Delphi being CI. You can manually modify the
wrapper to fix this. If you use the designer to create your client interface
object then you must explicitly cast it to your interface to get to the
correct interface or alternatively Philips suggestion using late binding
also works fine, but you might need to scout around the registry to find the
class name that VS generates.

These worked:


procedure TForm1.Button1Click(Sender: TObject);
Var
MyServer: Variant;
Begin
(InterfaceImplementation1.DefaultInterface as
IManagedInterface).PrintHi('Hello world');
MyServer := CreateOleObject('CSharpServer.InterfaceImplementation');
MyServer.PrintHi('Hello world');
End;

However, this is a must to figure out in order to be able to do this. If
I
can not get COM to work properly with .NET in VS then I will be forced to
continue to use Delphi 6 for all our solutions.

These problems are only minor as far as I can see.

Cheers
Doug Forster
 
W

Willy Denoyette [MVP]

glenn said:
Willy,

I have already reviewed and tried the sample programs that you suggest
here
and they don't work either. In fact they just add to the confusion for me
as they are completely different than what the book on COM gives as
tutorials for this subject. I am convienced that something has to be done
here to expose these functions to other program languages that run in
unmangaged code, however, I do not see how this is to be done yet...

Any help would be appreciated...

glenn

These samples work with VB, C++, VBScript native COM clients, seems like
Delphi has a problem importing the typelib created by regasm.

Willy.
 
G

glenn

Doug,

Thanks, I'll try that out and see if its my problem. I am also suspecting
Delphi here but just not sure what to do since it has never had a problem
like this before with any other COM server i've used.

Thanks,

glenn
 
G

glenn

Doug,

I must be missing something. I've copied your code exactly and also have
the same app setup exactly as the tutorial. However, when I run my delphi
exe, I get no errors, but I also get no results. It just passes over the
line for the COm server and moves on...
(InterfaceImplementation1.DefaultInterface as
IManagedInterface).PrintHi('Hello World');

Again, no compile errors and no errors at run time, but no results when
running the above line. I also tried the second method using the variant
but it results in same thing... Any ideas what i might be doing wrong here?

Thanks,

glenn
 
G

glenn

Actually the previous code was working. I just realized that the C# example
was writing to console and I changed it to a messagebox since I'm testing
with a Delphi Win form. its now popping up the messagebox perfectly. I am
very excited now and can start laying out this project. Thanks for all the
help guys...

glenn
 

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