Guide to Win32 Memory DC. The HDC is a handle to a Device Context similar to the HWND is a handle to a window. Let's draw a parallel between the two: Window: An abstraction to represent something that can receive messages from the system, may have a size and location on the screen, may contain other windows, has behaviors and so on.
In C++, I have opened a serial port that has a HANDLE
. Since the port may close by an external application, how can I verify that the HANDLE
is still valid before reading data?
I think it can be done by checking the HANDLE
against a suitable API function, but which?Thank you.
9 Answers
Checking to see whether a handle is 'valid' is a mistake. You need to have a better way of dealing with this.
The problem is that once a handle has been closed, the same handle value can be generated by a new open of something different, and your test might say the handle is valid, but you are not operating on the file you think you are.
For example, consider this sequence:
- Handle is opened, actual value is 0x1234
- Handle is used and the value is passed around
- Handle is closed.
- Some other part of the program opens a file, gets handle value 0x1234
- The original handle value is 'checked for validity', and passes.
- The handle is used, operating on the wrong file.
So, if it is your process, you need to keep track of which handles are valid and which ones are not. If you got the handle from some other process, it will have been put into your process using DuplicateHandle(). In that case, you should manage the lifetime of the handle and the source process shouldn't do that for you. If your handles are being closed from another process, I assume that you are the one doing that, and you need to deal with the book keeping.
janmjanmSome WinAPI functions return meaningless ERROR_INVALID_PARAMETER even if valid handles are passed to them, so there is a real use case to check handles for validity.
GetHandleInformation function does the job:http://msdn.microsoft.com/en-us/library/ms724329%28v=vs.85%29.aspx
as the port may close by a external application
This is not possible, an external application cannot obtain the proper handle value to pass to CloseHandle(). Once you have the port opened, any other process trying to get a handle to the port will get AccessDenied.
That said, there's crapware out there that hacks around this restriction by having secret knowledge of the undocumented kernel structures that stores handles for a process. You are powerless against them, don't make the mistake of taking on this battle by doing the same. You will lose. If a customer complains about this then give them my doctor's advice: 'if it hurts then don't do it'.
Hans PassantHans Passant![Handle Handle](https://aloiskraus.files.wordpress.com/2016/06/image_thumb2.png?w=1152&h=343)
Probably you are under windows and using ReadFile
to read the data. The only way to check it is trying to read. If the HANDLE
is invalid it'll return an error code (use GetLastEror() to see which one it is) which will probably be ERROR_HANDLE_INVALID
.
If you are given a HANDLE
and simply want to find out whether it is indeed an open file handle, there is the Windows API function GetFileInformationByHandle for that.
Depending on the permissions your handle grants you for the file, you can also try to move the file pointer using SetFilePointer, read some data from it using ReadFile, or perform a null write operation using WriteFile with nNumberOfBytesToWrite
set to 0.
Try using the IsWindow() function ;-)
IsWindow() is a function of the Windows API since Windows 2000.
I know that it's a little bit late but I had a similar question to you, how to check if a pipe (a pipe I created using CreateFile) is still open (maybe the other end shut down the connection) and can read, and if it is not, to open it again. I did what @Felix Dombek suggested, and I used the WriteFile to check the connection. If it returned 1 it means the pipe is open, else I opened it using the CreateFile again. This implies that your pipe is duplex. Here's the CreateFile:hPipe2 = CreateFile(lpszPipename2, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
and here is how I checked for the connection:
This is working just fine for me :)
HimanshuKonuyla ilgili örnek `
by http://www.tenouk.com/cpluscodesnippet/createfilegetclosehandleinfo.html
In order to check the handle , first we need to know what is our HANDLE for, (for a File/Port/Window, ...), Then find an appropriate function to check it (thanks @janm for help). Note that the function's duty may be specially for this destination or not. In my case that iv'e opened a Serial port by CreateFile() , i can check the COM status by GetCommState() API function that fills our COM info struct. If the port is not open anymore or inaccessible the function returns 0 and if you call GetLastError() immediately, you`ll get the ERROR_INVALID_HANDLE value. Thanks everyone for helps.
losingsleeeplosingsleeepNot the answer you're looking for? Browse other questions tagged c++cwinapiporthandle or ask your own question.
I am trying to create an OpenGL application on windows. As far as I can understand, one of the first things I must acquire is a Device Context, which must be passed on to a couple of functions that choose and set a pixel format and create a rendering context. I used the OpenGL wiki to get a rough idea about what I should do.My code is something like:
This prints
I assumed a Device Context refers to a physical device, but I read somewhere that it refers to any drawable 'surface'. In both cases is my question: how can I obtain a non-null DC? Or should I perform a completely different set of steps in order to set up this whole OpenGL system?
I found a lot of tutorials online, but they all use GLUT, GLEW, GLFW, X11, SDL etc. which are libraries. Libraries make certain things easier, but they usually do not perform tasks that are impossible without using them. This time, I want to try to do things the hard way and therefore use no libraries, just plain OpenGL.
I found, at last, a tutorial that only used the windows libraries for creating a window.
1 Answer
You did not state your OS but I assume Windows from the function names. The problem is exactly as Reto Koradi stated in the comment. To set up OpenGL you need to do this:
Obtain OS handle to object with valid device context
It can be OS window or OS bitmap. If you have just console app then you need to create a valid OS window first and use its handle (to my knowledge console does not have Canvas).
you can use GLUT for the window creation or If your compiler IDE has an window App you can use that. You can also combine OpenGL and Window components. VCL is also not a problem (I am using it for years with OpenGL)
In windows you can use CreateWindowEx so google an example for it...
Anyway you should have your handle in a variable like:
If you have no experience with windows applications then use GLUT for this. Otherwise you would need to learn a lot of stuff just to cover window creation, message handling of events and user/app interaction which can be really overwhelming for a rookie without guide.
Get Device context for that handle
Set pixel format you need of device context
Create OpenGL rendering context for device context
Set it as default OpenGL context
This is absolute minimum without any error checking , additional buffers etc. For more info and actual code see related QA's:
- How to render an openGL frame in C++ builder? for oldstyle GL
- simple complete GL+VAO/VBO+GLSL+shaders example in C++ with the new stuff
You can use GLUT for all of this. This is first hit I found by quick search:
Or follow OpenGL tutorials there are tons of them out there ...