Brad Younie
January 6th, 2000, 12:25 PM
I have a C++ server that has an event interface. My vb app is supposed to implement this interface so it can handle the events.
I have not yet figured out how to make this work. Can anyone shed some light?
Chris Eastwood
January 6th, 2000, 05:39 PM
You implement an interface in VB by firstly setting a reference to your COM program that exposes that interface (Project->References menu).
How you use the interface is completely down to the design of the original COM program though. For instance, in Visual Source Safe (a COM server/client) you can write a VB 'add-in' that will need to implement a VSS exposed COM interface (can't remember the name of it off hand). This interface only has one function (INIT) that get's passed an Object reference to one of VSS's objects. You then declare in your program a variable of this type ('VSSwhatever') using the WithEvents keyword.
eg:
'
' Class1 Implementing a completely made up VSS Interface
' as I can't remember the name of it off the top of my head
'
option Explicit
'
Implements IVSSInterfaceThingie
'
' Reference to a made-up VSS Object
'
private withevents moVSSObject as VSS.VSSObject
'
private Sub IVSSInterfaceThingie_Init(byval oObject as VSS.VSSObject)
set moVSSObject = oObject
End Sub
'
private Function moVSSObject_BeforeCheckin(Something as Something) as Boolean
'
' Your Code Goes Here
'
End Function
When VSS starts up, it looks in an INI file for a project CLSID (eg. Project1.Class1 - where class1 implements the interface) and then creates an instance of that object internally. VSS will then call that interface's INIT method and pass in the object. As VSS is used, various events are raised that are then captured in your program.
Of course, this is just one way of implementing it - there are many other ways.
I'll dig out some VSS code if it helps you get a better understanding (although you'll need to understand exactly how your C++ COM program works first of all - how events are raised, how it uses external COM objects etc).
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
Brad Younie
January 10th, 2000, 08:15 AM
I had already tried that and I was unable to get it to work.
Here is the idl that I'm using:
import "oaidl.idl";
import "ocidl.idl";
import "definitions.idl";
[
object,
uuid(F3CD44AC-C20A-11D3-9DC9-00104B6FB11F),
helpstring("ICallNotify Interface"),
pointer_default(unique)
]
interface ICallNotify : IUnknown
{
[helpstring("method IncomingCall")] HRESULT IncomingCall([in] UCI callId, [in] CALL_CONTEXT_DATA ccd);
[helpstring("method CallCleared")] HRESULT CallCleared([in] UCI callId);
};
[
object,
uuid(F3CD44AE-C20A-11D3-9DC9-00104B6FB11F),
dual,
helpstring("ICallContext Interface"),
pointer_default(unique)
]
interface ICallContext : IDispatch
{
[id(1), helpstring("method GetValue")] HRESULT GetValue([in] long callId, [in] BSTR tag, [out, retval] BSTR *value);
[id(2), helpstring("method GetURL")] HRESULT GetURL([in] long callId, [out, retval] BSTR *value);
[id(3), helpstring("method GetLink")] HRESULT GetLink([in] long callId, [out, retval] BSTR *value);
[id(4), helpstring("method GetContext")] HRESULT GetContext([in] long callId, [out, retval] BSTR *value);
[id(5), helpstring("method GetMediaType")] HRESULT GetMediaType([in] long callId, [out, retval] BSTR *value);
[id(6), helpstring("method GetCallType")] HRESULT GetCallType([in] long callId, [out, retval] BSTR *value);
[id(7), helpstring("method GetOriginationType")] HRESULT GetOriginationType([in] long callId, [out, retval] BSTR *value);
[id(9), helpstring("method GetIPAddress")] HRESULT GetIPAddress([in] long callId, [out, retval] BSTR *value);
[id(10), helpstring("method GetCallPassword")] HRESULT GetCallPassword([in] long callId, [out, retval] BSTR *value);
[id(11), helpstring("method GetCCDId")] HRESULT GetCCDId([in] long callId, [out, retval] long *value);
[id(12), helpstring("method Register")] HRESULT Register([in] IDispatch *pEvents);
[id(13), helpstring("method Unregister")] HRESULT Unregister();
[id(14), helpstring("method GetIniString")] HRESULT GetIniString([in] BSTR app, [in] BSTR key, [in] BSTR strDefault, [in] BSTR path, [out, retval] BSTR *value);
};
[
uuid(F3CD44A0-C20A-11D3-9DC9-00104B6FB11F),
version(1.0),
helpstring("PNXLink 1.0 Type Library")
]
library PNXLINKLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(F3CD44AD-C20A-11D3-9DC9-00104B6FB11F),
helpstring("PNXLinkCallNotify Class")
]
coclass PNXLinkCallNotify
{
[default] interface ICallNotify;
};
[
uuid(F3CD44B0-C20A-11D3-9DC9-00104B6FB11F),
helpstring("_ICallContextEvents Interface")
]
dispinterface _ICallContextEvents
{
properties:
methods:
[id(1), helpstring("method IncomingCall")] HRESULT IncomingCall([in] long callId);
};
[
uuid(F3CD44AF-C20A-11D3-9DC9-00104B6FB11F),
helpstring("PNXLinkCallContext Class")
]
coclass PNXLinkCallContext
{
[default] interface ICallContext;
[default, source] dispinterface _ICallContextEvents;
};
};
In VB, I want to handle the _ICallContextEvents interface. Here's what I've done:
In a class module, I define the following, after adding the Reference to the COM server's type library.
Dim withevents oEvents as PNXLinkCallContext
private Sub oEvents_IncomingCall(byval CallID as Long)
MsgBox ("got it")
End Sub
What happens is that I get no errors, but the event does not fire off.
Any thoughts?