473,893 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CreateFile Windows COM1

73 New Member
Trying to open COM1 in XP using CreateFile and I keep getting INVALID_HANDLE_ VALUE.

here is the code I am using:

Expand|Select|Wrap|Line Numbers
  1. HANDLE serialhCom = CreateFile("COM1",            
  2.                        GENERIC_READ | GENERIC_WRITE,
  3.                        0,                    
  4.                        NULL,                 
  5.                        OPEN_EXISTING,        
  6.                        FILE_FLAG_OVERLAPPED, 
  7.                        NULL);                
  10.      if (serialhCom != INVALID_HANDLE_VALUE) 
  11.      {
  12.        SetupComm(serialhCom, inLen, outLen);
  13.        serialSetTimeouts(serialhCom, MAXDWORD, 0, 0, 0, 0);
  14.      }
  15.      else
  16.      {
  17.        printf ("Could not open COM1\n");
  18.      }
I used GetLastError to get more details. and I received error # 123?

I have searched Google and looked at MSDN and could not info to help me solve the problem. There are dozens of articles explaining CreateFile and how one should use it, but no real substance. I have checked my device manager and my COM1 is active with no conflicts. I have checked my registry and have confirmed my COM1 in memory.

Please advise.

Nov 3 '08 #1
11 29624
2,426 Recognized Expert Top Contributor
Try adding a colon to the filename: "COM1:".
Nov 3 '08 #2
73 New Member
no good. I have even tried (recommended by some) ""////.//COM1" :(
Nov 3 '08 #3
9,065 Recognized Expert Moderator Expert
Error 123... you can look up error code in winerror.h where you would find

// MessageText:
// The filename, directory name, or volume label syntax
// is incorrect.
#define ERROR_INVALID_N AME 123L // dderror
You may have tried "////.//COM1" but did you try it with the correct slash "\\\\.\\COM 1"?

Actually I have to say that "COM1" has always worked for me I assume you computer has a COM1?
Nov 3 '08 #4
9,065 Recognized Expert Moderator Expert
Oh and 1 final thought, COM1 isn't already in use by some other program is it? Only one process can have access to the COM port at a time (in fact I would question if you can have more than 1 handle open to the COM port).
Nov 3 '08 #5
73 New Member

I tried the following:

1. char str[8];
//port was passed in through the function parameter
sprintf(str, "COM%d", port);

// "str" was used as the first parameter in CreateFile
2. "COM1"
3. "com1"
4. "COM1:"
5. "\\\\.\\COM 1"

and finally....

6. "TEXT("COM1 ") which is the one that finally went through and did not give me an "INVALID_HANDLE _VALUE".

All of these methods (2,3,4,5) were found on google. The first method was working on Borland C and did not have any problems. I moved the same file which compiled on my other machine to the new computer that has Visual Studio 2008 and that's when the problem started.

I havent tried to see if my program spits anything out of the serial port yet, but at least I am passing through the section which was failing before.

I do have COM1 and no other program uses it. I have tried COM1 on windows Terminal software and it does communicate with my other computer.

Nov 3 '08 #6
73 New Member
just for kicks:

I tried to open COM1 with 2 different handles and the error code for that is "5"

Nov 3 '08 #7
9,065 Recognized Expert Moderator Expert
and finally....

6. "TEXT("COM1 ")
Damn, sorry yes that would be required to turn it into a mu;tibyte string which is now the default for windows.
Nov 3 '08 #8
73 New Member
I am not sure what that is? (multibyte)

Oh and by the way, it works. I am getting data out of COM1.

My question now is, how do I make this dynamic. Can I pass a var to TEXT()?
It doesn't look like I can because it takes in a "quote"

How can I do this?

Nov 4 '08 #9
2 New Member
I have the same question:

Does anyone know how to generate the output of TEXT("COM1") in a dynamic way?

Mar 18 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Chuck Rittersdorf | last post by:
Hi There I am having a problem using the win32 API from VB6. I am trying to send a command string to a printer(zebra TLP 2742) on LPT1 using the folowing API functions CreateFile and WriteFile I have written C code which works fine, just the VB translation/port
by: Glen | last post by:
Hello. Can some one explain me why below example work LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect;
by: Antoine | last post by:
Hello, I'm writing a program to send requests to my wlan pocket pc device (UIO1: driver) in C#. Here how I import CreateFile functions from coredll.dll with DllImport: public static extern IntPtr CreateFile( string lpFileName, uint dwDesiredAccess,
by: Steve N. | last post by:
I'm having trouble getting a handle returned from the following code within a managed class: HANDLE m_hSerialComm = CreateFile("\\\\.\\COM1\0\0\0", GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, 0); It works fine in non-managed code but I get a '{void}' returned when run within managed code. I'm wondering if that's actually the problem or if there is something else I'm missing... If it is the problem, then...
by: JohnnyBoy | last post by:
I am playing with a Microsoft code example that uses the creatFile call to open a stream to a serial COM port. When I execute the example, the createFile call fails with an error code 5 (ERROR_ACCESS_DENIED) as if the COM port is inaccessible from the program. I am including a code snippit with the createFile call in hopes that someone out there has seen this before and can help. abytes is a string "COM1", and imode is set to the overlapped...
by: Lee | last post by:
I am getting this error trying to run a win forms .net app using the .net 2.0 serial port control. This occurs when the app tries to open the port. I knwo this is some type of security access problem but I have no clue where to fix it. Help....
by: mastermagrath | last post by:
Hi, Sorry for the possibly silly question. Am i right in saying that the C library i use on a windows machine really utilises the windows API for all its operations e.g. opening files, printing to the console etc.etc. Thanks in advance
by: =?Utf-8?B?R2FyeQ==?= | last post by:
Been struggling with the code below for the last couple of days: m_hSerialHandle = CreateFile("COM1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, /*FILE_FLAG_OVERLAPPED*/0, NULL); With VC++6.0, this executes fine and I am able to establish a connection w/ a device via COM1 however I recently installed VC++8 (.net 2005) and this code no longer works. It last error to ERROR_INVALID_NAME. Any recommendations will be greatly...
by: DotNetDanny | last post by:
Hello Machine: Windows Vista Business, standalone machine (no domain). Installed an old classic ASP webapplication in IIS7, running under a new app.pool with 'NETWORK SERVICE' account (using existing app.pool gives same results). This webapplication tries to write to a log file. Used Process Monitor (from Sysinternals) for monitoring purposes. Logged in to my machine as 'MYPC\danny', a local account that's a member of the administrators...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.