Setting an external application Window
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Setting an external application Window

  1. #1
    Join Date
    Jan 2012
    Posts
    18

    Setting an external application Window

    Hey guys,

    I know how to do this in non .NET C++ but I need to import a library which isn't available in the express version in order to use HWND.

    Is there another method of setting an external applications window title?

    E.g. from "Untitled - Notepad" to "Notepad55" or something else.

    Here is the code for non .NET:

    HWND notepad = FindWindow(NULL, "Untitled - Notepad");
    if(notepad)
    SendMessage(notepad,WM_SETTEXT,0,(LPARAM)"Message Recieved");
    else
    MessageBox(0,"Notepad isn't open.","Report",MB_OK);

  2. #2
    Join Date
    Jan 2012
    Posts
    18

    Re: Setting an external application Window

    Sorry for not posting this in CODE tags, the menu isn't showing in my browser and I was unable to edit the previous post.

    Code:
    HWND notepad = FindWindow(NULL, "Untitled - Notepad");
     if(notepad)
        SendMessage(notepad,WM_SETTEXT,0,(LPARAM)"Message Recieved");
     else
        MessageBox(0,"Notepad isn't open.","Report",MB_OK);

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,927

    Re: Setting an external application Window

    Well, what is your real problem? .NET? Express version? Import a library? What library? Platform SDK is free and downloadable from MS.
    Best regards,
    Igor

  4. #4
    Join Date
    Jan 2012
    Posts
    18

    Re: Setting an external application Window

    I cannot set an external applications Window title.

    The following works in normal C++:
    http://i445.photobucket.com/albums/q...w55/VCCF-C.png

    This will refuse to work in Visual C++:
    http://i445.photobucket.com/albums/q...55/VCCF-VC.png

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,019

    Re: Setting an external application Window

    Do not confuse C++ with C++/CLI.

    [ Moved thread ]
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Setting an external application Window

    As documented in http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx, SendMessage() (and that implies SendMessageW()) is defined in user32.lib. This library isn't referenced by default from a CLR project. Adding the library to the additional dependencies on the linker input property page should fix the issue.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Jan 2012
    Posts
    18

    Re: Setting an external application Window

    Trying to #import "user32.lib" gives the following error:

    1>c:\users\fluidz\documents\everything\visual c++ projects\backup of vc++ version\minecraft keybot\minecraft keybot\MCKB.h(6): error C2812: #import is not supported with /clrure and /clr:safe

    I have Googled around but I don't know how to swap back to normal CLR.

    When I upgrade my project to a Visual Studio one, view the tool in the IDE. I get an error for importing <sal.h>. So basically, it doesn't work on a VC++ project, does on a Visual Studio, however, on the Visual Studio project <sal.h> is missing or non-existent.

    Sal error: 1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\crtdefs.h(68): fatal error C1083: Cannot open include file: 'sal.h': No such file or directory


    ...

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,927

    Re: Setting an external application Window

    Trying to #import "user32.lib" gives the following error:
    You should read anything about #import directive. As long as user32.lib is neither a COM server nor type library, you cannot import it. Instead, it appears to be a plain WinAPI import library which should be correspondingly added to your project.

    Quote Originally Posted by Eri523
    Adding the library to the additional dependencies on the linker input property page should fix the issue.
    It's project settings.
    Last edited by Igor Vartanov; January 18th, 2012 at 12:39 AM.
    Best regards,
    Igor

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Setting an external application Window

    Quote Originally Posted by Fluidz View Post
    Trying to #import "user32.lib" gives the following error:

    [...]
    As Igor already confirmed, #import is the wrong directive here. The right one, if you don't want to simply add the library on the property page mentioned, is this:

    Code:
    #pragma comment(lib, "user32.lib")
    I have Googled around but I don't know how to swap back to normal CLR.
    This option is on the General property page (for the project) and on C/C++ -> General for both the project and individual source files.

    [...] I get an error for importing <sal.h>. So basically, it doesn't work on a VC++ project, does on a Visual Studio, however, on the Visual Studio project <sal.h> is missing or non-existent.

    Sal error: 1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\crtdefs.h(68): fatal error C1083: Cannot open include file: 'sal.h': No such file or directory
    I have Visual Studio 10, not 11, but I do have that file, in the same directory as crtdefs.h. Perhaps there's something wrong with your installation? BTW, headers aren't imported either, they're included.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #10
    Join Date
    Jan 2012
    Posts
    18

    Re: Setting an external application Window

    Code:
    #pragma comment(lib, "user32.lib")
    This seemed to fix my problem - Thanks!

    I do, however, have another. The generic SendMessage(hwnd,WM_CHAR,'a',0); from 'normal' C++ isn't working. Any ideas?

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Setting an external application Window

    Quote Originally Posted by Fluidz View Post
    I do, however, have another. The generic SendMessage(hwnd,WM_CHAR,'a',0); from 'normal' C++ isn't working. Any ideas?
    You did include Windows.h or at least Winuser.h, didn't you? From what you told me, I can't really tell you more than that shot in the dark. How did you try to use it and how did it fail?

    In the meantime, as a rough outline how calling native functions like that works, following is a C++/CLI module that, among other things, encapsulates the Win32 PostMessage() which, as you probaly know, is quite similar. The module is taken from the demo project attached to http://www.codeguru.com/forum/showthread.php?p=2047542. First the header file:

    Code:
    // WndMessageHolder.h
    
    #pragma once
    
    #include <Windows.h>
    
    namespace BringUpTest
    {
    
    using namespace System;
    using namespace System::Windows::Forms;
    
    public ref class WndMessageHolder
    {
    public:
      static WndMessageHolder ^GetInstance();
    
      static Int32 GetLastError_();
    
      initonly int WM_BRINGUP;
      initonly int WM_MESSAGEECHO_ADVERTISE;
      initonly int WM_MESSAGEECHO_ADVERTISEMENTRESPONSE;
    
      static bool PostMessage_(IWin32Window ^wnd_, int msg_, IntPtr wparam_, IntPtr lparam_);
      static bool PostMessage_(IntPtr ipHwnd_, int msg_, IntPtr wparam_, IntPtr lparam_);
      static bool BringWindowToTop_(IWin32Window ^wnd_);
      static bool BringWindowToTop_(IntPtr ipHwnd_);
    
    protected:
      WndMessageHolder();
    
    private:
      static WndMessageHolder ^s_wmhInstance = nullptr;
    };
    
    }
    ... and here's the implementation file:

    Code:
    // WndMessageHolder.cpp
    
    #include "StdAfx.h"
    
    #include "WndMessageHolder.h"
    
    #include <tchar.h>
    
    #pragma comment(lib, "user32.lib")
    
    using namespace BringUpTest;
    
    WndMessageHolder::WndMessageHolder(void)
    {
      TCHAR tstrBringUpMessage[46] = _T("{EF278EFA-CC40-45ef-B645-0996E5085FA4}");
    #ifdef _DEBUG
      _tcscat_s(tstrBringUpMessage, sizeof(tstrBringUpMessage) / sizeof(TCHAR), _T("_debug"));
    #endif
      WM_BRINGUP = RegisterWindowMessage(tstrBringUpMessage);
    
      TCHAR tstrMessageEchoAdvertise[46] = _T("{C63DD9D3-E18A-4309-94F5-6336B0A3E639}");
    #ifdef _DEBUG
      _tcscat_s(tstrMessageEchoAdvertise, sizeof(tstrMessageEchoAdvertise) / sizeof(TCHAR), _T("_debug"));
    #endif
      WM_MESSAGEECHO_ADVERTISE = RegisterWindowMessage(tstrMessageEchoAdvertise);
    
      TCHAR tstrMessageEchoAdvertisementResponse[46] = _T("{862A2170-8BA3-4f1d-BCDB-FAB9936F7F33}");
    #ifdef _DEBUG
      _tcscat_s(tstrMessageEchoAdvertisementResponse, sizeof(tstrMessageEchoAdvertisementResponse) / sizeof(TCHAR), _T("_debug"));
    #endif
      WM_MESSAGEECHO_ADVERTISEMENTRESPONSE = RegisterWindowMessage(tstrMessageEchoAdvertisementResponse);
    }
    
    WndMessageHolder ^WndMessageHolder::GetInstance()
    {
      if (!s_wmhInstance) s_wmhInstance = gcnew WndMessageHolder;
      return s_wmhInstance;
    }
    
    bool WndMessageHolder::PostMessage_(IWin32Window ^wnd_, int msg_, IntPtr wparam_, IntPtr lparam_)
    {
      return PostMessage_(wnd_->Handle, msg_, wparam_, lparam_);
    }
    
    bool WndMessageHolder::PostMessage_(IntPtr ipHwnd_, int msg_, IntPtr wparam_, IntPtr lparam_)
    {
      return ::PostMessage((HWND)(void *)ipHwnd_, msg_, (WPARAM)(void *)wparam_, (LPARAM)(void *)lparam_);
    }
    
    bool WndMessageHolder::BringWindowToTop_(IWin32Window ^wnd_)
    {
      return BringWindowToTop_(wnd_->Handle);
    }
    
    bool WndMessageHolder::BringWindowToTop_(IntPtr ipHwnd_)
    {
      return ::BringWindowToTop((HWND)(void *)ipHwnd_);
    }
    
    Int32 WndMessageHolder::GetLastError_()
    {
      return ::GetLastError();
    }
    Finally, for further demonstration, here's a function that calls it:

    Code:
    // Handles WM_MESSAGEECHO_ADVERTISE message - gets passed the non-primary instance main window handle
    
    void Form1::MessageEchoAdvertiseHandler(IntPtr ipHwndNonPrimary)
    {
      bool bResult = WndMessageHolder::PostMessage_(ipHwndNonPrimary,
        WndMessageHolder::GetInstance()->WM_MESSAGEECHO_ADVERTISEMENTRESPONSE, Handle, (IntPtr)0);
      Trace::Assert(bResult, String::Format("Posting WM_MESSAGEECHO_ADVERTISEMENTRESPONSE message failed (code {0})",
        WndMessageHolder::GetLastError_()));
    }
    Of course it would have been possible to directly call the native functions, but I usually prefer to encapsulate stuff like that rather than scattering it all around the entire app.
    Last edited by Eri523; January 21st, 2012 at 10:35 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  12. #12
    Join Date
    Jan 2012
    Posts
    18

    Re: Setting an external application Window

    I tried to use SendMessage with the window in focus and out of focus immediately before another SendMessage which changes the windows title. The second one works, however, the first shows no sign of working at all.

  13. #13
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Setting an external application Window

    I'm afraid that's still too few information. What is "the window in focus and out of focus"?

    Quote Originally Posted by Fluidz View Post
    The second one works, however, the first shows no sign of working at all.
    Is that meant to mean you're calling SendMessage() twice in succession with identical parameters, and only the second one of those calls results in any observable effect?

    The bottom line clearly is: Please post the concrete code involved, best accompanied by a detailed descriptgion of your overall scenario, as is seems to involve some "outside" app in some way.

    Perhaps this is a general issue regarding inter-process interaction rather than one specific to C++/CLI or even .NET.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  14. #14
    Join Date
    Jan 2012
    Posts
    18

    Re: Setting an external application Window

    I am reinstalling Visual C++ so I shall post the code later on as an edit of this post.

  15. #15
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Setting an external application Window

    Please post the code in a new post, otherwise I won't get notified when yourf update comes in. (And others aren't able to tell there's something new from the thread listing either.)
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center