2

我正在“本机子系统”中的 wdk 中构建一个简单的代码我有一个重载 new 和 delete 运算符的库这里是代码:



    #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:'operator new':重新定义;不同类型修饰符错误 C2373:“运算符删除”:重新定义;不同的类型修饰符

感谢您的帮助

4

1 回答 1

1

如果您查看编译器的<new>标头,则 for 的声明operator delete如下所示:

void __CRTDECL operator delete(void *) _THROW0();

如果宏__CRTDECL扩展为非空(但可能是__cdecl),则您的声明可能不匹配。

类似的operator new

于 2012-05-01T13:09:56.033 回答