Unable to open the device to send IOCTALS using createfile

Hi Guys,
I am unable to access my virtual com port driver using createfile function. It gives error as “the system cannot find the file specified”

What could be wrong?
I have tried all the paths, but it keeps giving the same error.

This is the definition in my driver

#define OBJECT_DIRECTORY L"\DosDevices\Global\"

The port number i am accessing is COM24.

The code i am using to access is ,
HANDLE hFile;
DWORD dwReturn;
char szTemp[256] = { 0 };
wchar_t buf[256];
hFile = CreateFile(“COM24”, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
printf(“%s”,buf);

After this the function the buf contains ,
“The system cannot find the file specified”

I have tried,

\\.\COM24
\\.\DosDevices\Global\COM24
\DosDevices\Global\COM24
\Devices\COM24
\\.\Devices\COM24
All possible combinations that came into my mind
The WinObj tool shows that port as COM24 with a shortcut ICON. When i select it the status bar shows \GLOBAL??\COM24

The properties of COM24 shows
Name:COM24
Type:SymbolicLink
References:2
Handles: 0

Link: \Device\0000004b

I have tried the path as,
\\.\Device\0000004b
\Device\0000004b
Device\0000004b

also.

Please help …

try “\.\COM24”

On Tue, Jan 6, 2015 at 1:38 PM, wrote:

> Hi Guys,
> I am unable to access my virtual com port driver using createfile
> function. It gives error as “the system cannot find the file specified”
>
> What could be wrong?
> I have tried all the paths, but it keeps giving the same error.
>
> This is the definition in my driver
>
> #define OBJECT_DIRECTORY L"\DosDevices\Global\“
>
> The port number i am accessing is COM24.
>
> The code i am using to access is ,
> HANDLE hFile;
> DWORD dwReturn;
> char szTemp[256] = { 0 };
> wchar_t buf[256];
> hFile = CreateFile(“COM24”, GENERIC_READ | GENERIC_WRITE, 0, NULL,
> OPEN_EXISTING, 0, NULL);
>
> FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
> MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
> printf(”%s",buf);
>
>
> After this the function the buf contains ,
> “The system cannot find the file specified”
>
> I have tried,
>
> \\.\COM24
> \\.\DosDevices\Global\COM24
> \DosDevices\Global\COM24
> \Devices\COM24
> \\.\Devices\COM24
> All possible combinations that came into my mind
> The WinObj tool shows that port as COM24 with a shortcut ICON. When i
> select it the status bar shows \GLOBAL??\COM24
>
> The properties of COM24 shows
> Name:COM24
> Type:SymbolicLink
> References:2
> Handles: 0
>
> Link: \Device\0000004b
>
> I have tried the path as,
> \\.\Device\0000004b
> \Device\0000004b
> Device\0000004b
>
> also.
>
> Please help …
>
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>

xxxxx@gmail.com wrote:

I am unable to access my virtual com port driver using createfile function. It gives error as “the system cannot find the file specified”

The code i am using to access is ,
HANDLE hFile;
DWORD dwReturn;
char szTemp[256] = { 0 };
wchar_t buf[256];
hFile = CreateFile(“COM24”, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
printf(“%s”,buf);

You cannot call GetLastError unless you know that an API has failed.
So, you should do:

hFile = CreateFile( “\\.\COM24”, … );
if( hFile == INVALID_HANDLE_VALUE )
{
FormatMessage(… );
puts( buf );
}


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

umar haris Shaikh wrote:

try “\.\COM24”

That would be the correct name, but you have to write it in C as
“\\.\COM24”.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Hi Guys,
Thank you for the replies.

It didnt work for “\.\COM24” or “\\.\COM24”

The getLastError returned, The system cannot find the file specified.
But it did work for L"\\.\COM24

hFile = CreateFile(L"\\.\COM24", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

I don’t know why adding L typecast worked. and getLastError returned operation completed succesfully.

Thank you very much. :slight_smile:

Are you compiling as ANSI or UNICODE? You can use ANSI strings if you are compiling for UNICODE, so much so that using an ANSI string when the API expects a UNICODE string should not even compile.

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Tuesday, January 6, 2015 11:11 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Unable to open the device to send IOCTALS using createfile

Hi Guys,
Thank you for the replies.

It didnt work for “\.\COM24” or “\\.\COM24”

The getLastError returned, The system cannot find the file specified.
But it did work for L"\\.\COM24

hFile = CreateFile(L"\\.\COM24", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

I don’t know why adding L typecast worked. and getLastError returned operation completed succesfully.

Thank you very much. :slight_smile:


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

I am using Visual studio 2013 to compile. UNICODE is enabled somewhere. i can see it in

#ifdef UNICODE
#define CreateFile CreateFileW
#else
#define CreateFile CreateFileA
#endif // !UNICODE

That is standard across all windows headers. That snippet doesn’t mean UNICODE is defined, rather if UNICODE is deifned, use CreateFileW, otherwise use the A version. UNICODE would be defined in your project

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Tuesday, January 06, 2015 11:25 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Unable to open the device to send IOCTALS using createfile

I am using Visual studio 2013 to compile. UNICODE is enabled somewhere. i can see it in

#ifdef UNICODE
#define CreateFile CreateFileW
#else
#define CreateFile CreateFileA
#endif // !UNICODE


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

On Jan 6, 2015, at 11:11 PM, xxxxx@gmail.com wrote:

It didnt work for “\.\COM24” or “\\.\COM24”

The getLastError returned, The system cannot find the file specified.
But it did work for L"\\.\COM24

hFile = CreateFile(L"\\.\COM24", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

I don’t know why adding L typecast worked. and getLastError returned operation completed succesfully.

The L is not a typecast. I don’t understand how you can be programming drivers without an understanding of C syntax and character sets. The L modifier on a string constant says the string is to be defined using “large” (or “wide”) characters, where the definition of “large" is left to the compiler. In the WIndows case, wide characters (wchar_t) are 16-bit Unicode (UTF-16). As Doron suggested, that probably means you were compiling a Unicode project, in which case you should have received a compiler warning. Hence the danger of ignoring compiler warnings.

And, to repeat my earlier warning, you CANNOT depend on GetLastError returning 0 when there is no error. That’s not part of the contract. No one promises that the last error will ever be reset to 0. GetLastError will only return something reasonable when an API has actually encountered an error. All Windows APIs will return a signal that says “there was an error”; only then can you go find out what the error was.

Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

I am sorry Tim, Thank you for correcting my mistake. I understand now. I am into windows driver development from 15 days. I have been doing microcontroller programming, so i don’t know much about the character sets. I will bear in mind about the getLastError caution. I am thankful for Doron and you for your guidance.