calling a WINAPI function from a managed main creates linker error

OK, here is the latest saga from this newbie:

Whenever I try to call EnumDesktopWindows or GetWindowsText the linker vomits out the following:

LocateWindow.obj : error LNK2028: unresolved token (0A00000D) "int __stdcall EnumDesktopWindows(struct HDESK__ *,int (__stdcall*)(struct HWND__ *,long),long)" ( EnumDesktopWindows@@$$J212YGHPAUHDESK__@@P6GHPAUHWND__@@J@ZJ@Z) referenced in function "public: void __thiscall LocateWindow::setPopulateWindowArray(void)" ( setPopulateWindowArray@LocateWindow@@$$FQAEXX

Here is the code: (Yeah, its ugly. I am coming at this slow so I get as much of the concepts down as I can)

// LocateWindow.cpp : main project file.///////////////////////////////////////

#include "stdafx.h"

#include "LocateWindow.h"

//using namespace System;

 

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

{

LocateWindow mainLocateWindow;

mainLocateWindow.PopulateWindowArray();

System::Console::WriteLine(L"Hello World");

return 0;

}

//LocateWindow.h///////////////////////////////////////////////////////////////////

#pragma once

#include <windows.h>

using namespace System::Runtime::InteropServices;

 

//using namespace System;

static BOOL WINAPI myEnumWindowsHandler(HWND paramTopLevelHandle, LPARAM paramLPARAM){

int functionTest;

LPSTR functionWindowTitle;

functionWindowTitle = new char[MAX_PATH];

functionTest=GetWindowText(paramTopLevelHandle, (LPSTR)functionWindowTitle, MAX_PATH);

if (functionTest){

System::Console::WriteLine(functionWindowTitle);

delete functionWindowTitle;

return true;

}

else{

System::Console::WriteLine("No Title");

delete functionWindowTitle;

return false;

}

};

 

class LocateWindow{

HDESK classDesktop;

public:

void PopulateWindowArray(){

::EnumDesktopWindows(NULL, myEnumWindowsHandler, (LPARAM)this);

}

};


 



Answer this question

calling a WINAPI function from a managed main creates linker error

  • Scafe

    Have you tried linking with user32.lib

    Thanks,
      Ayman Shoukry
      VC++ Team

  • Mujeeb_rahman

    I have looked all through the menus of the IDE, cannot find where to make that link.  When I installed the SDK, I made sure the additional dependencies read as follows:

    AdditionalDependencies="kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib"

    in file corewin_express.vsprops.

    The funny thing is I can create a bare bones WIN32 app so it seems my SDK is installed properly :(.


  • Beaulieu

    I just tried your sample code with VC2005 and it worked as expected. Make sure you are linking to User32.lib.

    Thanks,
      Ayman Shoukry
      VC++ Team

  • SAS8721

    Honestly, I am not sure how additional dependencies work. Other folks on the forum might know.

    Any ways, I am glad that solved your issue.

    Thanks,
      Ayman Shoulry
      VC++ Team

  • Michela

    Just in case, under the project properties --> linker --> command line, add user32.lib and see if that takes care of the errors.

    Thanks,
     Ayman Shoukry
     VC++ Team



  • Kajit

    OK, here they are:

    LocateWindow.obj : error LNK2028: unresolved token (0A00000D) "int __stdcall EnumDesktopWindows(struct HDESK__ *,int (__stdcall*)(struct HWND__ *,long),long)" ( EnumDesktopWindows@@$$J212YGHPAUHDESK__@@P6GHPAUHWND__@@J@ZJ@Z) referenced in function "public: void __thiscall LocateWindow::PopulateWindowArray(void)" ( PopulateWindowArray@LocateWindow@@$$FQAEXXZ)

    LocateWindow.obj : error LNK2028: unresolved token (0A000011) "int __stdcall GetWindowTextA(struct HWND__ *,char *,int)" ( GetWindowTextA@@$$J212YGHPAUHWND__@@PADH@Z) referenced in function "int __stdcall `anonymous namespace'::myEnumWindowsHandler(struct HWND__ *,long)" ( myEnumWindowsHandler@ A0x767a8b47@@$$FYGHPAUHWND__@@J@Z)

    LocateWindow.obj : error LNK2019: unresolved external symbol "int __stdcall GetWindowTextA(struct HWND__ *,char *,int)" ( GetWindowTextA@@$$J212YGHPAUHWND__@@PADH@Z) referenced in function "int __stdcall `anonymous namespace'::myEnumWindowsHandler(struct HWND__ *,long)" ( myEnumWindowsHandler@ A0x767a8b47@@$$FYGHPAUHWND__@@J@Z)

    LocateWindow.obj : error LNK2019: unresolved external symbol "int __stdcall EnumDesktopWindows(struct HDESK__ *,int (__stdcall*)(struct HWND__ *,long),long)" ( EnumDesktopWindows@@$$J212YGHPAUHDESK__@@P6GHPAUHWND__@@J@ZJ@Z) referenced in function "public: void __thiscall LocateWindow::PopulateWindowArray(void)" ( PopulateWindowArray@LocateWindow@@$$FQAEXXZ)


  • Arul Ram

    OK, that worked. Sorry if I was so dense.  Have not done any work at the command line, since I normally have had no trouble with the auto settings.  But shouldn't the line where I put the additional dependencies in the corewin_.... file have taken care of that
  • Hamish_NZ

    If that is the linker command line, you just need to type in user32.lib. I would also macke sure that is not there already. You can confirm this by looking at the build log.

    Thanks,
      Ayman Shoukry
      VC++ Team



  • RazzleDazzle

    Sorry, haven't messed with the command line functions much.  Do have to put a switch in front of it with a / or do I just type in user32.lib
  • DavidNJ

    Hmmm, I use VC2005 as well.  I used the app wizard to create the solution. I currently have 2 projects in the solution:

    One is a CLR Windows Form. That one compiles and executes great. Until I try to use EnumDesktopWindows, then it goes crazy, complaining during the compile process about me trying to do a _stdcall with the /clr:pure switch.

    So I created a second project with a target as a CLR console.

    As a CLR console, I can at least create the .obj file, but the linker blows up on it with either link2028 or link2019 errors.




  • numberonesuperguy

    Could you display the exact errors you are seeing

    Thanks,
      Ayman Shoukry
      VC++ Team

  • calling a WINAPI function from a managed main creates linker error