I'm trying to build example application from here. In Visual Studio I created a Win32 C++ console application. when I try to compile I get a lot of errors about undeclared identifiers. The code as mentioned in the link is:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include "stdafx.h"
void DisplayError(LPTSTR lpszFunction)
// Routine Description:
// Retrieve and output the system error message for the last-error code
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL);
lpDisplayBuf =
(LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)
+ lstrlen((LPCTSTR)lpszFunction)
+ 40) // account for format string
* sizeof(TCHAR));
if (FAILED(StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error code %d as follows:\n%s"),
lpszFunction,
dw,
lpMsgBuf)))
{
printf("FATAL ERROR: Unable to output error code.\n");
}
_tprintf(TEXT("ERROR: %s\n"), (LPCTSTR)lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
int main(int argc, TCHAR *argv[])
{
HANDLE hFile;
char DataBuffer[] = "This is some test data to write to the file.";
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
DWORD dwBytesWritten = 0;
BOOL bErrorFlag = FALSE;
printf("\n");
if (argc != 2)
{
printf("Usage Error:\tIncorrect number of arguments\n\n");
_tprintf(TEXT("%s <file_name>\n"), argv[0]);
return;
}
hFile = CreateFile(argv[1], // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_NEW, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
DisplayError(TEXT("CreateFile"));
_tprintf(TEXT("Terminal failure: Unable to open file \"%s\" for write.\n"), argv[1]);
return;
}
_tprintf(TEXT("Writing %d bytes to %s.\n"), dwBytesToWrite, argv[1]);
bErrorFlag = WriteFile(
hFile, // open file handle
DataBuffer, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL); // no overlapped structure
if (FALSE == bErrorFlag)
{
DisplayError(TEXT("WriteFile"));
printf("Terminal failure: Unable to write to file.\n");
}
else
{
if (dwBytesWritten != dwBytesToWrite)
{
// This is an error because a synchronous write that results in
// success (WriteFile returns TRUE) should write all data as
// requested. This would not necessarily be the case for
// asynchronous writes.
printf("Error: dwBytesWritten != dwBytesToWrite\n");
}
else
{
_tprintf(TEXT("Wrote %d bytes to %s successfully.\n"), dwBytesWritten, argv[1]);
}
}
CloseHandle(hFile);
}
but when I try to build it I get:
1>------ Build started: Project: trainer, Configuration: Debug x64 ------
1>Build started 5/7/2021 1:46:02 AM.
1> 1>
1>InitializeBuildStatus:
1> Touching "x64\Debug\trainer.tlog\unsuccessfulbuild".
1> ClCompile:
1> All outputs are up-to-date.
1> trainer.cpp
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(7): error C2065: 'LPTSTR': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(7): error C2146: syntax error: missing ')' before identifier 'lpszFunction'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(10): error C2143: syntax error: missing ';' before '{'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(10): error C2447: '{': missing function header (old-style formal list?)
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(51): error C2065: 'HANDLE': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(51): error C2146: syntax error: missing ';' before identifier 'hFile'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(51): error C2065: 'hFile': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(53): error C2065: 'DWORD': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(53): error C2146: syntax error: missing ';' before identifier 'dwBytesToWrite'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(53): error C2065: 'dwBytesToWrite': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(53): error C2146: syntax error: missing ';' before identifier 'strlen'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(53): error C3861: 'strlen': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(54): error C2065: 'DWORD': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(54): error C2146: syntax error: missing ';' before identifier 'dwBytesWritten'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(54): error C2065: 'dwBytesWritten': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(55): error C2065: 'BOOL': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(55): error C2146: syntax error: missing ';' before identifier 'bErrorFlag'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(55): error C2065: 'bErrorFlag': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(55): error C2065: 'FALSE': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(61): error C3861: 'TEXT': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(62): error C2561: 'main': function must return a value
1> d:\moki\projects\trainer\trainer\trainer.cpp(49): note: see declaration of 'main'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(65): error C2065: 'hFile': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(66): error C2065: 'GENERIC_WRITE': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(69): error C2065: 'CREATE_NEW': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(70): error C2065: 'FILE_ATTRIBUTE_NORMAL': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(65): error C3861: 'CreateFile': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(73): error C2065: 'hFile': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(73): error C2065: 'INVALID_HANDLE_VALUE': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(75): error C3861: 'TEXT': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(75): error C3861: 'DisplayError': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(76): error C3861: 'TEXT': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(77): error C2561: 'main': function must return a value
1> d:\moki\projects\trainer\trainer\trainer.cpp(49): note: see declaration of 'main'
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(80): error C3861: 'TEXT': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(80): error C2065: 'dwBytesToWrite': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(82): error C2065: 'bErrorFlag': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(83): error C2065: 'hFile': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(85): error C2065: 'dwBytesToWrite': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(86): error C2065: 'dwBytesWritten': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(82): error C3861: 'WriteFile': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(89): error C2065: 'FALSE': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(89): error C2065: 'bErrorFlag': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(91): error C3861: 'TEXT': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(91): error C3861: 'DisplayError': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(96): error C2065: 'dwBytesWritten': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(96): error C2065: 'dwBytesToWrite': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(106): error C3861: 'TEXT': identifier not found
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(106): error C2065: 'dwBytesWritten': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(110): error C2065: 'hFile': undeclared identifier
1> 1>
1>d:\moki\projects\trainer\trainer\trainer.cpp(110): error C3861: 'CloseHandle': identifier not found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.20
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I am new to Visual Studio and Windows API. Is there something I need to do in the configuration / settings that I am unaware?