Problem with Windows API

Started by
12 comments, last by admitche 20 years, 1 month ago
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?
Advertisement
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)
"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
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.
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.
quote:Original post by admitche
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.
Make sure the proper windows version preprocessor defines are used. IIRC, for KEYBDINPUT, _WIN32_WINNT should be greater than 0x0400.
^ 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.
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; 
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.
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.

This topic is closed to new replies.

Advertisement