From: SQACPP on
I'm trying to understand an error when compiling a C++ form project :

Error LNK2028 unresolved token...
Error LNK2019 Unrevolved external symbol ...


Here is how to understand/reproduce my problem with VS2005 :
1- Create a C++ win32 console application with the defaults parameters
2- In the header section add #include <windows.h> and add the
following code in the main() :
HWND hNotepad;
hNotepad = FindWindow(L"Notepad", 0);
3- Compile the console application
-> It compile without any error

4- Now Create a C+ Form application with the default parameters when
creating the new project.
5- In the header section add #include <windows.h> and add the
following code in the main() or somewhere else :
HWND hNotepad;
hNotepad = FindWindow(L"Notepad", 0);

** Now the compiler return errors :

Error error LNK2028: unresolved token (0A00000C) "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(a)System@@@Z) AutomationWinForm.obj

Error 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(a)System@@@Z) AutomationWinForm.obj
Why?? How can this can be fixed?

Thanks again!

From: Igor Tandetnik on
"SQACPP" <lsdisciples(a)hotmail.com> wrote in message
news:1189763218.727152.36150(a)k79g2000hse.googlegroups.com
> HWND hNotepad;
> hNotepad = FindWindow(L"Notepad", 0);
>
> ** Now the compiler return errors :
>
> Error error LNK2028: unresolved token (0A00000C) "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(a)System@@@Z) AutomationWinForm.obj

Project | Properties | Linker | Input | Additional Dependencies. Add
user32.lib to the list.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


From: Kim Gräsman on
Hi SQACPP,

> I'm trying to understand an error when compiling a C++ form project :
>
> 1- Create a C++ win32 console application with the defaults parameters
> 3- Compile the console application
> -> It compile without any error
>
> 4- Now Create a C+ Form application with the default parameters when
> hNotepad = FindWindow(L"Notepad", 0);
> ** Now the compiler return errors :

The difference between the two is that the first is a native C++ project,
while the second is a managed C++ project.

The latter is a different language, formally called C++/CLI, that targets
the .NET platform rather than the native Win32 platform. C++/CLI allows you
to mix in native C++ pretty much at will, but using C++/CLI causes your app
to depend on the .NET framework.

The reason your C++/CLI project doesn't have user32.lib setup in the linker
input is that the .NET framework already provides similar services, and the
IDE assumes that you want to prefer those to the older, native ones.

So, either you can use the managed way of doing FindWindow, etc. (in which
case you might as well just switch to C#) or you can create a native UI project
(MFC, most likely) and have access to the full Win32 API, or you can mix;
..NET for the UI and native C++ for the back-end.

The last alternative is probably the one likely to cause you the most grief
with all the switching between native and managed code.

FWIW,
- Kim



From: SQACPP on
On Sep 14, 8:35 am, Kim Gräsman <k...(a)mvps.org> wrote:
> Hi SQACPP,
>
> > I'm trying to understand an error when compiling a C++ form project :
>
> > 1- Create a C++ win32 console application with the defaults parameters
> > 3- Compile the console application
> > -> It compile without any error
>
> > 4- Now Create a C+ Form application with the default parameters when
> > hNotepad = FindWindow(L"Notepad", 0);
> > ** Now the compiler return errors :
>
> The difference between the two is that the first is a native C++ project,
> while the second is a managed C++ project.
>
> The latter is a different language, formally called C++/CLI, that targets
> the .NET platform rather than the native Win32 platform. C++/CLI allows you
> to mix in native C++ pretty much at will, but using C++/CLI causes your app
> to depend on the .NET framework.
>
> The reason your C++/CLI project doesn't have user32.lib setup in the linker
> input is that the .NET framework already provides similar services, and the
> IDE assumes that you want to prefer those to the older, native ones.
>
> So, either you can use the managed way of doing FindWindow, etc. (in which
> case you might as well just switch to C#) or you can create a native UI project
> (MFC, most likely) and have access to the full Win32 API, or you can mix;
> .NET for the UI and native C++ for the back-end.
>
> The last alternative is probably the one likely to cause you the most grief
> with all the switching between native and managed code.
>
> FWIW,
> - Kim

Thanks!!!! :)