Overloading operator new with WDK

橙三吉。 提交于 2019-12-08 03:51:22

问题


i'm building a simple code in wdk in "native subsystem" i have a library that overload new and delete operator here is the code:



    #include<ntddk.h&gt

    #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
            );
    }

and the cpp file code:



    #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);
    }

and the error is : error C2373: 'operator new' : redefinition; different type modifiers error C2373: 'operator delete' : redefinition; different type modifiers

thanks for your help


回答1:


If you look into the compiler's <new> header, the declaration for operator delete looks like this:

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

If the macro __CRTDECL expands into something other than nothing (but perhaps __cdecl), you may have a mismatch with your declaration.

Similar for operator new.



来源:https://stackoverflow.com/questions/10397826/overloading-operator-new-with-wdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!