-
FindWindowLike()
I wrote a program for work that finds the window name
ex:
Code:
HWND hWnd = FindWindow(blah, NULL);
Well, apparently (assume blah is the actual window name) the name changes. Is there a way to include wildcards in the function?
I did some research and saw a vb function called FindWindowLike that takes more parameters
ex:
Code:
FindWindowLike(hWnd(), 0, "*blah*", "*", Null)
However, it seems it's VB specific, and I didn't find anything dealing with C\++... Any recommendations?
-
try this:
Code:
struct result_stru {
char* text;
UINT (__stdcall*GET)(HWND,LPSTR,UINT);
HWND hRet;
};
BOOL WINAPI StruEnumProc( HWND hwnd, result_stru* stru )
{
char loc_buf[128];
stru->GET( hwnd, loc_buf, 127 );
if( strstr( loc_buf, stru->text ) ) {
stru->hRet = hwnd;
return FALSE;
}
return TRUE;
}
//case sensitive!
HWND FindWindowTitleContains( char* text )
{
result_stru res = { text, (UINT (__stdcall *)(HWND,LPSTR,UINT))GetWindowTextA, 0 };
EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
return res.hRet;
}
//case sensitive!
HWND FindWindowClassContains( char* text )
{
result_stru res = { text, RealGetWindowClassA, 0 };
EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
return res.hRet;
}
int main()
{
cout << FindWindowTitleContains( "Microsoft Visual Stu" ) << endl;
cout << FindWindowClassContains( "tooltips_" ) << endl;
cin.get();
}
-
Great, works like a charm! Thanks so much.
I also found this but couldn't get it to work initially
Code:
#include <windows.h>
#include <stdio.h>
BOOL CALLBACK SearchProc(HWND hWnd, LPARAM lParam)
{
int nLen = GetWindowTextLength(hWnd);
LPSTR lpszTitle = new CHAR[nLen + 1];
GetWindowText(hWnd, lpszTitle, nLen);
if(strstr(lpszTitle, "- Bubbles")){
// It found "- Bubbles" in the title of the window
// do what you want with the window now
delete lpszTitle;
return TRUE;
}
delete lpszTitle;
return TRUE;
}
int main(int argc, char **argv)
{
EnumWindows(SearchProc, NULL);
return 0;
}
I think cause I overlooked the fact that it's looking for the title as opposed to the class haha. Silly me ha.
-
Let me share my code on here & get some feedback. So here is the entire code..
PHP Code:
// blah.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <math.h>
#include <windows.h>
using namespace std;
bool minimized = false;
bool maximized = true;
bool msgHide = false;
bool msgShow = false;
struct result_stru
{
char* text;
UINT (__stdcall*GET)(HWND,LPSTR,UINT);
HWND hRet;
};
BOOL WINAPI StruEnumProc( HWND hwnd, result_stru* stru )
{
char loc_buf[128];
stru->GET( hwnd, loc_buf, 127 );
if( strstr( loc_buf, stru->text ) )
{
stru->hRet = hwnd;
return FALSE;
}
return TRUE;
}
//case sensitive!
HWND FindWindowTitleContains( char* text )
{
result_stru res = { text, (UINT (__stdcall *)(HWND,LPSTR,UINT))GetWindowTextA, 0 };
EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
return res.hRet;
}
//case sensitive!
HWND FindWindowClassContains( char* text )
{
result_stru res = { text, RealGetWindowClassA, 0 };
EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
return res.hRet;
}
int _tmain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//Afx:00400000:b:00010011:00000000:00000000 --> This is VERY odd, but it's the handle to the ShoreWare Agent Tool Bar
// HWND hWnd = FindWindow("Afx:00400000:b:00010011:00000000:00000000", NULL );
HWND hWnd = FindWindowClassContains( "Afx:00400000:b:0001" );
/*Local Variables*/
// WINDOWPLACEMENT wd;
// UINT nCurShow;
//We do a while loop (this allows the program to run in the background)
while(1)
{
Sleep (50);
// Stealth mode so we don't see the command prompt window
/*
HWND stealth;
AllocConsole();
stealth = FindWindowA("ConsoleWindowClass",NULL);
ShowWindow(stealth,0);
*/
HWND stealth = FindWindowA("ConsoleWindowClass",NULL);
FreeConsole();
//If you hit Esc, it will close the program
if((GetAsyncKeyState(VK_ESCAPE)&1))
return 0;
//Check if the window handle exists
if(hWnd)
{
Sleep (50);
/*Local Variables declared to get a Process Handle ont he Window Object*/
DWORD ProcessID;
GetWindowThreadProcessId( hWnd, &ProcessID );
HANDLE Process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID );
//Check if the process exists
if(Process)
{
/*More local variables declared. These variables will hold the read information from the memory address*/
BYTE data;
DWORD datasize = sizeof(data);
//Read the data from memory
ReadProcessMemory(Process,(void *)0x005556FC, &data, datasize, 0);
/*
We can check to see what state the agent toolbar is in by reading the data from memory
Information:
Toggle between release\resume state: 0x005556FC <--- This is the address where the states are stored
BYTE data = *(int*)0x005556FC;
DWORD datasize = sizeof(data);
Now we check the values. We know that the value will either be 0 or 1.
0 = release - green
1 = resume - flashing red
Release mode - green
00 01 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
Resume mode - red flashing
01 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*/
/*
NOTE:
There are 2 ways to do this. Method 1 seems to run a bit better, but I still need to find a way to optimize the code so it runs more smoothly.
*/
/*Method 1*/
//We are checking to see whether or Shoretel is in Resume Mode & whether or not the Window is already minimized
if((int)data == 1 && minimized == true)
{
//Assuming the above statement is true, we go ahead and check to make sure if the Window is minimized
if((ShowWindow(hWnd, SW_MINIMIZE)))
{
//Maximize the window
ShowWindow(hWnd, SW_SHOWNORMAL);
maximized = true;
minimized = false;
//This is optional, but the sole purpose of this is to popup a messagebox & let the user know what's going on
if(msgShow != true)
{
MessageBoxA(NULL,"Maximized now","Mnfo",MB_OK);
msgShow = true;
msgHide = false;
}
}
}
else if((int)data == 0 && maximized == true)
{
if((ShowWindow(hWnd, SW_SHOWNORMAL)))
{
//Minimize the window
ShowWindow(hWnd, SW_MINIMIZE);
minimized = true;
maximized = false;
//This is optional, but the sole purpose of this is to popup a messagebox & let the user know what's going on
if(msgHide != true)
{
MessageBoxA(NULL,"Minimized now","Info",MB_OK);
msgHide = true;
msgShow = false;
}
}
}
/*
//This is just for testing purposes
//printf("Value for 0x005556FC is = %i \n", (int) data);
//Method 2
//This is another way to check the state the Window is in
if(GetWindowPlacement( hWnd, &wd ))
{
//We store the variable inside nCurShow
nCurShow = wd.showCmd;
//We check to see if Window is minimized & whether Shoretel is in resume mode
if((int)data == 1 && minimized == true && nCurShow == SW_MINIMIZE)
{
//Maximize the Window and set the foreground
ShowWindow( hWnd, SW_SHOWNORMAL);
SetForegroundWindow(hWnd);
//This is optional, but the sole purpose of this is to popup a messagebox & let the user know what's going on
if(msgShow != true)
{
MessageBoxA(NULL,"maximized now","info",MB_OK);
msgShow = true;
msgHide = false;
}
}
else if((int)data == 0 && maximized == true && nCurShow == SW_SHOWNORMAL)
{
//Minimize the Window and set the foreground
ShowWindow( hWnd, SW_MINIMIZE );
SetForegroundWindow(hWnd);
//This is optional, but the sole purpose of this is to popup a messagebox & let the user know what's going on
if(msgHide != true)
{
MessageBoxA(NULL,"minimized now","info",MB_OK);
msgHide = true;
msgShow = false;
}
}
}
else
{
//The default state it should be in is ShowNormal, which sets the application in a default state.
ShowWindow( hWnd, SW_SHOWNORMAL );
SetForegroundWindow(hWnd);
}
*/
//We want to make sure we close the Process handle so that we don't corrupt the memory access or cause any unexpected errors
CloseHandle(Process);
}
}
}
//On Exit, restore the window
ShowWindow(hWnd, SW_SHOWNORMAL);
return 0;
}
Kinda sloppy, sorry about that, anywho, it works the way it's supposed to, but when I minimize\maximize the window I have a messagebox that comes up saying it's minimized or maximized
, however I need to make it VERY apparent what state the window is in because alot of our employees are older and have a hard time seeing some things like that, so I was thinking instead of a small messagebox, make a noticable flashing screen (say red\green flashing screen) that has the state written down in big characters "Blah is minimized" or "Blah is maximized" and then if they click on it, it gets rid of the flashing screen until the state changes again.
I'm assuming something like this would require opengl, because atm, I'm stumped on how to do this. If it would be any easier (I'm not sure), I could potentially make a gif that flashes in a screen resolution of 1024 x 768 so just make 2 gif's and execute them based on the application state.
Not sure, trying to get some ideas to see the best way of accomplishing a task like this.
Thanks!
-
you have alot of commented code blocks, and without syntax highlighting its kinda hard to see what is commented and what not.
if you want an app without console window, just create a Win32 project instead of Console ;)
the 'Afx:00400000:b:00010011:00000000:00000000' is an auto-generated class, that is generated by the Afx framework from MS :)
so, if there are more apps running that are made using that you could detect the wrong window.
Seeing as you do not use the window at all,
enumerate all processes instead, and match the process name. (or, if that can be different, get a string from the application, and with ReadProcessMemory match the process)
-
Hmm, there is something for me to ponder about then. Thanks for the input. I changed the tags so it's easier to read.
As far as why I went with a console over win32, primarily cause console is easier for me to understand haha. I'll see what I can do though. Thanks!
-
Maybe try this:
http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx
Also you could maybe try changing desktop color from red to green etc.
It saved in:
HKEY_CURRENT_USER\ControlPanel\Colors\
Key:
Background
-
nice and informative read so far. Made me a bit curious about that shortel app why its window state needs to be controlled.
I wondered if this mechanism would be reproducable with .net, I knew that u can easily launch external applications and check them, but not how to control window state of ext. apps with .net. Well a bit investigation later and with the help of win api .net should be able to do: http://www.c-sharpcorner.com/UploadF...95904AM/2.aspx
and for flashing the window I found this snippet:
Code:
Private Declare Function FlashWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal bInvert As Long) As Long
-
This is for VB right? :)
Another way of doing what James wants to do is to write normal Win32 app (not console one) with one big form edited in Resource Editor. This app would sit in the system tray and when needed show the form, with big button ok or whatever.
@James
What's the proccess name of this app?
-
the example is in C# ( ; behind each command), but VB is almost the same. Eiither win32, mfc or .net its all using a framework :)