I'm trying to compile the following program (file named test.cpp):

#include "stdafx.h"

int main(int argc, char* argv[]){
INPUT *buffer = new INPUT[1]; //allocate a buffer
buffer->type = INPUT_MOUSE;
buffer->mi.dx = 100;
buffer->mi.dy = 100;
buffer->mi.mouseData = XBUTTON1;
buffer->mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
buffer->mi.time = 0;
buffer->mi.dwExtraInfo = 0;
SendInput(1,buffer,sizeof(INPUT));
delete (buffer); //clean up our messes.
}

when "stdafx.h" is:

#pragma once

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>
#include <windows.h>

Then I get the following errors:

e:\test\src\test.cpp(4) : error C2065: 'INPUT' : undeclared identifier
e:\test\src\test.cpp(4) : error C2065: 'buffer' : undeclared identifier
e:\test\src\test.cpp(4) : error C2061: syntax error : identifier 'INPUT'
e:\test\src\test.cpp(5) : error C2227: left of '->type' must point to class/struct/union type is ''unknown-type''
e:\test\src\test.cpp(5) : error C2065: 'INPUT_MOUSE' : undeclared identifier
e:\test\src\test.cpp(5) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(6) : error C2227: left of '->mi' must point to class/struct/union type is ''unknown-type''
e:\test\src\test.cpp(6) : error C2228: left of '.dx' must have class/struct/union type
e:\test\src\test.cpp(6) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(7) : error C2227: left of '->mi' must point to class/struct/union type is ''unknown-type''
e:\test\src\test.cpp(7) : error C2228: left of '.dy' must have class/struct/union type
e:\test\src\test.cpp(7) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(8) : error C2227: left of '->mi' must point to class/struct/union type is ''unknown-type''
e:\test\src\test.cpp(8) : error C2228: left of '.mouseData' must have class/struct/union type
e:\test\src\test.cpp(8) : error C2065: 'XBUTTON1' : undeclared identifier
e:\test\src\test.cpp(8) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(9) : error C2227: left of '->mi' must point to class/struct/union type is ''unknown-type''
e:\test\src\test.cpp(9) : error C2228: left of '.dwFlags' must have class/struct/union type
e:\test\src\test.cpp(9) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(10) : error C2227: left of '->mi' must point to class/struct/union type is ''unknown-type''
e:\test\src\test.cpp(10) : error C2228: left of '.time' must have class/struct/union type
e:\test\src\test.cpp(10) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(11) : error C2227: left of '->mi' must point to class/struct/union type is ''unknown-type''
e:\test\src\test.cpp(11) : error C2228: left of '.dwExtraInfo' must have class/struct/union type
e:\test\src\test.cpp(11) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(12) : error C2070: ''unknown-type'': illegal sizeof operand
e:\test\src\test.cpp(12) : error C3861: 'SendInput': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(12) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(12) : error C3861: 'INPUT': identifier not found, even with argument-dependent lookup
e:\test\src\test.cpp(13) : error C2541: 'delete' : cannot delete objects that are not pointers
e:\test\src\test.cpp(13) : error C3861: 'buffer': identifier not found, even with argument-dependent lookup

I'm using everyting as specified in MSDN, but still got this stupid compiler error. Have no idea why... :(
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputFunctions/SendInput.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputStructures/INPUT.asp

Obviously what I want is this code to compile and run :)

PS
When I compile it with only <winuser.h> (instead #include "stdafx.h" I write #include <winuser.h>), I get the following errors:

c:\...\include\winuser.h(39) : error C2146: syntax error : missing ';' before identifier 'HDWP'
c:\...\include\winuser.h(39) : fatal error C1004: unexpected end of file found

I've tried to compile both on VC++ 6.0 and .net and both produce the same errors!!!

maybe you should try tinkering with the compiler options

or try including both winuser.h and stdafx.h

I am having similar problems. Let us know if you find a solution. Thanks

Member Avatar for bobd

I took a quick look at the code then tried to compile under VC7.1. It failed there too. I right clicked in the IDE to find the definition of INPUT which took me to WINUSER.H. Everything looked ok right there, but the looking up in the file I found that the definition of INPUT was guarded by a

#if (_WIN32_WINNT > 0x0400)

This _WIN32_WINNT define appears to limit access to specific functionality (including INPUT) to certain versions of windows. I looked to see where _WIN32_WINNT defined but could not find it quickly, however it was being tested with WINVER first thing in windows.h. It looks like it must be defined prior to including windows.h

I was able to cleanly compile by adding

#define _WIN32_WINNT 0x0501

as the first line in stdafx.h.

have fun
-bobd-

Hey guys now be careful not to modify your actual headers it's not safe.Bodb, nice of you to check it out but that kinda protection is usally related to memory modes or some sort of diffrence in the way the compiled program inteacts with the system or is compatable with.Now in this particular case it may complie well but I would still recommend you change the options on your complier when you are compliing your program.That way you will be able to avoid any errors during runtime due to illegally structured programs.

Member Avatar for bobd

A little further searching for _WIN32_WINNT in the VS7 help index brings the following.

[Begin help file text ]
Certain functions that depend on a particular version of Windows are declared using conditional code. This enables you to use the compiler to detect whether your application uses functions that are not supported on its target version(s) of Windows. To compile an application that uses these functions, you must define the appropriate macros. Otherwise, you will receive the C2065 error message. The following table indicates the macros you must define to target each system.

Minimum system required Macros to define
Windows Server 2003 family _WIN32_WINNT>=0x0502
Windows XP _WIN32_WINNT>=0x0501
Windows 2000 _WIN32_WINNT>=0x0500
Windows NT 4.0 _WIN32_WINNT>=0x0400
Windows Me _WIN32_WINDOWS=0x0490
Windows 98 _WIN32_WINDOWS>=0x0410
Internet Explorer 6.0 _WIN32_IE>=0x0600
Internet Explorer 5.01, 5.5 _WIN32_IE>=0x0501
Internet Explorer 5.0, 5.0a, 5.0b _WIN32_IE>=0x0500
Internet Explorer 4.01 _WIN32_IE>=0x0401
Internet Explorer 4.0 _WIN32_IE>=0x0400
Internet Explorer 3.0, 3.01, 3.02 _WIN32_IE>=0x0300


For example, to use the features specifically marked for Windows 2000 in the header files, you need to explicitly define _WIN32_WINNT as 0x0500 or greater. You can define the symbols using the #define statement in each source file, or by specifying the /D_WIN32_WINNT=0x0500 compiler option supported by Visual C++.

Visual C++ 6.0: To specify compiler options, go to the Projects menu and click Settings, then select the C/C++ tab.

Visual C++ 7.0: To specify compiler options, go to the Projects menu and click Properties.

[End help file text]

So, it looks like the INPUT structure was added to Windows after NT4. According to the SDK help, to get access to the "new" structures, you must either add a /D_WIN32_WINNT=0x0500 (or anything greater than 0x0400) to the compile options or add the define to one of your header files before you include windows.h.

I prefer putting project options of this nature in the source as it makes the OS dependency visible, browsable, and understandable (presuming an appropriate comment is included.) Since the IDE generates stdafx.h as the "general header" for all files in a project, I believe it is safe and reasonable to put the define for OS platform there.

I totally agree that windows.h or any headers not specific to a project should not be modified by a single project.

-bobd-

how I can find the reason for error2065¨undeclared identifier¨ in a cpp file of c++in visualstudio.
thanks

Member Avatar for bobd

Usually the compiler error will say what identifier is undeclared and indicate where it is first used.
If you have not declared it earlier (e.g int i) then you'll need to add the appropriate declaration prior to that use. In C++, you can frequently declare it at the first use too ( e.g. for( int i=0;i<10;++i){} )

winuser.h

CAN ANYBODY TELL SOLUTION FOR THIS

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.