Hi,
Using the C code outlined below (extract from a larger program) in Visual Studio 2110 express, I am having difficulty using CreateFile to return a valid pointer. It continues to return 0xfffff (the Invalid_Handle_Value).
I have followed numerous guides on the internet to open COM ports in C in a Windows environment and have followed the syntax exactly, but with no success. Any suggestions why I can only get an invalid pointer would be appreciated.
Thanks in advance,
Mr. Whippy.
(sorry about the formatting - first post - see attached file)
temp1.c
Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<conio.h>
HANDLE hSerialIn = INVALID_HANDLE_VALUE;
DCB dcbSerialParams = { 0 };
DWORD WINAPI GetLastError(void);
LPCWSTR lpFileName;
long lLastError = ERROR_SUCCESS;
char portIn[16];
int main(void)
{
sprintf(portIn, "COM1");
lpFileName = (LPCWSTR) portIn;
printf("lpFileName: %s\n", lpFileName);
hSerialIn =
CreateFile(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hSerialIn == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
printf("\nError: \nThe system cannot find the file specified (%s)\n", portIn); //error code 0x02
}
else if(GetLastError() == ERROR_INVALID_NAME) {
printf("\nError: \n%s 'filename, directory name, or volume label syntax is incorrect'\n", portIn); //error code 0x7B
}
else
{
printf("\nHandle creation error code: %x\n", GetLastError());
}
puts("\t...CreateFile returned an invalid handle value");
}
if (!GetCommState(hSerialIn, &dcbSerialParams)) {
printf("\nError: \ncould not get %s state!\n", portIn);
}
CloseHandle(hSerialIn);
puts("\npress any key to exit...");
getchar();
return 1;
}