Click here to Skip to main content
12,953,937 members (44,208 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi all , i am hooking to CopyItems function ,but my problem with this is while returning the real CopyItems Function in the Callback am getting the Following Error:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  
This is usually a result of calling a function declared with one calling convention with a function pointer 
declared with a different calling convention.


my code is as below please go through it and help me.

#include "stdafx.h"
#include <windows.h>
#include "MinHook.h"
#include <Winternl.h>
#include <stdio.h>
#include <shlwapi.h>
#include <tchar.h>
#include <string.h>
#include <psapi.h>
#include <strsafe.h>
#include <Shobjidl.h>
 
#if defined _M_X64
#pragma comment(lib, "libMinHook.x64.lib")
#elif defined _M_IX86
#pragma comment(lib, "libMinHook.x86.lib")
#endif

PVOID GetInterfaceMethod(PVOID intf, DWORD methodIndex)
 {
  return *(PVOID*)(*(DWORD*)intf + methodIndex * 4);
 }
 
typedef HRESULT (WINAPI  *CopyItemsNext)(IUnknown *punkItems,IShellItem *psiDestinationFolder);
CopyItemsNext Real_CopyItems = NULL;
CopyItemsNext Actual_CopyItems;
 

HRESULT WINAPI CopyItemsCallback(IUnknown *punkItems,IShellItem *psiDestinationFolder)
{
 
    MessageBoxW(NULL,L"CopyItems Function Called", L"HookedCopyItemS", MB_OK);
    return Real_CopyItems(punkItems, psiDestinationFolder);
}
 

HRESULT WINAPI CoCreateInstanceCallback(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID *ppv)
{
   const char *IFileOperation_GUID = "{3AD05575-8857-4850-9277-11B85BDB8E09}";
   char GUIDString[64];
 
   HRESULT HR = Real_CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv);
 
   sprintf_s(GUIDString,64, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\0",
    rclsid.Data1, rclsid.Data2, rclsid.Data3,
    rclsid.Data4[0], rclsid.Data4[1],
    rclsid.Data4[2], rclsid.Data4[3],
    rclsid.Data4[4], rclsid.Data4[5],
    rclsid.Data4[6], rclsid.Data4[7]);
 
   if(strcmp(GUIDString, IFileOperation_GUID) == 0)
   {
       MessageBoxA(NULL, "IFileOperation_GUID Found", GUIDString, MB_OK);
 
       if(Real_CopyItems == NULL)
       {
        Actual_CopyItems = (CopyItemsNext)GetInterfaceMethod(*ppv, 17);
        MessageBoxA(NULL,"AFTER GetInterfaceMethod", "TEST", MB_OK);
 
        if (MH_CreateHook(Actual_CopyItems, &CopyItemsCallback, reinterpret_cast<void**>(&Real_CopyItems)) != MH_OK)
        {
            MessageBoxW(NULL, L"Failed CreateHook Real_CopyItem", L"Info!", MB_ICONWARNING|MB_OK);
        }
        if (MH_EnableHook(Actual_CopyItems) != MH_OK)
        {
            MessageBoxW(NULL, L"Failed EnableHook Real_CopyItem", L"Info!", MB_ICONWARNING|MB_OK);
        }
    }
}
return HR;
}
 
//DllMain Function 
BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
    if (MH_Initialize() != MH_OK)
    {
        MessageBoxW(NULL, L"Failed Initialize", L"Info!", MB_ICONWARNING|MB_OK);    
    }
    if (MH_CreateHook(&CoCreateInstance, &CoCreateInstanceCallback, reinterpret_cast<void**>(&Real_CoCreateInstance)) != MH_OK)
    {
        MessageBoxW(NULL,L"Failed MH_CreateHook CoCreateInstance",L"Info!",MB_ICONWARNING|MB_OK);
    }
    if (MH_EnableHook(&CoCreateInstance) != MH_OK)
    {
        MessageBoxW(NULL,L"Failed MH_EnableHook StartDocA",L"Info!",MB_ICONWARNING|MB_OK);
    }
    break;
 
case DLL_PROCESS_DETACH:
    if (MH_Uninitialize() != MH_OK)
    {               
    }
    if (MH_DisableHook(Actual_CopyItems) != MH_OK)
    {
    }
    if (MH_DisableHook(&CoCreateInstance) != MH_OK)
    {
    }
 
    break;
}
return TRUE;
}
Posted 16-Aug-12 20:01pm
Updated 16-Aug-12 20:04pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,464
CHill60 3,490
Maciej Los 3,123
ppolymorphe 2,020
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 17 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100