When you get an error from a WinAPI function, always check GetLastError to see what went wrong:
Code:
#include <windows.h>
const char* ErrorMessage(DWORD error)
{
static char buffer[255];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, 0, buffer, 255, 0);
return buffer;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HANDLE hFile = CreateFile("c:\\myfile.txt", GENERIC_READ,
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile == INVALID_HANDLE_VALUE)
{
// Something happened - find out what
MessageBox(0, ErrorMessage(GetLastError()), 0, MB_OK);
}
else
{
MessageBox(0, "File opened successfully.", "Success", MB_OK);
CloseHandle(hFile);
}
return 0;
}