 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rich Noons Guest
|
Posted: Thu Jan 06, 2005 12:51 pm Post subject: Non default interfaces in Delphi |
|
|
Hi,
I'm a bit of a newbie to COM, so please excuse the terminology if it is not
correct.
I've imported a interface from .NET (sorted problems out with types etc...),
and
have tried using the interface in delphi & VB
A simple example is
namespace TestTLB1
{
[Guid("FB754D92-8814-470f-8DA5-78D12F2C0901")]
public interface ITestLib
{
string WSFunction(string AString);
}
public class Class1 : ITestLib
{
...
#region ITestLib Members
public string WSFunction(string AString)
{
return AString + " : String Updated";
}
#endregion
}
When I import this type library
It has an Interface ITestLib
a coClass Class1
and another interface _Class1
For some reason the TLB import sets the default interface of the CoClass to
be _Class1.
But the coClass still implements ITestLib (which is good...)
If I use VB I can simple do
Dim O As TestTLB1.Class1
Set O = New TestTLB1.Class1
MsgBox (O.WSFunction("Hello"))
Which works ok. In Delphi however,
I try and create the Class1,
var TestClass : TClass1;
begin
TestClass := TClass1.Create(Self);
TestClassIntf := TestClass (or TestClass as ITestLib)
end;
I'll get a compliation error or Interface not supported with the safecast.
Am I missing something obvious?
I'm not sure if this is the correct newsgroup to post, but hope someone can
help.
Many thanks
Rich
|
|
| Back to top |
|
 |
Rich Noons Guest
|
Posted: Thu Jan 06, 2005 4:35 pm Post subject: Re: Non default interfaces in Delphi |
|
|
I managed to get the interface working.
I needed the following line in the .NET code
[ClassInterface (ClassInterfaceType.None)]
We also had to use a Delphi 7 generated mscorlib_TLB.pas which we used in
Delphi 6.
(although we're now using the default interface, it now prevents the problem
we were having)
This works ok
var
TestClassIntf : ITestLib;
TestClassIntf:= CoClass1.Create;
ShowMessage(TestClassIntf.WSFunction('Hello'));
I was bit confused that I couldn't do :
var
TestClass : TTestClass;
TestClassIntf : ITestLib;
TestClassIntf := TestClass as ITestLib;
(interface not supported error)
and
Var
TestClass: TClass1;
//IDefault : _Class1;
//IOther : ITestLib;
//hr : HResult;
begin
TestClass:= TClass1.Create(Self);
TestClass.WSFunction('Hello');
Doesn't invoke the function WSFunction.
Does anyone have any ideas why?
Thanks
Rich
"Rich Noons" <djspira-nospam (AT) hotmail (DOT) com> wrote
| Quote: | Hi,
I'm a bit of a newbie to COM, so please excuse the terminology if it is
not
correct.
I've imported a interface from .NET (sorted problems out with types
etc...),
and
have tried using the interface in delphi & VB
A simple example is
namespace TestTLB1
{
[Guid("FB754D92-8814-470f-8DA5-78D12F2C0901")]
public interface ITestLib
{
string WSFunction(string AString);
}
public class Class1 : ITestLib
{
...
#region ITestLib Members
public string WSFunction(string AString)
{
return AString + " : String Updated";
}
#endregion
}
When I import this type library
It has an Interface ITestLib
a coClass Class1
and another interface _Class1
For some reason the TLB import sets the default interface of the CoClass
to
be _Class1.
But the coClass still implements ITestLib (which is good...)
If I use VB I can simple do
Dim O As TestTLB1.Class1
Set O = New TestTLB1.Class1
MsgBox (O.WSFunction("Hello"))
Which works ok. In Delphi however,
I try and create the Class1,
var TestClass : TClass1;
begin
TestClass := TClass1.Create(Self);
TestClassIntf := TestClass (or TestClass as ITestLib)
end;
I'll get a compliation error or Interface not supported with the safecast.
Am I missing something obvious?
I'm not sure if this is the correct newsgroup to post, but hope someone
can
help.
Many thanks
Rich
|
|
|
| Back to top |
|
 |
Rich Noons Guest
|
Posted: Thu Jan 06, 2005 4:44 pm Post subject: Re: Non default interfaces in Delphi |
|
|
I managed to get the interface working.
I needed the following line in the .NET code
[ClassInterface (ClassInterfaceType.None)]
We also had to use a Delphi 7 generated mscorlib_TLB.pas which we used in
Delphi 6.
(although we're now using the default interface, it now prevents the problem
we were having)
"Rich Noons" <djspira-nospam (AT) hotmail (DOT) com> wrote
| Quote: | Hi,
I'm a bit of a newbie to COM, so please excuse the terminology if it is
not
correct.
I've imported a interface from .NET (sorted problems out with types
etc...),
and
have tried using the interface in delphi & VB
A simple example is
namespace TestTLB1
{
[Guid("FB754D92-8814-470f-8DA5-78D12F2C0901")]
public interface ITestLib
{
string WSFunction(string AString);
}
public class Class1 : ITestLib
{
...
#region ITestLib Members
public string WSFunction(string AString)
{
return AString + " : String Updated";
}
#endregion
}
When I import this type library
It has an Interface ITestLib
a coClass Class1
and another interface _Class1
For some reason the TLB import sets the default interface of the CoClass
to
be _Class1.
But the coClass still implements ITestLib (which is good...)
If I use VB I can simple do
Dim O As TestTLB1.Class1
Set O = New TestTLB1.Class1
MsgBox (O.WSFunction("Hello"))
Which works ok. In Delphi however,
I try and create the Class1,
var TestClass : TClass1;
begin
TestClass := TClass1.Create(Self);
TestClassIntf := TestClass (or TestClass as ITestLib)
end;
I'll get a compliation error or Interface not supported with the safecast.
Am I missing something obvious?
I'm not sure if this is the correct newsgroup to post, but hope someone
can
help.
Many thanks
Rich
|
|
|
| Back to top |
|
 |
John Carlyle-Clarke Guest
|
Posted: Tue Jan 11, 2005 8:44 am Post subject: Re: Non default interfaces in Delphi |
|
|
"Rich Noons" <djspira-nospam (AT) hotmail (DOT) com> wrote in
news:41dd68d7$1 (AT) newsgroups (DOT) borland.com:
| Quote: | This works ok
var
TestClassIntf : ITestLib;
TestClassIntf:= CoClass1.Create;
ShowMessage(TestClassIntf.WSFunction('Hello'));
I was bit confused that I couldn't do :
var
TestClass : TTestClass;
TestClassIntf : ITestLib;
TestClassIntf := TestClass as ITestLib;
(interface not supported error)
|
My guess is that this is because TClass1 is not Class1. TClass1 is
probably a Delphi generated wrapper class. When you imported the
type lib, did you select the option to create a wrapper class
component?
If you look at the source code for that component, you'll see that
it contains the coClass but is not it.
If I'm right, to get your second example working, you'd have to do
this:
TestClassIntf := TestClass.DefaultInterface as ITestLib;
Although the wrapper classes have their place, I've come round to
avoiding their use for non-visual classes.
| Quote: | Var
TestClass: TClass1;
//IDefault : _Class1;
//IOther : ITestLib;
//hr : HResult;
begin
TestClass:= TClass1.Create(Self);
TestClass.WSFunction('Hello');
Doesn't invoke the function WSFunction.
If I use VB I can simple do
Dim O As TestTLB1.Class1
Set O = New TestTLB1.Class1
MsgBox (O.WSFunction("Hello"))
|
My *guess* here is that this is because VB may be falling back on a
call by name. If a dual interface is not marked with the flag
"nonextensible", then VB assumes that it may be extended by
supporting other calls via the dispatch interface. If the method
called is not in the interface definition, VB will still compile the
code, and at runtime will try a call by name.
Perhaps your coClass is set up so that this method can be called by
name?
I learned empirically always to mark interfaces as nonextensible, so
that if I mistype a method or property name in VB it will throw a
compile error, rather than silently compiling and then failing at
some later run-time moment.
Hope this helps a bit.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|