How to use win32 API in windows form

May I use win32 API in when windows form development?For the example , I tend to use FindWindow in windows form , but the compiling failed:


compile ouputs

NK2028: unresolved token (0A000011) "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const *,wchar_t const *)" (?FindWindowW@@$$J18YGPAUHWND__@@PB_W0@Z) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)
1>win1.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const *,wchar_t const *)" (?FindWindowW@@$$J18YGPAUHWND__@@PB_W0@Z) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)



Code

// win1.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"
#include <windows.h>
#include <winuser.h>
#include <conio.h>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

using namespace win1;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created

HWND Find = FindWindow(NULL,_T("Mozilla Firefox"));
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
Last edited on
You need to add 'User32.lib' which might not be added automatically in a CLR project. See:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
This works for me:
1
2
3
4
5
6
7
8
9
10
// somewhere outside the form
include <windows.h>
extern "C" __declspec(dllimport) HWND WINAPI FindWindow(LPCWSTR lpClassName, LPCWSTR 
                                                             lpWindowName);
// Inside an event handler in the form
  HWND hWindow = ::FindWindowW(L"MozillaWindowClass", nullptr);
  if (IsWindow(hWindow))
    MessageBox::Show(L"Found");
  else
    MessageBox::Show(L"Not Found");


A good book to learn C++ / CLR is
https://www.amazon.co.uk/Pro-Visual-CLI-NET-Platform/dp/1430210532/ref=sr_1_1?s=books&ie=UTF8&qid=1513890786&sr=1-1&keywords=stephen+fraser+c%2B%2B
Last edited on
Registered users can post here. Sign in or register to post.