CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2003
    Posts
    3

    Finding window handle from a service

    Hi All!

    I´m sorry if this ha been posted already but I haven´t been able to find it....

    I have an NT service that is trying to send a message to a user application window. I know the window title so I´m trying to use FindWindow and FindWindowEx to get a handle that I can use to send messages using PostMessage and SendMessage... As long as I do it from a "normal" application (Window or console based) it works neat and tidy, but when run from my service, the same piece of code i sunable to retrieve a handle to the window....

    So, my question is: How do I correctly get the handle of a user application from an NT service?

    Thanks in advance!

  2. #2
    Join Date
    May 2002
    Posts
    52
    You should set your service to interact with the desktop.

  3. #3
    Join Date
    Mar 2003
    Posts
    3
    Doesn´t work if the service runs as a user account(which mine has to do)....

    However, I now have found a way to find the handle to an interactive window anyway.

    Thanks anyway!

  4. #4
    Join Date
    Jan 2002
    Location
    TamilNadu, India
    Posts
    158
    How did you do it? If you tell us we may also be benefitted.
    Muthu

  5. #5
    Join Date
    Mar 2003
    Posts
    3
    Well, it was actually simpler than I thought....

    In short (I don´t have the details here, but if you like I can elaborate more later on):

    Start by getting the Process object of the target application (the one running in user context), using GetProcessByName(string processname).

    Now that you have the process you can actually find an IntPtr handle to the main window of the target process by using Process.GetMainWindow() (don´t remember the exact name, but it was similar to that).

    Now you have IntPtr that you can pass to the API function PostMessage or SendMessage as the recieving window!

    Let me know if you have any problems implementing this...

    Greetings!

  6. #6
    Join Date
    Jan 2002
    Location
    TamilNadu, India
    Posts
    158
    Thanks for the tip!
    Muthu

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured