Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

redirecting function to another version of dll

Posted on 2009-04-09
2
769 Views
Last Modified: 2013-12-14
Hi,

I would like to redirect a COM interface method into another version of same dll. I use Detours 2.1 Express to hook the function. in the snippet you can see a sample of hooking the IWebBrowser2::Navigate2 method. it works as expected but not as needed :) what I need is to call the Navigate2 method from custom dll (redirect it).

This is not a problem when it's an ordinary function like i.e. Sleep from kernel32.dll (I can use LoadLibrary, GetProcAddress or DetourFindFunction which in fact tries to use LoadLibrary...). Problem is how to call a COM interface method from custom dll. Any suggestions? Will appreciate any help. Thank you in advance.
#define CINTERFACE
 
#include <windows.h>
#include "detours.h"
#include <stdlib.h>
#include <iostream>
#include <string.h>
 
#include <ObjBase.h>
#include <ExDisp.h>
 
using namespace std;
 
//function prototype
HRESULT (__stdcall *RealNavigate2)(IWebBrowser2 *This,
								VARIANT *URL,
								VARIANT *flags,
								VARIANT *TargetFrameName,
								VARIANT *PostData,
								VARIANT *Headers) = NULL;
 
//detour function
HRESULT __stdcall NewNavigate2(IWebBrowser2 *This,
							VARIANT *URL,
							VARIANT *flags,
							VARIANT *TargetFrameName,
							VARIANT *PostData,
							VARIANT *Headers)
{
	//HERE IT CALLS THE ORIGINAL IWebBrowser2::Navigate2
	//I NEED TO REDIRECT IT TO OTHER VERSION OF SAME DLL
	cout << "hooked Navigate2 :)" << endl;
	HRESULT hr = RealNavigate2(This, URL, flags, TargetFrameName, PostData, Headers);
	return hr;
}
 
 
int main(int argc, char **argv){
	
	if(SUCCEEDED(OleInitialize(NULL))){
		
		IWebBrowser2 *pBr;
		CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void**)&pBr);
		
		//assigning the target function for detouring
		RealNavigate2 = pBr->lpVtbl->Navigate2;
		
		//this part is not important, only some string processing
		string url = "http://www.google.com";
		VARIANT myURL;
		VariantInit(&myURL);
		myURL.vt = VT_BSTR;
 
		#ifndef UNICODE
		{
			wchar_t		*buffer;
			DWORD		size;
 
			size = MultiByteToWideChar(CP_ACP, 0, url.c_str(), -1, 0, 0);
			if (!(buffer = (wchar_t *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t) * size))){
				cout << "badalloc" << endl;
				getchar();
				return 0;
			}
			MultiByteToWideChar(CP_ACP, 0, url.c_str(), -1, buffer, size);
			myURL.bstrVal = SysAllocString(buffer);
			GlobalFree(buffer);
		}
		#else
		myURL.bstrVal = SysAllocString(url);
		#endif		
		
		//attaching target and detour function
		DetourTransactionBegin();
		DetourUpdateThread(GetCurrentThread());
		DetourAttach(&(PVOID&)RealNavigate2, NewNavigate2);
		DetourTransactionCommit();
 
		VARIANT vFlags;
		V_VT(&vFlags) = VT_I4;
		V_I4(&vFlags) = navOpenInNewWindow;
		
		VARIANT vEmpty;
		VariantInit(&vEmpty);
 
		//calling the target function, which gets hooked and it will jump to execution of NewNavigate2 detour function
		HRESULT hr = pBr->lpVtbl->Navigate2(pBr, &myURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
		if (SUCCEEDED(hr))
		{
			pBr->lpVtbl->put_Visible(pBr, VARIANT_TRUE);
		}
		else
		{
			pBr->lpVtbl->Quit(pBr);
		}
 
		pBr->lpVtbl->Release(pBr);
 
		OleUninitialize();
 
		
	}
	
	return 0;
}

Open in new window

0
Comment
Question by:StraySod
2 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24105537
A COM Dll is registered in registry what means that only one (probably the youngest) version is currently active.

The only way I know to call a dll of different version (beside of explicitly loading from file) is to register it in the .manifest file of your application. AFAIK that could be done with MSI but I never did it myself. And I don't know either whether it works for COM as well (but there is a good chance cause the .manifest simply overrules normal registry entries).

Note, for using .manifest files you need XP or Vista.
0
 
LVL 7

Author Closing Comment

by:StraySod
ID: 31568438
Ok, thank you for your reply :) will try it
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 163
WinWaitActive parameters 12 31
sum13 challenge 24 122
what is the best Integrated development environment 2 20
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question