#### Archived

This topic is now archived and is closed to further replies.

# Problem with Windows API

This topic is 5101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im having a problem compiling and using the windows API functions. This is using Visual Studio .NET Here is the smallest code example of my problem I can think of: #include <windows.h> KEYBDINPUT leftkey; With this code, when I try and compile I get the following errors: c:\WINDOWS\Desktop\Stuff\My Documents\Visual Studio Projects\aekite\main.cpp(4) : error C2146: syntax error : missing '';'' before identifier ''leftkey'' c:\WINDOWS\Desktop\Stuff\My Documents\Visual Studio Projects\aekite\main.cpp(4) : error C2501: ''KEYBDINPUT'' : missing storage-class or type specifiers c:\WINDOWS\Desktop\Stuff\My Documents\Visual Studio Projects\aekite\main.cpp(4) : error C2501: ''leftkey'' : missing storage-class or type specifiers I *believe* that I have linked the user32.lib file correctly to the project, but I am not sure. Here is what I did: Went into Project Properties, went to linker, general tab.. Then went to additional libraries to link at the bottom. There I added a path to the user32.lib directory. However, this should already be linked to b/c it is in the /lib directory for the compiler. One thing that is wierd... if I put the mouse over KEYBDINPUT in my code, it will come up with "typedef tagKEYBDINPUT KEYBDINPUT". This seems to make me think the compiler is recognizing the struct, which even further confuses me. Any help out there?

##### Share on other sites
It''s a compiler error, not a linker error: the compiler didn''t find any definition for KEYBDINPUT.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

##### Share on other sites
Okay, well how would I fix that?

I have also tried this already:

#include <windows.h>
#include <winuser.h>
KEYBDINPUT leftkey;

With the same errors.

The winuser.h file contains the following code in it:

typedef struct tagKEYBDINPUT {
WORD wVk;
WORD wScan;
DWORD dwFlags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KEYBDINPUT, *PKEYBDINPUT, FAR* LPKEYBDINPUT;

However, the windows.h file already contains a line:
#include <winuser.h>

So, how is the compiler not recognizing this then? Keep in mind what I said in the previous post where when I put the mouse over the KEYBDINPUT word, that it comes up with "typedef tagKEYBDINPUT KEYBDINPUT". So at least at that level it seems to be finding it.

##### Share on other sites
Here is some other info that might be relevant, but Im not sure.

The project is a "Windows 32 Project".

I clicked on Application Settings when creating the project, and made it a console application, and an empty project.

I dont see how that would effect this, but it is possible, so Im including this information.

##### Share on other sites
quote:
c:\WINDOWS\Desktop\Stuff\My Documents\Visual Studio Projects\aekite\main.cpp(4) : error C2501: ''KEYBDINPUT'' : missing storage-class or type specifiers
Check the lines above for any syntax errors. Check any of your own header files as well.

The line at which a compiler error is reported is not always where it occurs. It is simply where the input encountered turns to nonsense.

##### Share on other sites
Make sure the proper windows version preprocessor defines are used. IIRC, for KEYBDINPUT, _WIN32_WINNT should be greater than 0x0400.

##### Share on other sites
^ How do I set the IIRC, for KEYBDINPUT, _WIN32_WINNT values?

Im running windows XP, this is the first time ive done anything with the Windows API, so is there anything else I need to make sure I set in order to get this to work?

^^ Unfortunatly I am actually just trying to compile the two lines that I am giving in the example, so there are no other things that could be messing it up. But based on SiCrane''s info, I am beting there is a bunch of other lines I need to add in order to get this to work.

##### Share on other sites
IIRC is an abbreviation for "If I recall correctly." You don''t need to set that value in your program. All you need to do is set a preprocessor define for _WIN32_WINNT in your program. Either place in in your code above the headers or use a compiler option. e.g.: This code should compile:
#define _WIN32_WINNT 0x500#include <windows.h>KEYBDINPUT leftkey;

##### Share on other sites
Thanks! That seems to have gotten rid of the old problem, however now I am getting a new error. When I compile:

#define _WIN32_WINNT 0x0501
#include <windows.h>
KEYBDINPUT leftkey;

I get the following errors:

LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup
Debug/aekite.exe : fatal error LNK1120: 1 unresolved externals

This is during the linking stage of the compiling.
I tried many different values for _WIN32_WINNT as well, not just that one.

##### Share on other sites
You don''t have a complete program with just those lines. For a console project you need to define a function main() in order for the program to link.

##### Share on other sites
I beleive the issue is that the pre-compiled headers are missing from the project. As a Console App does not need WinUser and such include files, just stdio headers not even the libs.

Its complaining about not being able to find the external libraries not that it can find the definition for it. The symbol is missing from it....

##### Share on other sites
quote:
Original post by SiCrane
You don''t have a complete program with just those lines. For a console project you need to define a function main() in order for the program to link.

Not sure you understand windows API in the underlying code there is a winMainCTR function that is called when the program exits. However its not a window program rather a DOS. Which means that it can not find the exit functions.

The main function part is correct but thats an entry point after windows gets a thread/process ID. again that is done in WinMainStartupCRT, this is where it allocates the correct memory for globals. Take a better look at the underlying API structure every compiler will allow you to see this function call after you step past the last line of your code.

##### Share on other sites
quote:
Original post by Anonymous Poster
quote:
Original post by SiCrane
You don't have a complete program with just those lines. For a console project you need to define a function main() in order for the program to link.

Not sure you understand windows API in the underlying code ...

I don't think you were following the thread properly. He was compiling a source file with those three lines and only those three lines.

edit: for that matter, I think you're the one who does understand the API correctly. The function you list is not part of the Windows API, it's a design artifact based on the compiler's choice of entry point, specifically it refers to a function inside the C runtime implementation. Furthermore, it's not trying to find an exit function, it's trying to find the runtime library defined entry point. Also, the C runtime startup function doesn't allocate memory for globals, that's done by the loader when it loads the executable image into the virtual memory space.

[edited by - SiCrane on March 9, 2004 3:53:07 PM]

##### Share on other sites
Sorry for leaving this up here unposted, but I did figure it out.. Actually the only problem was that it wasn''t in a main function like someone said. I was just trying to keep it simple, guess I overdid it.

But, it is not a DOS program, it is a console windows program, so it does work with the Windows API.