msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

COM-Lowlevel / Event-"Latebinding"

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB WinAPI
Author Message
Alexander Mueller



Joined: 04 Oct 2007
Posts: 61

PostPosted: Thu Dec 13, 2007 4:00 pm    Post subject: COM-Lowlevel / Event-"Latebinding" Reply with quote

Hi

I need to find a solution to reliably catch the event of
a MS-Word doc being closed. The special part is that
(I think) I cannot bind to a TLB/OLB since customers may have
any version of MS-WORD from 97 to 2007 or no MS-WORD at all,
so I think I'll get into trouble with early binding and
referencing the OLB.

I've seen code in C++ that creates COM-instances the
'hard way' using CoCreateInstance and QueryInterface.
Can I do this also in VB and moreover can i get the
applications eventinterface this way?


If this is not such a good idea is there a way to
spy MS-WORD toplevel windows and find out if a
certain document (whose fully qualified path is known)
is opened a child window using Windows-API
(GetClassName, SendMessage, GetWindowText et al)


Thanks for all hints in advance

Alex

Archived from group: microsoft>public>vb>winapi
Back to top
View user's profile Send private message
Ralph



Joined: 04 Oct 2007
Posts: 4148

PostPosted: Thu Dec 13, 2007 3:49 pm    Post subject: Re: COM-Lowlevel / Event-"Latebinding" Reply with quote

"Alexander Mueller" wrote in message$0$13117$9b4e6d93@newsspool2.arcor-online.net...
> Hi
>
> I need to find a solution to reliably catch the event of
> a MS-Word doc being closed. The special part is that
> (I think) I cannot bind to a TLB/OLB since customers may have
> any version of MS-WORD from 97 to 2007 or no MS-WORD at all,
> so I think I'll get into trouble with early binding and
> referencing the OLB.
>
> I've seen code in C++ that creates COM-instances the
> 'hard way' using CoCreateInstance and QueryInterface.
> Can I do this also in VB and moreover can i get the
> applications eventinterface this way?
>
>
> If this is not such a good idea is there a way to
> spy MS-WORD toplevel windows and find out if a
> certain document (whose fully qualified path is known)
> is opened a child window using Windows-API
> (GetClassName, SendMessage, GetWindowText et al)
>

I can answer one question. No you can not capture Events in VB if you use
Late-Binding. Attempting to do it on your own is a bit dicey (unreliable). I
think Matt Curland talks about it in his book "Advanced VB Programming". The
problem is VB tends to swallow IConnectionPoints.

There may be some kind of 'spy' method. I don't know of one, but what in
programming is impossible.

If your needs are minimal you might compile with the lowest version of
Office you need to support. [Warning Air Code!]
Reference the "Microsoft Office 8.0 Object Library" (I think Off97 was '8'
???)
Private WithEvents oWord As Word.Application.8
' Fetch whatever is installed on the target box by using the
version-independent PID with CreateObject
Set oWord = CreateObject("Word.Application")
IIRC that works with Word 97 thru 2003. Not sure about anything newer.

As you noted, it isn't that hard to do with VC++:
http://support.microsoft.com/kb/q183599/
You can create an ATL component (fascade) for VB that connects to the
target's version of Word and publishes Events. The VB could early-bind to
this component.
I've done that on occasion (out of desperation ).

However, I suspect that you actually have another problem that you are
attempting to solve. Just catching a Close Event may not the solution you
really need. For example, if you just want to massage a document after Word
is done with it, perhaps a simple FindNextChange routine would work as well.


hth
-ralph
Back to top
View user's profile Send private message
Alexander Mueller



Joined: 04 Oct 2007
Posts: 61

PostPosted: Wed Dec 19, 2007 4:41 am    Post subject: Re: COM-Lowlevel / Event-"Latebinding" Reply with quote

Ralph schrieb:

Hi Ralph,

> "Alexander Mueller" wrote in message
> $0$13117$9b4e6d93@newsspool2.arcor-online.net...
>> Hi
>>
>> I need to find a solution to reliably catch the event of
>> a MS-Word doc being closed. The special part is that
>> (I think) I cannot bind to a TLB/OLB since customers may have
>> any version of MS-WORD from 97 to 2007 or no MS-WORD at all,
>> so I think I'll get into trouble with early binding and
>> referencing the OLB.
>>
>> I've seen code in C++ that creates COM-instances the
>> 'hard way' using CoCreateInstance and QueryInterface.
>> Can I do this also in VB and moreover can i get the
>> applications eventinterface this way?
>>
>>
>> If this is not such a good idea is there a way to
>> spy MS-WORD toplevel windows and find out if a
>> certain document (whose fully qualified path is known)
>> is opened a child window using Windows-API
>> (GetClassName, SendMessage, GetWindowText et al)
>>
>
> I can answer one question. No you can not capture Events in VB if you use
> Late-Binding. Attempting to do it on your own is a bit dicey (unreliable). I
> think Matt Curland talks about it in his book "Advanced VB Programming". The
> problem is VB tends to swallow IConnectionPoints.
>
> There may be some kind of 'spy' method. I don't know of one, but what in
> programming is impossible.
>
> If your needs are minimal you might compile with the lowest version of
> Office you need to support. [Warning Air Code!]
> Reference the "Microsoft Office 8.0 Object Library" (I think Off97 was '8'
> ???)
> Private WithEvents oWord As Word.Application.8
> ' Fetch whatever is installed on the target box by using the
> version-independent PID with CreateObject
> Set oWord = CreateObject("Word.Application")
> IIRC that works with Word 97 thru 2003. Not sure about anything newer.
>
> As you noted, it isn't that hard to do with VC++:
> http://support.microsoft.com/kb/q183599/
> You can create an ATL component (fascade) for VB that connects to the
> target's version of Word and publishes Events. The VB could early-bind to
> this component.
> I've done that on occasion (out of desperation ).
>
> However, I suspect that you actually have another problem that you are
> attempting to solve. Just catching a Close Event may not the solution you
> really need. For example, if you just want to massage a document after Word
> is done with it, perhaps a simple FindNextChange routine would work as well.
>

Thx for your reply!

I need to check 2-3 related things.
Situation:
I have a VB-Form for editing a document-record.
The record has some attributes and also the document itself,
which is stored as a blob in MS-SQL-SRV.
Users edit the document ( == any kind of a file) with the application
registered to edit or, if no edit is registered, to open it.

Although I think it is not actually possible,

- I would like to reliably catch the event when the document is closed
in the editing application. Since the app can be virtually any app this
is not possible.
It is possible with COM, if the app supports COM. Most documents are
actually word-docs edited by MS-word, so I think i should integrate a
word-special solution, which has to be latebinding, since customers do
not need word to be installed and lots don't have (some use Open Office,
Star Office, which we do appreciate).

- reliably test if the doc is still in use.
One could think of try and exclusively open the file for read.
Since some apps don't keep a file handle open while editing a file
this may be misleading. On the other hand: if the file can not be opened
this clearly indicated that the file is in use.

- reliable test if the editing app is still running when i close the
form, to warn users that changes made to doc will no longer be
rewritten to DB from now on.

Since I use ShellExecuteEx I do not always get the process-handle
If i don't get it, i can get the editing app from the registry and
enum processes to get the handle, which is also not 100% safe, because
there maybe multiple instances of the app loaded. Or the registry
entry maybe misleading (it may be the app that launches the 'real' app)

- reliably check if the document has been changed.
This is possible using FindNextChange or even more simple be simply
comparing the initial date-lastmodified with current.



So the idea of latebinding to events was for solving the doc-close issue
without needing a TLB.
In the meantime i noticed that i can use GetObject to check if the doc
is still opened, which is less code, again GetObject behaves kind of a
odd with Word, if Word was already opened, but that a different story.

MfG
Alex
Back to top
View user's profile Send private message
Alexander Mueller



Joined: 04 Oct 2007
Posts: 61

PostPosted: Wed Dec 19, 2007 4:50 am    Post subject: Re: COM-Lowlevel / Event-"Latebinding" Reply with quote

mr_unreliable schrieb:
> Alexander, if you don't mind using a 3rd-party control,
> you could use "John's Event Mapper" (EventMapper.dll),
> which was posted on a scripting newsgroup several years
> ago, but which still works -- at least for me.
>
> John's Event Mapper was superseded (for scripters) by
> the WScript.ConnectObject method, which allows for
> connecting a late-bound object's events to event sinks.
> But then, the wscript object and its methods and
> properties are not available to vb coders, at least
> I could never get it to work.
>
> Just in case you might be interested, I have included
> "John's Event Mapper" as an attachment, including the
> source code, and a little sample code to show how it
> works.


Hi mr_unreliable

Thanks you very much for the code.
I tried it with VC-6 but had some trouble that must have something
to do with Unicode vs ANSI settings of the project.
It's clearly an very interesting solution for event-latebinding.
I've been looking for such a DLL for quite some time so this is
really very useful code and binary.

"Wertarbeit" in some sense, as deutschmen say.


Thanks a bunch!



>
> mfg, jw

mfg, am

>
> p.s. "John" never provided his last (family) name, but
> from other postings signed by John, I have concluded
> that the provider of the code was John Roberts(?).
>
> Alexander Mueller wrote:
>> Hi
>>
>> I need to find a solution to reliably catch the event of
>> a MS-Word doc being closed. The special part is that
>> (I think) I cannot bind to a TLB/OLB since customers may have
>> any version of MS-WORD from 97 to 2007 or no MS-WORD at all,
>> so I think I'll get into trouble with early binding and
>> referencing the OLB.
>>
>> I've seen code in C++ that creates COM-instances the
>> 'hard way' using CoCreateInstance and QueryInterface.
>> Can I do this also in VB and moreover can i get the
>> applications eventinterface this way?
>>
>>
>> If this is not such a good idea is there a way to
>> spy MS-WORD toplevel windows and find out if a
>> certain document (whose fully qualified path is known)
>> is opened a child window using Windows-API
>> (GetClassName, SendMessage, GetWindowText et al)
>>
>>
>> Thanks for all hints in advance
>>
>> Alex
Back to top
View user's profile Send private message
mr_unreliable



Joined: 04 Oct 2007
Posts: 137

PostPosted: Tue Dec 18, 2007 11:38 pm    Post subject: Re: COM-Lowlevel / Event-"Latebinding" Reply with quote

Alexander Mueller wrote:
> Thanks you very much for the code.
> I tried it with VC-6 but had some trouble that must have something
> to do with Unicode vs ANSI settings of the project.
>
hi Herr Mueller,

Yes, John's event mapper is showing it's age.

If you understand c++, then maybe you can adapt it to a
unicode environment. (If you do work this out, I would
appreciate it if you would post your code here).

If not, then there is one other possibility. And that is
Meadroid's ScriptX control. This is also a 3rd-party
control, but it is not open source so you will not be
able to get any source code with it.

http://www.meadroid.com/scriptx/index.asp

While ScriptX is primarily oriented towards improving
the printing capabilities when printing from scripts
embedded in web pages, it does have one feature which
is applicable to your situation, called "Dynamic Event
Binding". You can read about that here:

http://www.meadroid.com/scriptx/docs/gendocs.htm

(To find the discussion click on the "Dynamic Event
Binding(DEB)" link at the top of the page).

ScriptX comes in two versions. A "Basic" (free) version,
and an "Advanced" (for sale) version. As best I can
recall, the "Dynamic Event Binding" feature is
included in the free version.

I can testify that "Dynamic Event Binding" also works to
late-bind events to event sinks, at least in a scripting
environment. I assume that it can also be made to work
in a (classic) vb environment as well.

mfg, jw
Back to top
View user's profile Send private message
mr_unreliable



Joined: 04 Oct 2007
Posts: 137

PostPosted: Wed Dec 19, 2007 12:24 am    Post subject: Late-Binding Events, yet another possibility Reply with quote

Servus Alex (we can drop the formal form, ja?),

There is yet another possibility that I stumbled across,
this one from MSDN Magazine, by Carlo Randone, entitled:
"COM: Handle Late-bound Events within Visual Basic Using
an ATL Bridge".

http://msdn.microsoft.com/msdnmag/issues/01/03/connpoints/

The article mentions a couple of possibilities, the "scripting
solution" the "vbExtenderObject solution", and finally gets
to the "guts" (American slang) of the article, "The Bridge
Solution".

Note: the code is included, although it is easy enough to
miss. Look for it at the top of the article, under the
hyperlink "Connpoints.exe".

I have not compiled or tested this, but it looks promising.

mfg, jw

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
LowLevel Mouse Hook Global Can anyone help me implement a Global lowlevel mouse hook in Visual Basic.net. I basically need to write a global applicaiton that launches an application anytime a user clicks the center mouse button... Thanks, Jonathan

Event Hello, I don't know it is the right newsgroup. Is it possibile from a dll written in c to raise an event at a vb application. The task is to control the by this dll. Or does anybody knows another (perhaps better) possibility? Thanks Albert

Event Log Hi, I am wondering how can I write something to event log in Win 2000 or XP? Which API should I use? -- Enggas "There are two types of people in the world. Those that know binary and those that don't"

Detecting an event I need to write a service (or a background app) to perform a certain set of action if no keybord input or mouse movements have been made in a given time. Now, I know that vb.net can detect the such events within the form, but I need this to be system wide

keyboard event hello everyone i have had to re-post this question due to a lack of any responce from another group. i really need some help with this. this is what i am currently using in my vb6 app. keybd_event VK_T, 0, 0, 0 keybd_event VK_T, 0, 0 the
Post new topic   Reply to topic    msvisual.com Forum Index -> VB WinAPI All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group