This example illustrates the use of the GetProcessHeaps function to retrieve handles to the default process heap and any private heaps that are active for the current process.
The example calls GetProcessHeaps twice, first to calculate the size of the buffer needed and again to retrieve handles into the buffer. The buffer is allocated from the default process heap, using the handle returned by GetProcessHeap . The example prints the starting address of each heap to the console. It then uses the HeapFree function to free memory allocated for the buffer.
The number of heaps in a process may vary. A process always has at least one heap—the default process heap—and it may have one or more private heaps created by the application or by DLLs that are loaded into the address space of the process.
Note that an application should call heap functions only on its default process heap or on private heaps that the application has created; calling heap functions on a private heap owned by another component may cause undefined behavior.
代码:
1 #include <windows.h>
2 #include <tchar.h>
3 #include <stdio.h>
4 //#include <intsafe.h>
5
6 int __cdecl _tmain()
7 {
8 DWORD NumberOfHeaps;
9 DWORD HeapsIndex;
10 DWORD HeapsLength;
11 HANDLE hDefaultProcessHeap;
12 HRESULT Result;
13 PHANDLE aHeaps;
14 SIZE_T BytesToAllocate;
15
16 //
17 // Retrieve the number of active heaps for the current process
18 // so we can calculate the buffer size needed for the heap handles.
19 //
20 NumberOfHeaps = GetProcessHeaps(0, NULL);
21 if (NumberOfHeaps == 0) {
22 _tprintf(TEXT("Failed to retrieve the number of heaps with LastError %d.\n"),
23 GetLastError());
24 return 1;
25 }
26
27 //
28 // Calculate the buffer size.
29 //
30 //Result = SIZETMult(NumberOfHeaps, sizeof(*aHeaps), &BytesToAllocate);
31 //if (Result != S_OK) {
32 // _tprintf(TEXT("SIZETMult failed with HR %d.\n"), Result);
33 // return 1;
34 // }
35 BytesToAllocate = NumberOfHeaps * sizeof(*aHeaps);
36
37 //
38 // Get a handle to the default process heap.
39 //
40 hDefaultProcessHeap = GetProcessHeap();
41 if (hDefaultProcessHeap == NULL) {
42 _tprintf(TEXT("Failed to retrieve the default process heap with LastError %d.\n"),
43 GetLastError());
44 return 1;
45 }
46
47 //
48 // Allocate the buffer from the default process heap.
49 //
50 aHeaps = (PHANDLE)HeapAlloc(hDefaultProcessHeap, 0, BytesToAllocate);
51 if (aHeaps == NULL) {
52 _tprintf(TEXT("HeapAlloc failed to allocate %d bytes.\n"),
53 BytesToAllocate);
54 return 1;
55 }
56
57 //
58 // Save the original number of heaps because we are going to compare it
59 // to the return value of the next GetProcessHeaps call.
60 //
61 HeapsLength = NumberOfHeaps;
62
63 //
64 // Retrieve handles to the process heaps and print them to stdout.
65 // Note that heap functions should be called only on the default heap of the process
66 // or on private heaps that your component creates by calling HeapCreate.
67 //
68 NumberOfHeaps = GetProcessHeaps(HeapsLength, aHeaps);
69 if (NumberOfHeaps == 0) {
70 _tprintf(TEXT("Failed to retrieve heaps with LastError %d.\n"),
71 GetLastError());
72 return 1;
73 }
74 else if (NumberOfHeaps > HeapsLength) {
75
76 //
77 // Compare the latest number of heaps with the original number of heaps.
78 // If the latest number is larger than the original number, another
79 // component has created a new heap and the buffer is too small.
80 //
81 _tprintf(TEXT("Another component created a heap between calls. ") \
82 TEXT("Please try again.\n"));
83 return 1;
84 }
85
86 _tprintf(TEXT("Process has %d heaps.\n"), HeapsLength);
87 for (HeapsIndex = 0; HeapsIndex < HeapsLength; ++HeapsIndex) {
88 _tprintf(TEXT("Heap %d at address: %#p.\n"),
89 HeapsIndex,
90 aHeaps[HeapsIndex]);
91 }
92
93 //
94 // Release memory allocated from default process heap.
95 //
96 if (HeapFree(hDefaultProcessHeap, 0, aHeaps) == FALSE) {
97 _tprintf(TEXT("Failed to free allocation from default process heap.\n"));
98 }
99
100 return 0;
101 }
结果:
转自:http://msdn.microsoft.com/en-us/library/ee175820(v=vs.85).aspx
说明:
- heap handle就是堆地址
- GetProcessHeap()获取默认堆地址0x00150000
- 在进程的用户区存着一个叫PEB(进程环境块)的结构,这个结构中存放着一些有关进程的重要信息,其中在PEB首地址偏移0x18处存放的ProcessHeap就是进程默认堆的地址,而偏移0x90处存放了指向进程所有堆的地址列表的指针。
- AP(malloc/new)--->CRT(HeapCreat)--->Heap Manager(VirtualAlloc)--->Windows