Catch Windows Messages in VSTO Plug-In - c#

Is there a way to catch windows messages within word (I tried overriding WinProc… no go)? I have a word VSTO plug-in that I’m trying to communicate with externally… ideally I would like to post a message from one application, catch that message in word, and then perform the appropriate response…

Since this functionality isn't exposed by the interop assemblies I would not suggest this method. You should only try and interact with office applications through these APIs.
If you just need interprocess communication and you have control over the sender and receiver I would suggest using a socket connection or a pipe. Word is going to to be handling it's own windows messages, and it would not be a good idea to interfere with that process.

Avoiding interprocess communication in Word (which is a mess when it comes to privileges with UAC) I came up with my own solution:
I set up a hook within my instance of my word VSTO plugin with SetWinEventHook() (hooking SYS_ALERT) … I simply monitor new windows created (OB_CREATE), if it’s an application that uses the explained camera then I close the currently running graph in DS, freeing the camera and allowing it to be used in the application that has focus. Each of my programs that use the camera will implement this class hook.
Better ideas welcome…

Related

How to capture WM_MINIMIZE for another application?

I am currently looking for a solution in C# how to capture window-messages like WM_MINIMIZE for another application for which I do not have any source code, for example Notepad. My goal is to prevent a user or the system from minimizing a certain application. So my basic idea is to have a process running that filters all Window-messages, and just does nothing, unless a message is posted to the application I define (for example by knowing the window-handle of the applications mainwindow), and then look if this message is a certain size-operation, like WM_MINIMIZE. Only in this case, the message should be removed from the Windows message queue.
What would be a first approach to achieve this? At the moment I am stuck a little bit at finding online resources because most topics concerning resizing deal about messages of own applications, but not to control messages posted to other, external applications running on the same computer.
Either you trap all messages of windows by creating a dll which does a global hook (in c++) (risk of slowdown the OS)
Either You have something to hook the program and in this case:
EasyHook is your friend

Windows 7 - Disable Close Program/Debug Program Dialog, yet crash dump and notify the user

I'm developing a commercial Windows 7 desktop application (somewhat shrink wrap, but not quite). The application has both Windows Service and a WPF User Interface components. In the event that our application fails at a customer site we want to be able to capture information about the runtime environmnent, including a crash dump of our processes to a dump location. That information could then be forwared to our support personnel upon request for further investigation. We also want to turn off any Windows Error Reporting and Send To Microsoft dialogs that might popup as a result of the failure.
The important thing is that we only want this to affect our application processes. We don't want to affect some global setting that will change the way all other applications on the customers desktop report fatal errors.
I'm looking for some suggestions and best practices for doing this sort of thing in a shrink wrap application.
The best way I know how to do this is by subscribing to the AppDomain.CurrentDomain.UnhandledException event. There you will be able to use the Win32 API function MiniDumpWriteDump to create your own minidump file. Check out this blog post to see a good example. Also, there is the ClrDump library.
Before you exit from your UnhandledException handler, call Environment.Exit() and you shouldn't see any more Windows error dialogs.
I have not actually used these minidump libraries myself yet, but I will soon. Hopefully this answer will at least give you a few keywords that you can plug into Google.

How to know when a user console is locked or has logged "back into" windows

This is in regards to applications that run in the taskbar but should be applicable to standard apps, Winforms, WPF, etc.
Question: I am after some method (preferably via managed code) to be notified when a user either has their screen "locked" while my app is running and/or know when they log back in.
GMail Notifier does this sort of thing for example, if my PC is locked for a while when I log in again it shows a list of emails that arrived since locking the PC. I'm looking to replicate that kind of functionality.
Does anyone have any ideas on how to accomplish this?
You want to hook into the System Event Notification Service and subscribe to the ISensLogon DisplayLock/DisplayUnlock events.
The question mentioned by Mike Daniels has answers:
Checking for workstation lock/unlock change with c#
The one I prefer is using is SessionSwitchReason:
http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionswitchreason.aspx

Sharing Add-in between Office Apps

I have written an Office shared Add-in with C# and .NET 2.0. It uses the same COM Shim for all office apps. Currently, every instance creates its own instance of my class -- they have no knowledge of the fact that the add-in is running on another application.
Is it possible to make it so that, say, when the Word add-in launches it can detect that the Excel add-in is already running? Can they communicate between each other?
Let's say my dll is called Addin.dll. When, for example, Word opens, it runs the code in Addin.dll that implements the IExtensibility interface, and creates a class, let's call it WordAddin. When Excel opens, it also runs the code in Addin.dll, and it creates an instance of ExcelAddin. Now, suppose that Word is running and WordAddin exists. When Excel is opened, Addin.dll is loaded into a different AppDomain. It has no knowledge that WordAddin exists. I want ExcelAddin to have know WordAddin exists, and be able to communicate with it, perhaps through a parent class that creates both.
Anyone know how to do this?
You could do this using a Microsoft Message Queue (MSMQ): Use Microsoft Message Queuing in C# for inter-process communication
This code project article shows how to detect a running instance and how to copy command line parameters between them:
Single-Instance C# Application - for .NET 2.0
Not quite what you are asking but might be of interest: Detecting a running instance of a program and passing it information
I ended up using named pipes to communicate between processes
I have in fact done exactly what you're asking about. I went for old fashioned windows messages. Nothing fancy - so it will just work! :-)
Let your addin create a "window" using the NativeWindow class and give it a predetermned, unique name. Then search for another instance of such a window using GetWindow and GetWindowText when your addin launches. Communicate using SendMessage or PostMessage. Easy as that.

How does someone go about writing a "macro" in C#?

I know something about MACROS. I don't mean the ASSEMBLY language kind. I am talking about those programs that you can use perform repetitions actions on another program. I am talking about those programs that you can use to record a series of events on your computer, like, mouse movements and button clicks and then you can play them back. Some of them are elaborate enough to run only on a paricular app that you designate.
I wrote one of sorts once. It was a program that launched an Excel sessions and then used the dynamic data exchage pipe of some kind to feed the excell session script commands. It worked.
But something on the level of the operating system, I imagine, is a whole different story.
How does someone go about writing a "macro" in C#?
I think the approach I will take is to use the spy routine that comes with the development environment to get a list of the proper messages and parameters (wm_lbuttondown for example) and then use dynamic data exchange to send those messages to the app.
So I have three questions.
Is this the best way to do this?
How do I get a handle to an app that is already running?
How do I send user-like messages to an app that is already running?
There are different answers based on many following factors:
is it 3rd party or your own
application?
does it have automation interface
GUI toolkit used in app
If it is a 3rd party app then you need to work on Windows API level via PInvoke - subclassing WinMain proc, capturing and sending input messages, etc. There are 3rd party library for that task. C# obviously is not a right choice for such task.
In case application has automation model (like Excel) it's a pretty straight forward to write program that will be interact with this app.
If it's your own application you want to enhance with macros functionality then you should take this into account on design state. If you use Command pattern from the beginning then it's not hard to program macro recording.
You should provide more details to get a better answer.
Oh, I almost forgot to answer those three questions
Is this the best way to do this?
Depends on concrete scenario
How do I get a handle to an app that is already running?
Depends on application. If it's a native Win app you can easily get process Id and window's handle via WinApi.
How do I send user-like messages to an app that is already running?
Once again it depends on application type. For native win apps you can easily send WM_XXX messages via WinAPI
Unless its something you need to add in your own program you can just download a keyboard/mouse macro program and use it to perform these repeatable actions.
On the other hand to perform macro's in your own program you would want to find a way to record the buttons clicked and write them to a temporary list that can be saved and then run the list by clicking the buttons (programmically).

Categories

Resources