2
我建立一個簡單的代碼在WDK在「本地子系統」 我有超載新圖書館和刪除操作 這裏是代碼:重載new運算符與WDK
#include<ntddk.h>
#define HEAP_ZERO_MEMORY 0x00000008
#define HEAP_GROWABLE 0x00000002
typedef NTSTATUS
(NTAPI * PRTL_HEAP_COMMIT_ROUTINE)(
IN PVOID Base,
IN OUT PVOID *CommitAddress,
IN OUT PSIZE_T CommitSize
);
typedef struct _RTL_HEAP_PARAMETERS {
ULONG Length;
SIZE_T SegmentReserve;
SIZE_T SegmentCommit;
SIZE_T DeCommitFreeBlockThreshold;
SIZE_T DeCommitTotalFreeThreshold;
SIZE_T MaximumAllocationSize;
SIZE_T VirtualMemoryThreshold;
SIZE_T InitialCommit;
SIZE_T InitialReserve;
PRTL_HEAP_COMMIT_ROUTINE CommitRoutine;
SIZE_T Reserved[ 2 ];
} RTL_HEAP_PARAMETERS, *PRTL_HEAP_PARAMETERS;
void* operator new(size_t sz);
void operator delete(void* p);
void* mg_malloc(unsigned int size);
int mg_free(void *buffer);
void InitAPSupportLibrary();
void FinishAPSupportLibrary();
extern "C"
{
PVOID RtlAllocateHeap(
__in PVOID HeapHandle,
__in_opt ULONG Flags,
__in SIZE_T Size
);
BOOLEAN RtlFreeHeap(
__in PVOID HeapHandle,
__in_opt ULONG Flags,
__in PVOID HeapBase
);
PVOID
RtlCreateHeap(
IN ULONG Flags,
IN PVOID Base ,
IN ULONG Reserve ,
IN ULONG Commit,
IN BOOLEAN Lock ,
IN PRTL_HEAP_PARAMETERS RtlHeapParams );
PVOID RtlDestroyHeap(
__in PVOID HeapHandle
);
}
和cpp文件代碼:
#include "main.h"
PVOID mainHeapHandle = NULL;
void InitAPSupportLibrary()
{
mainHeapHandle = RtlCreateHeap(HEAP_GROWABLE,NULL,0,0,NULL,0);
}
void FinishAPSupportLibrary()
{
if(mainHeapHandle) RtlDestroyHeap(mainHeapHandle);
}
void* mg_malloc(unsigned int size)
{
if(!mainHeapHandle)
InitAPSupportLibrary();
void *buf = RtlAllocateHeap(mainHeapHandle,HEAP_ZERO_MEMORY,size);
return buf;
}
int mg_free(void *buffer)
{
if (!mainHeapHandle) return 0;
return RtlFreeHeap(mainHeapHandle,0,buffer)?1:0;
}
void* operator new(size_t sz)
{
void* p = (void*)mg_malloc(sz);
return p;
}
void operator delete(void* p)
{
mg_free(p);
}
和錯誤是: 錯誤C2373: '運算新':重新定義;不同類型修飾符 錯誤C2373:'operator delete':重新定義;不同類型修飾符
感謝您的幫助
感謝您的回覆,但我正在尋找另一種解決方案。不使用這些調用約定 – native99
問題是,有些代碼顯然看到''中的運算符和代碼中的運算符。編譯器不喜歡它們在調用約定時有所不同,因爲它不知道如何調用它們。 –