-1
  1. How can i get HANDLE to the memory region or mapped file ?
  2. What actually HANDLE is ?

Please do not answer like this :

A handle is an abstract reference to some resourc,e provided to you by another party (usually the OS), that you can hand back to reference that resource.

I'm interested in technical side

xdoborax
  • 9
  • 2
  • There is no such thing as a *"`HANDLE` to the memory region"*. If you want high quality answers you need to provide a good question. See [How to ask questions](http://stackoverflow.com/questions/how-to-ask) for details. – IInspectable Sep 01 '13 at 15:55
  • @IInspectable A `HANDLE` to a memory region could be implied from what is returned by `GetPrcoessHeap()`. – Mike Weir Sep 01 '13 at 16:00
  • @PhoenixX_2 And how do memory mapped files fit into this picture? Of course, we could run this guessing game for a bit. As an alternative, the poster can ask a specific question. The latter is going to be more valuable. – IInspectable Sep 01 '13 at 16:02
  • @IInspectable Memory mapped files (`CreateFileMapping()`) are `HANDLE`s as well. – Mike Weir Sep 01 '13 at 16:04
  • I know... but where's the connection between the process heap and a file mapping? I cannot see one. Do you? – IInspectable Sep 01 '13 at 16:06
  • @IInspectable They are both using the `HANDLE` data-type, which is what the user is trying to wrap their head around. – Mike Weir Sep 01 '13 at 16:07

3 Answers3

1

A HANDLE is just some arbitrary nugget to some data.

For example, it's returned by the following APIs: CreateFile() and OpenProcess() - as you can tell, these two are very different, yet return the exact same data-type.

Or for memory, you can get access to the heap (which is returned as a HANDLE from GetProcessHeap()) and then use HeapAlloc() against it.

As MSDN points out, it's used in many other contexts:

http://msdn.microsoft.com/en-us/library/windows/apps/ms724211%28v=vs.85%29.aspx

Access token

Communications device

Console input

Console screen buffer

Event

File

File mapping

I/O completion port

Job

Mailslot

Memory resource notification

Mutex

Named pipe

Pipe

Process

Semaphore

Thread

Transaction

Waitable timer

Community
  • 1
  • 1
Mike Weir
  • 3,094
  • 1
  • 30
  • 46
  • 1
    A `HANDLE` is **not** a pointer. Also, what's so *flawed* about handles? In other words: How would you implement `WaitForMultipleObjects` that can consume polymorhpic types? – IInspectable Sep 01 '13 at 16:00
  • @IInspectable Actually, `HANDLE` is `typedef`d from a void*. I clarified my comment about big flaws. – Mike Weir Sep 01 '13 at 16:01
  • Whether you state your opinion or someone else's, please take the time to explain this. Or don't post opinions without a rationale. As for the `typedef`, every object in C/C++ must have a type. A `void*` is chosen as the type for a number of handles (like `HWND`). Those aren't pointers, though. They are rather indexes into a system controlled list. – IInspectable Sep 01 '13 at 16:05
  • @IInspectable I removed the opinion comment to upset you less. – Mike Weir Sep 01 '13 at 16:07
  • I'm not upset, merely waiting for the answer to my first question. So how would you implement it with a less flawed `HANDLE` type? How would your less flawed `HANDLE` type look? I'm interested, not upset. – IInspectable Sep 01 '13 at 16:08
  • @IInspectable I don't mind the Win32 mechanism, as I clarified (and have since removed for sake of argument). Especially since this makes the Win32 available to almost any language that handles DLLs - which is amazing. You can handle polymorphic types with proper use of inheritance in C++ - obviously since Win32 (well, Win16) dates back before classes/C++, this was not an option. – Mike Weir Sep 01 '13 at 16:11
  • can i pass handle recieved from GetProccessHeap to WriteFile or CopyFile or even ShellExecute ? – xdoborax Sep 01 '13 at 18:09
  • @xdoborax The documentation usually spells out which handles are allowed. You cannot, for example, pass the handle returned from `GetProcessHeap` to `WriteFile`. `ShellExecute` has no `HANDLE` parameter. The first parameter is a `HWND` which is a USER handle, unrelated to kernel objects referenced through `HANDLE`s. – IInspectable Sep 01 '13 at 18:20
0

If you do just want to know what it is:

In simple operating systems such handles are really only pointers to a structure in Kernel space. The structure contain more information about the handle itself (e.g. pointer to a file structure). More complex operating systems like Windows will check the validity of a handle before trying to access this structure.

Because the HANDLE is opaque in Windows (this means: Microsoft says: Do not interpret it - it is just a 32-bit number) the actual meaning of a HANDLE may be different in different versions of Windows. Even within one Version (e.g. Vista 32-bit vs. Vista 64-bit or XP SP2 vs XP SP3) the (internal) meaning may be different.

Microsoft may change the meaning anytime they want to change it (it may be a pointer in Windows 95 but an index into an array in Windows 8 - who knows).

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
0

Handles in windows used to be pointer to a pointer to memory location of an object. In win 3.1 days some program cast some kind of handle to see what's behind. Today to my knowledge this is not possible anymore, they are simple unique identifier or token to communicate between you and the system.
Some more information http://en.wikibooks.org/wiki/Windows_Programming/Handles_and_Data_Types
some more from msdn read paragraph object in 16 bits windows http://msdn.microsoft.com/en-us/library/ms810501.aspx

ColdCat
  • 1,192
  • 17
  • 29