Using AddAdvise with Visio 2003

S

Scott Metzger

Hi,

Ok, I know how to use AddAdvise with the Visio 2002 SDK...
IUnknown FAR* pSink = NULL;
CVisioEventList vsoEList;
CVisioEvent vsoEvent;

if ( SUCCEEDED(vsoApp.EventList(vsoEList)) )
{
if (SUCCEEDED(CoCreateAddonSink(ShapeAdded, &pSink)) )
{
if ( SUCCEEDED(vsoEList.AddAdvise( (short)(visEvtShape |
visEvtAdd), VVariant(pSink),
VBstr(_T("")), VBstr(_T("")),
vsoEvent)) )
{
vsoEvent.ID(&stclEventID);
}
} // end if created sink event
} // end if got list of events

But how do I use VisEventProc and AddAdvise in a Visio 2003 C++ program?
The documentation shows a VB example but I am unsure how to translate
that to C++.

Thanks,
Scott Metzger
 
G

Graham Wideman \(Visio MVP\)

Scott:

To my knowledge, AddAdvise hasn't changed between V2002 and V2003 -- do you
think that it has,, and if so why?

Graham
 
S

Scott Metzger

Graham said:
Scott:

To my knowledge, AddAdvise hasn't changed between V2002 and V2003 -- do you
think that it has,, and if so why?

No, AddAdvise has not changed.

However, I would like to use AddAdvise straight from the typelibe for 3
reasons:
1) There is no Visio 2003 SDK and I don't want to use the 2002 SDK.
Perhaps I am wrong, but I believe the way that the Visio 2002 SDK
interacts with Visio is slower. I believe the Visio 2002 SDK uses late
binding while importing the typelib would use early binding. I may be
talking out of my !@#$% here, but this is my impression.

2) My code will be alot cleaner if I don't mix the 2002 SDK with the
typelib classes. For example, I don't see a straightforward way to
convert between the SDKs CVisioShape and the imported class CVShape.

3) Using the Visio 2002 SDK I can 'map' a function with the following
declaration:
HRESULT __stdcall OnConnection( IN IUnknown* ipSink,
IN short nEventCode,
IN IDispatch* pSourceObj,
IN long lEventID,
IN long lEventSeqNum,
IN IDispatch* pSubjectObj,
IN VARIANT vMoreInfo,
OUT VARIANT* pvResult)
A 'C' function throws object oriented programming to the wind.

I am hoping that if I use the Typelib files directly I can use something
like...
HRESULT CMyVisioInstanceClass::OnConnection( IN IUnknown* ipSink,
IN short nEventCode,
IN IDispatch* pSourceObj,
IN long lEventID,
IN long lEventSeqNum,
IN IDispatch* pSubjectObj,
IN VARIANT vMoreInfo,
OUT VARIANT* pvResult)

So my question really is...
How do I use the imported CVEventList::AddAdvise to map an event. What
I have so far is the following...

--------- Here is my C++ code -----------

void CVisio2003ReadDlg::OnBnClickedMapshapeadded()
{
CVDocument myDocument(MyVisio.get_Document());
CVApplication myApplication(myDocument.get_Application());
IUnknown FAR* pSink = NULL;

CVEventList myEventList(myApplication.get_EventList());
MyShapeAddedEvent = new CVisEventProc;
// declared in .h file for this class

VARIANT m_v;
VariantInit(&m_v);
V_VT(&m_v) = VT_DISPATCH;
LPDISPATCH temp = LPDISPATCH(MyShapeAddedEvent);
V_DISPATCH(&m_v) = temp;
if (temp)
temp->AddRef(); // Problem #1 here

MyEvent= myEventList.AddAdvise( (visEvtShape | visEvtAdd), m_v, "",
""); // Problem #2 here
}

Ok, so when I call the AddRef my CPU goes up to 100% used and the
program never returns from the call to AddRef.
When I take out the call to AddRef I get an unhandled exception error
when AddAdvise is called.

Thanks,
Scott Metzger
 
M

Mai-lan [MS]

Hi, Scott:
Some additional notes on this.
We will ship a Visiwrap version of Visio 2003 (it will be in the SDK in Q1
2004). There is an AddAdvise sample in the Visio 2002 SDK on MSDN if you
want to take a look -- it's VB. In the Visio 2002 SDK, we prefer to use
#import and now tend to view shipping visiwrap as a backward compatibility
thing only. The sink class that we provided in the Visio 2002 SDK can be
used with Visio 2003. In the Visio 2002 SDK, it's in
SdK\libraries\cpp\include\AddSink.h and SdK\libraries\cpp\source\AddSink.cpp

The sample that show how to use this in an object oriented way is called
GenericMFC (in the Visio 2002 SDK). The sample does use Visiwrap but as the
differences between Visiwrap and other wrapper classes is mostly syntactic
it still a good example of how to use the code in AddSink.h/cpp. The sample
is an add-on BUT that's irrelevant from the event handling part of this.
Add-on are just easy ways to run code in Visio in C++. The relevant code
for event handling purposes are the the creation of the sink class and calls
to AddAdvise in the Run method. If you're not creating an add-on, it needs
to go in some other method called, say InitializeEvents. The other
important code to look at is its HandleVisioEvent method. This is a virtual
method inherited from the VEventHandler class defined in AddSink.h. For
non-addons that use this, think about something like:

Class MyEventHandlerClass : public VEventHandler
{
/* Class must implement HandlevisioEvent method
Class must be passed as the event handler in the call to
CoCreateAddonSinkForHandler (defined in Addsink.h)*/
};


Thanks,
Mai-lan
 
T

tvntuong

Hi,
I am writing an EXE Visio addon that needs to control the Mouse Event. For
exmaple, when mouse clicked, I want to get the shape that was clicked.

I have read the Visio SDK document but I doesn't have a C++ example.
So does any have code of this problem?
If yes, please send me at: (e-mail address removed)
Thank you,
NTuong
 
M

Mark Wheeler

If you look in the Visio Developer Help under the Event AddAdvise there is
lots of info. Also look at VisEventProc.

Also the Office Developer Centre on the Visio page has got a number of
useful articles.
 

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