Solved

Subclassing a form based on class name

Posted on 2006-07-20
11
380 Views
Last Modified: 2008-02-20
I need to detect the creation of windows of a certain windows class name.  
For example, if a user opens a browser window ("IEFrame"), I'd like intercept the windows message for that event, and have access to that window's handle so that I can supress the display of that window, and re-display it at a later time.

How would I do this?

Thanks!
0
Comment
Question by:glennuser
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
This can be done using Windows API. You need SetWindowsHookEx with WH_CBT parameter (system-wide CBT hook). Callback CBTProc function is called with HCBT_CREATEWND parameter when new window is created. C# doesn't support this directly, you need to use PInvoke. I think this is better to use C++/CLI for this.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
I cannot find C# sample for this, this is really not task for C#. If you know C++/CLI, take a look at Managed Spy sample:
http://msdn.microsoft.com/msdnmag/issues/06/04/ManagedSpy/

It contains ManagedSpyLib library written in C++/CLI, which installs system-wide WH_CALLWNDPROC hook and can be called from C# client. You must replace hook type, remove all spying stuff from this library, add your own code and change library interface. In this case you can detect windows creation from C# program.

About second task, supress the display of that window, I don't know whether it is possible.
0
 
LVL 3

Expert Comment

by:BBK_
Comment Utility
I agree with AlexFM, Hooking is the Ideal approach. But there is another way also. If you want to check some window in the system , use FindWindow API, and pass the class name as the parameter, run this in a Threading timer. So when ever you find an expected window, you can pass WM_Quit message to that window to Close that instance of that window. If you use the ProcessInformation, you will also get the path of the Executable from the Window Handle, so that you can re invoke the application in a later time also.
0
 

Author Comment

by:glennuser
Comment Utility
BBK:
But if I use FindWindow API doesn't that presuppose that the Window has already appeared?
I need to suppress the display the window before it first appears.
0
 
LVL 3

Expert Comment

by:BBK_
Comment Utility
Yes, it will appear for a moment, I mean once the window appeared , then only you will get the window handle in "EnumWindows" API.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:glennuser
Comment Utility
BBK:
I'm afraid that momentary flash of the window wouldn't pass muster.
0
 

Author Comment

by:glennuser
Comment Utility
If I had control of launching the application, would I have more options open?

Meaning, if my code was responsible for launching IE, could I do it in such a way that I would be able to accomplish this?
0
 
LVL 12

Expert Comment

by:topdog770
Comment Utility
If you launch the application, then yes.. you can keep track of the process id that's created and control the started application as you described, showing or not showing the window, etc.
0
 

Author Comment

by:glennuser
Comment Utility
TopDog:
But even if I have the process id, I can't do what I need until I get a handle.
It looks to me that from the process class I can get a MainWindowHandle, but its value is 0 until -- again -- the window is created, and then its too late.

Am I going about this the wrong way?
0
 
LVL 12

Accepted Solution

by:
topdog770 earned 500 total points
Comment Utility
From
==================================================
http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.windowstyle.aspx

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    /// <summary>
    /// Shell for the sample.
    /// </summary>
    class MyProcess
    {
       
        /// <summary>
        /// Opens the Internet Explorer application.
        /// </summary>
        void OpenApplication(string myFavoritesPath)
        {
            // Start Internet Explorer. Defaults to the home page.
            Process.Start("IExplore.exe");
                   
            // Display the contents of the favorites folder in the browser.
            Process.Start(myFavoritesPath);
 
        }
       
        /// <summary>
        /// Opens urls and .html documents using Internet Explorer.
        /// </summary>
        void OpenWithArguments()
        {
            // url's are not considered documents. They can only be opened
            // by passing them as arguments.
            Process.Start("IExplore.exe", "www.northwindtraders.com");
           
            // Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
            Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
        }
       
        /// <summary>
        /// Uses the ProcessStartInfo class to start new processes, both in a minimized
        /// mode.
        /// </summary>
        void OpenWithStartInfo()
        {
           
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");

======> // Here is key.. you can use the startinfo struct and tell the application to start with the
ProcessWindowStyle set to Hidden!

            startInfo.WindowStyle = ProcessWindowStyle.Minimized;
           
            Process.Start(startInfo);
           
            startInfo.Arguments = "www.northwindtraders.com";
           
            Process.Start(startInfo);
           
        }

        static void Main()
        {
                    // Get the path that stores favorite links.
                    string myFavoritesPath =
                    Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
               
                    MyProcess myProcess = new MyProcess();
         
            myProcess.OpenApplication(myFavoritesPath);
            myProcess.OpenWithArguments();
            myProcess.OpenWithStartInfo();

               }    
    }
}



==================================================
Here's a link with several examples and more info then probably needed..

http://www.codinghorror.com/blog/archives/000133.html

http://msdn.microsoft.com/msdnmag/issues/02/07/CQA/

0
 

Author Comment

by:glennuser
Comment Utility
Thanks for everyone's feedback.

TopDog, your code worked perfectly for IE, thanks.
Do you think you have any insight into why this approach does not work for some of the Office applications (Word, Outlook, OneNote seem to ignore it).  

I suppose its up to the application to honor the WindowStyle?
Perhaps I should open up a second question for these apps?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now