-
January 13th, 2012, 01:59 PM
#1
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);
-
January 13th, 2012, 02:01 PM
#2
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);
-
January 14th, 2012, 05:22 AM
#3
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
-
January 14th, 2012, 08:44 AM
#4
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
-
January 14th, 2012, 09:53 AM
#5
Re: Setting an external application Window
Do not confuse C++ with C++/CLI.
[ Moved thread ]
-
January 14th, 2012, 05:43 PM
#6
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.
-
January 18th, 2012, 01:14 AM
#7
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
...
-
January 18th, 2012, 01:31 AM
#8
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.
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 01:39 AM.
Best regards,
Igor
-
January 18th, 2012, 07:57 AM
#9
Re: Setting an external application Window
Originally Posted by Fluidz
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.
-
January 21st, 2012, 11:17 AM
#10
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?
-
January 21st, 2012, 01:56 PM
#11
Re: Setting an external application Window
Originally Posted by Fluidz
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 11: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.
-
January 22nd, 2012, 11:28 PM
#12
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.
-
January 23rd, 2012, 12:06 AM
#13
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"?
Originally Posted by Fluidz
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.
-
January 23rd, 2012, 12:40 AM
#14
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.
-
January 23rd, 2012, 08:41 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|