Ajanth
Ajanth

Reputation: 2515

Using win32 functions in windows forms project.(.net envir)

i need to use some win32 functions in my windows form project under clr mode.(v c++ 2005) The Error I get when i use win32 functions directly in my forms project is

**dbms.obj : error LNK2028: unresolved token (0A00000E) "extern "C" struct HWND__ * __stdcall GetForegroundWindow(void)" (?GetForegroundWindow@@$$J10YGPAUHWND__@@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)
dbms.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall GetForegroundWindow(void)" (?GetForegroundWindow@@$$J10YGPAUHWND__@@XZ) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)**

my main cpp code is:

#include "stdafx.h"
#include "Form1.h"
#include "windows.h"

using namespace dbms;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    HWND neu;
    neu=GetForegroundWindow();
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 
    Application::Run(gcnew Form1());
    return 0;
}

the code is just to demonstrate the errors i get. This may be silly, but i know atleast a little about win32 prog but nothing about .net platform. if some one can help me use a win32 func in the above code ill be gratefull. (ive not included the form.h file.. i think it may not be needed)

Upvotes: 1

Views: 934

Answers (1)

Hans Passant
Hans Passant

Reputation: 942358

It is a linker error, not a compile error. You need to tell the linker to search the Windows import libraries for these winapi identifiers.

Right-click your project in the Solution Explorer window, Properties, Linker, Input, Additional Dependencies setting. Delete $(NoInherit). That allows the defaults from the "Core Windows Libraries" project property sheet to be used, it specifies the .lib files of the most common Windows dlls. Including user32.lib, the one that declares GetForegroundWindow().

You can see the list of the .libs that were inherited from the project property sheet by clicking the dotted button in the text box. "Inherited values" list. If you use an 'obscure' winapi whose import library is not in the list then you need to add the name of the .lib to the setting. The .lib you need is documented in the MSDN Library article for the winapi function in the "Function Information" section at the bottom of the article.

Upvotes: 1

Related Questions