why doesnt this work on microsoft visual c++?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdafx.h>
#include <WinSock.h>
#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int counter=0;
    ofstream myfile;
    myfile.open("dam.txt");
    while(true)
    {
        if(GetAsyncKeyState(VK_ESCAPE))
        {
            counter++;
            myfile <<"Escape : " <<counter;
            cout<<"Button pressed !"<<endl;
           
		}

		if(GetAsyncKeyState(VK_RETURN))
            {
            myfile <<"." << counter;
            }

        Sleep(100);
    }
    return 0;
}


when i write this up in dev c++ is compiles but in mvc++ it dose not heres my error code

------ Build started: Project: keylog, Configuration: Debug Win32 ------
keylog.cpp
keylog.obj : error LNK2028: unresolved token (0A0003A0) "extern "C" short __stdcall GetAsyncKeyState(int)" (?GetAsyncKeyState@@$$J14YGFH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
keylog.obj : error LNK2019: unresolved external symbol "extern "C" short __stdcall GetAsyncKeyState(int)" (?GetAsyncKeyState@@$$J14YGFH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
C:\Users\jaffarali\Documents\Visual Studio 2010\Projects\keylog\Debug\keylog.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What is this, keylog day?!!!
keylog and = != == day I think!
The output you quote shows the compile phase succeeded, but that it then failed to link.

Are you linking to User32.lib? (where GetAsyncKeyState() lives)
msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx
i dont understand
All function you didn't personally write have to be linked to complete your prorgram. Calls like strcpy(), printf(), etc come from the C runtime (CRT). Visual C++ will be configured to link these when you create a new project. But if you use functonality not normally used in a console app, you need to edit the linker properties of you project to add the extra, required libraries.

Linker Property Pages
http://msdn.microsoft.com/en-us/library/024awkd1%28v=VS.90%29.aspx
(this is for the 2008 version: follow links to other versions if required)

You can provide the linker with two types of libraries:
- Static libraries contains all the machine code for the functions, so you final application includes the code for all the functions you use (though these functions can call functions in DLLs).
- Import libraries just provide the information about where the function comes from -- a DLL -- so when you use this kind of llibrary you need to provide the necessary DLLs when you distribute your app. (A static library provides the data needed for the linker to create entries in its import table, which is what the system uses to resolve dependencies when it load you app).

In the case of User32.lib, you are using an import library. But as User32.dll is a standard part of the operating system, you don't have to worry too much about it (you do have to worry the function you want to use has only been introduced in a more recebt version of Windows).

Andy
Last edited on
sounds complicated i think i will be writing this in dev c++...
All IDEs do the same thing: compile and then link.

Sounds like dev c++ is providing the libraries you need this time. But if you need other functionality one day, you will need to tell it about other libraries, too!
Topic archived. No new replies allowed.