I have the following code which finds a window by a partial name given..
I've tested it and it doesnt work in Dev c++ but it definitely works in visual studio 2008.. So I decided to make a form with nothing on it and implement this code to see if it will work.. The code is below and below that is my attempt.
#include <Windows.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct FindWindowData{
FindWindowData(TCHAR const * windowTitle)
: WindowTitle(windowTitle)
, ResultHandle(0)
{}
std::basic_string<TCHAR> WindowTitle;
HWND ResultHandle;
};
BOOL CALLBACK FindWindowImpl(HWND hWnd, LPARAM lParam){
FindWindowData * p = reinterpret_cast<FindWindowData*>(LongToPtr(lParam));
if(!p) {
// Finish enumerating we received an invalid parameter
return FALSE;
}
int length = GetWindowTextLength(hWnd) + 1;
if(length > 0) {
std::vector<TCHAR> buffer(std::size_t( length), 0);
if(GetWindowText(hWnd, &buffer[0], length)) {
// Comparing the string - If you want to add some features you can do it here
if(_strnicmp(&buffer[0], p->WindowTitle.c_str(), min(buffer.size(), p->WindowTitle.size())) == 0){
p->ResultHandle = hWnd;
// Finish enumerating we found what we need
return FALSE;
}
}
}
// Continue enumerating
return TRUE;
}
// Returns the window handle when found if it returns 0 GetLastError() will return more information
HWND FindWindowStart(TCHAR const * windowTitle){
if(!windowTitle){
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
FindWindowData data(windowTitle);
if( !EnumWindows(FindWindowImpl, PtrToLong(&data)) && data.ResultHandle != 0){
SetLastError(ERROR_SUCCESS);
return data.ResultHandle;
}
// Return ERROR_FILE_NOT_FOUND in GetLastError
SetLastError(ERROR_FILE_NOT_FOUND);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "HWND: " << FindWindowStart(TEXT("foobar "));
std::cout << "GetLastError() = " << GetLastError() << std::endl;
return 0;
}
My Attempt which failed with 1 Error!! =( :
#pragma once
#include <Windows.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct FindWindowData{
FindWindowData(TCHAR const * windowTitle)
: WindowTitle(windowTitle)
, ResultHandle(0)
{}
std::basic_string<TCHAR> WindowTitle;
HWND ResultHandle;
};
namespace FindWindowEnum {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
BOOL CALLBACK FindWindowImpl(HWND hWnd, LPARAM lParam){
FindWindowData * p = reinterpret_cast<FindWindowData*>(LongToPtr(lParam));
if(!p) {
// Finish enumerating we received an invalid parameter
return FALSE;
}
int length = GetWindowTextLength(hWnd) + 1;
if(length > 0) {
std::vector<TCHAR> buffer(std::size_t( length), 0);
if(GetWindowText(hWnd, &buffer[0], length)) {
// Comparing the string - If you want to add some features you can do it here
if(_strnicmp(&buffer[0], p->WindowTitle.c_str(), min(buffer.size(), p->WindowTitle.size())) == 0){
p->ResultHandle = hWnd;
// Finish enumerating we found what we need
return FALSE;
}
}
}
// Continue enumerating
return TRUE;
}
// Returns the window handle when found if it returns 0 GetLastError() will return more information
HWND FindWindowStart(TCHAR const * windowTitle){
if(!windowTitle){
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
FindWindowData data(windowTitle);
if( !EnumWindows(FindWindowImpl, PtrToLong(&data)) && data.ResultHandle != 0){
SetLastError(ERROR_SUCCESS);
return data.ResultHandle;
}
// Return ERROR_FILE_NOT_FOUND in GetLastError
SetLastError(ERROR_FILE_NOT_FOUND);
return 0;
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
Can anyone help me?? I've never had to convert before but this time I have no other choice =(
And no I don't want to alloc a console and do this.. I want it to run without a console that why I wanted a form.