Jump to content

FindWindow Problem in CLR Project

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Blaxus

Blaxus

    Newbie

  • Members
  • Pip
  • 5 posts
Hey, i'm trying to find a window, and for some reason the code for findwindow doesn't work properly in my project.

Here are my files in the project:

test1.h :
#pragma once

using namespace System;
using namespace System::Windows::Forms;

ref class CTest1 : public Form
{
public:
	CTest1(void);
};

test1.cpp :
#include "Test1.h"


CTest1::CTest1(void)
{
}

central.cpp :
#include <Windows.h>
#include "Test1.h"

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
	Application::Run(gcnew CTest1());

	// Find the window of the calculator
	HWND hWnd = FindWindow(0, "Calculator");
    
	// The Window Does not Exist
	if(hWnd == 0)
    {
		//MessageBox(0, "Error cannot find window.", "Error", MB_OK|MB_ICONERROR);
    } 
    else 
    {
		//MessageBox(0, "Window exists.", "Error", MB_OK|MB_ICONERROR);
	}

	return 0;
}

this code returns these errors:

Quote

1>------ Build started: Project: Test1, Configuration: Debug Win32 ------
1> central.cpp
1>central.obj : error LNK2028: unresolved token (0A00004A) "extern "C" struct HWND__ * __stdcall FindWindowA(char const *,char const *)" (?FindWindowA@@$$J18YGPAUHWND__@@PBD0@Z) referenced in function "extern "C" int __stdcall WinMain(struct HINSTANCE__ *,struct HINSTANCE__ *,char *,int)" (?WinMain@@$$J216YGHPAUHINSTANCE__@@0PADH@Z)
1>central.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall FindWindowA(char const *,char const *)" (?FindWindowA@@$$J18YGPAUHWND__@@PBD0@Z) referenced in function "extern "C" int __stdcall WinMain(struct HINSTANCE__ *,struct HINSTANCE__ *,char *,int)" (?WinMain@@$$J216YGHPAUHINSTANCE__@@0PADH@Z)
1>c:\users\dave\documents\visual studio 2010\Projects\Test1\Debug\Test1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have probably overlooked something because in normal c++ it works however in a CRL project this findwindow causes problems, maybe something i should have included to make it work, i don't really know.

Hope you guys can help me out ;)

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Windows Forms doesn't use WinMain() function. It uses just main(). If you are using vc++ 2008/2010 then the IDE will generate that file for you. If not and you have to do it yourself, here is the contents of the project I am working on

#include "stdafx.h"

#include "Form1.h"


using namespace AddressBooki;


[STAThreadAttribute]

int main(array<System::String ^> ^args)

{

	// Enabling Windows XP visual effects before any controls are created

	Application::EnableVisualStyles();

	Application::SetCompatibleTextRenderingDefault(false); 


	// Create the main window and run it

	Application::Run(gcnew Form1());

	return 0;

}


Several articles have been written about how to use unmanaged functions in windows forms and other managed c++ programs. Hereare a few of them.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Make sure you have included the right library ( User32.lib) that contains the FindWindow function.

I hope this helps!

-Munir

#4
kernelcoder

kernelcoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 309 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal
User32.lib is already in the linker settings to link. To solve this error follow the steps below...
  • Right click on project -- > Properties --> Configuration Properties --> Linker --> Input
  • Select the 'Additional Dependencies" property node and then Click the on the 3 dotted (...) button
  • Then in the 'Addition Dependencies' window, mark 'Checked' the 'Inherit from parent or project defaults'
  • Click on Ok button and on the 'Apply' button on the parent window. Now build and run the project.
This way the linker will be linked to "kernel32.lib, user32.lib, gdi32.lib, winspool.lib, comdlg32.lib, advapi32.lib, shell32.lib, ole32.lib, oleaut32.lib, uuid.lib, odbc32.lib, odbccp32.lib" libraries. Now if you don't want to link to all those libraries and want to only the user32.lib one, follow the steps below...
  • Right click on project -- > Properties --> Configuration Properties --> Linker --> Input
  • Select the 'Additional Dependencies" property node and then Click the on the 3 dotted (...) button
  • Then on the 'Additional Dependencies" popup, input "user32.lib" on the top textbox and click on 'Ok' button
  • Click on 'Apply' button and re-compile your application. .............. the linker errors are gone!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users