What is handle linux

What is a handle in C++?

I have been told that a handle is sort of a pointer, but not, and that it allows you to keep a reference to an object, rather than the object itself. What is a more elaborate explanation?

Look into the Chain of Responsibility pattern, you’ll learn that a «Handle» is a basically a node, and that a «Handler» is a small set of them. The «magic» comes from recursion

8 Answers 8

A handle can be anything from an integer index to a pointer to a resource in kernel space. The idea is that they provide an abstraction of a resource, so you don’t need to know much about the resource itself to use it.

For instance, the HWND in the Win32 API is a handle for a Window. By itself it’s useless: you can’t glean any information from it. But pass it to the right API functions, and you can perform a wealth of different tricks with it. Internally you can think of the HWND as just an index into the GUI’s table of windows (which may not necessarily be how it’s implemented, but it makes the magic make sense).

EDIT: Not 100% certain what specifically you were asking in your question. This is mainly talking about pure C/C++.

A Handle can be useful for saving states (among others). If u have data in a structure like an std::vector. Your object may be at different memory locations at different times during execution of a program, which means your pointer to that memory will change values. With a handle it never changes, it always references your object. Imagine saving a state of a program (like in a game) — you wouldn’t save out a pointer location to data and later import the data again and try to get that address in memory. You can however save out a Handle with your data, and import the data and handle.

Is it possible to convert a HANDLE into an equivalent in Linux? I have to migrate a program that uses HANDLE from Windows to Linux.

That is the correct answer, that they can be anything, and that the code that uses them defines the type of the handle. I tried to make a more concise version of my own similar answer, couldn’t help myself, for posterity. @CornelVerster — They are the same in linux. I mean, not OS handles, but the concept. So, it depends on the handle as to its migration, or even need to migrate.

@Matthew Iselin: in any API documentation, do they define that thing is a handler then we should know to pass them to functions, otherwise how we can know what is a handler in API documentation

A handle is a pointer or index with no visible type attached to it. Usually you see something like:

 typedef void* HANDLE; HANDLE myHandleToSomething = CreateSomething(); 

So in your code you just pass HANDLE around as an opaque value.

Читайте также:  Linux name resolution failed

In the code that uses the object, it casts the pointer to a real structure type and uses it:

 int doSomething(HANDLE s, int a, int b) < Something* something = reinterpret_cast(s); return something->doit(a, b); > 

Or it uses it as an index to an array/vector:

 int doSomething(HANDLE s, int a, int b) < int index = (int)s; try < Something& something = vecSomething[index]; return something.doit(a, b); >catch (boundscheck& e) < throw SomethingException(INVALID_HANDLE); >> 

A handle is a sort of pointer in that it is typically a way of referencing some entity.

It would be more accurate to say that a pointer is one type of handle, but not all handles are pointers.

For example, a handle may also be some index into an in memory table, which corresponds to an entry that itself contains a pointer to some object.

The key thing is that when you have a «handle», you neither know nor care how that handle actually ends up identifying the thing that it identifies, all you need to know is that it does.

It should also be obvious that there is no single answer to «what exactly is a handle», because handles to different things, even in the same system, may be implemented in different ways «under the hood». But you shouldn’t need to be concerned with those differences.

Источник

Is HANDLE similar to file descriptor in Linux?

Is HANDLE similar to file descriptor in Linux? As far as I know, HANDLE is used for handling every resources on Windows, such as font, icons, files, devices. which in essence is just a void pointer point to a memory block holding data of a specific resource

A Windows HANDLE is a sort of token for a kernel object (always referring to objects that have been loaded into memory). The OS creates a mapping to the kernel objects and the handle (passed to user space) is the «key» to that mapping. Notice there can be different handles for the same kernel object (even in the same process) and the OS will not destroy the object until you close all the handles.

Print out the HANDLE values. you’ll see they’re not pointers. Thing is, it doesn’t matter what the values represent, you shouldn’t care. So yes, they’re both provide the same functionality but use different methods.

But you should make a difference between true kernel HANDLEs, which are, plainly speaking, those closed with CloseHandle() , and other objects that are syntactically similar but are not kernel objects, such as window handles (HWND), GDI objects (HICON, HGC, HBITMAP), HMENU, etc.

4 Answers 4

Yes, Windows handles are very similar to Unix file descriptors (FDs).

Note that a HANDLE is not a pointer to a block of memory. Although HANDLE is typedef ‘d as void * , that’s just to make it more opaque. In practice, a HANDLE is an index that is looked up in a table, just as an FD number is.

Yes, they are conceptually similar. File descriptors in unix map integers to a per-process table of pointers to other objects (which can be other things than files, too). File descriptors are not as unified though — some things exist in a separate «namespace» (e.g., process timers). In that respect, Windows is more orthogonal — CloseHandle will always free a resource regardless of what it is.

Читайте также:  Показать размеры папок linux

Besides the fact that handles refer to a far broader concept on Windows. Even we restrict the discussion to only file handles, there is significant differences. There is a function called _open_osfhandle() as part of C run-time library on Windows. Its purpose is to, quote «Associates a C run-time file descriptor with an existing operating-system file handle.» That is, a glue function between the kernel land and the C Run-time land. The function signature is as below:

int _open_osfhandle ( intptr_t osfhandle, int flags ); 

File handles Windows is actually more feature rich than file descriptors in C, which can be configured when a file handle is created with CreateFileA (ANSI version) or CreateFile (UTF16 version), reflecting the design difference between *Nix and Windows. And the resulted handle carries all these information around with all its implications.

Источник

What exactly is «handle»?

I’ve often heard about «handles», what exactly are those? Edit: For instance I have heard about: windows handles event handles file handles and so on. Are those things the same? Or they are some abstract terms?

What is the context in which you have heard the term? It’s a very general term and has multiple meanings.

3 Answers 3

A handle is an indirect way to reference an object owned by the OS or a library. When the operating system or a library owns an object but wants to let a client refer to it, it can provide a reference to that object called a handle.

Handles can be implemented in different ways. Typically they are not references in the C++ or C# sense. Often they are pointers cast to some opaque type, or they might be (or contain) an index into a table of objects that are owned by the operating system or library.

For example, in Windows, if you create a window, the OS creates an object that represents the window, but it doesn’t return a pointer to that object. Instead, it returns a window handle, which provides an extra layer of indirection. When you pass the window handle back in another OS call, the OS knows which window object to use based on the handle. This prevents your code from directly accessing the window object.

The extra layer of indirection allows the OS or library to do things like move objects around, reference count the objects, and generally control what happens to the object. Like the PIMPL idiom, the implementation may change completely while still preserving the original API and thus not forcing clients to recompile. It’s especially useful if you’re trying to offer a non-object-oriented API for clients written in procedural languages like C.

Источник

What data type of HANDLE could be in C++ Linux? [duplicate]

What data type of HANDLE could be in C++ Linux?, or it can be any type based on the requirement. HANDLE can be void type or other data types. The example below to define the use of HANDLE type in my code.

HANDLE hFile; HANDLE hFile = CreateFile(hldFile, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 

I need to define the data type to HANDLE to return value of calling other functions like CreateFile in the example above.

Читайте также:  Get all packages installed linux

The question is not duplicated I SEARCHED BEFORE. I am not asking about CreateFile . I am asking about HANDLE

HANDLE and CreateFile go hand in hand. If your code uses both then both need to change if you want your code to work on Linux. Linux has neither HANDLE nor CreateFile . If your code uses other functions that return HANDLE values or take HANDLE values as arguments, like ReadFile or CloseHandle , then those functions don’t exist on Linux either and also need to be changed.

Doesn’t look like a duplicate to me — but it is unclear what you’re asking. If you can rewrite your question to make it clearer what you’re actually trying to achieve, it might get reopened.

This question is not a duplicate, and it is wrong to close it just «because you can». Anyway, @YAcaCsh I spent some time testing and I ended up using open() and pwrite(), and they get the job done exactly as CreateFile and WriteFile does. Here my piece for you. pastebin.com/gwn3PCDp

1 Answer 1

That might be open() function with appropriate flags or creat(). Roughly speaking that would be int type.

Then, as I understand from your answer that HANDLE can be any type based on the use of it. Like wchar or char

HANDLE is a pointer. You cannot coerce it into an integer. With the STRICT symbol defined, the Windows headers help you out by defining it as a pointer-to-struct to prevent you from interconverting between different types of incompatible pointers, but it’s still a pointer, and you do have to worry about that. It is never a wchar_t or a char .

@CodyGray, if STRICT is defined, as is normally the case (i.e. NO_STRICT is not defined), then HANDLE and HGDIOBJ are typedefs for void * ; HGLOBAL and HLOCAL are typedefs for HANDLE ; and most other handle types with a given type name## are typedefs for a struct name##__ * . Otherwise with the non-strict definitions, HANDLE is a typedef for PVOID , and other handle types are typedefs for HANDLE .

@Cody, my interpretation of the question is that the OP is trying to build a library that emulates part of the Win32 API on Linux (e.g., he wants to write a CreateFile function) in which case he’s probably not using the Windows headers.

I had a completely different view of the question, @Harry. It’s possible my interpretation was wrong. It isn’t especially clear to me. Also, I appear to have misremembered how HANDLE was defined. eryksun is right; it’s unaffected by STRICT . It’s always a void* .

Источник

Оцените статью
Adblock
detector