C++ 封装私有堆(Private Heap) 创建进程私有堆

Heap,创建进程私有堆

http://blog.csdn.net/hisinwang/article/details/45115885

原创 2015年04月18日 20:01:52
#include <Windows.h>
#include <iostream>

using namespace std;

int main()
{
    // Create a private heap
    HANDLE hHeapNew = HeapCreate(0,         // serialized and no exception
                                 1024*4,    // the initial size of the heap
                                 1024*16);  // the maximum size of the heap
    if (hHeapNew == NULL)
    {
        cout << "HeapCreate failed with error: " << GetLastError() << endl;
        return 0;
    }

    // Allocate a block of memory from the private heap
    LPVOID lpBuffer = HeapAlloc(hHeapNew,           // heap handle
                                HEAP_ZERO_MEMORY,   // initialized to be 0
                                100);               // number of bytes to be allocated

    if (lpBuffer == NULL)
    {
        cout << "HeapAlloc failed with error: " << GetLastError() << endl;
        return 0;
    }
    sprintf((char*)lpBuffer, "Hello Kitty");
    cout << (char*)lpBuffer << endl;

    // Free the memory block allocated from the private heap
    BOOL flag;
    flag = HeapFree(hHeapNew, 0, lpBuffer);
    if (!flag)
    {
        cout << "HeapFree failed with error: " << GetLastError() << endl;
        return 0;
    }

    // Destroy the heap 
    flag = HeapDestroy(hHeapNew);
    if (!flag)
    {
        cout << "HeapDestroy failed with error: " << GetLastError() << endl;
        return 0;
    }

    system("pause");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
 

 

posted @ 2018-03-19 14:19  sky20080101  阅读(76)  评论(0)    收藏  举报