0

Before writing this I went through more than 10 questions on this topic to make sure I'm not missing smth obvious, but I couldn't find answers to this specific scenario.

I am working on an enterprise product which has (very unfortunately) inherited some old VB6 code that is still used in several parts of the system. I am currently in the middle of an analysis on how best to identify how much of this VB6 code is actually in use so the company can target strategic migration to .NET. I've come up with the following idea:

In the places where the VB6 code is used the typical code that calls it (VB.NET) looks like:

Dim myComObject = Server.CreateObject("MyCOMObject")
With myComObject
    .MyCOMProperty = ""
    .MyCOMMethod()
End with

My idea is to build a facade in .NET behind which to hide all the code that calls COM and call the facade abstraction in all the places where the COM code is currently called. In other words:

public interface ICOMFacade 
{
    public object CreateCOMObject(string comObject)
}

...

Dim myComObject = comFacade.CreateCOMObject("MyCOMObject")
With myComObject
    .MyCOMProperty = ""
    .MyCOMMethod()
End with

The implementation of CreateCOMObject would internally call Server.CreateObject(comObject) but it would also log this call to a database. In this way I could analyse all the calls to all the COM objects and understand what is used and what isn't. However, this isn't enough. I also need to understand which methods and properties are called from the COM objects. For this reason I need to understand if there is a way in which every method call and property of the COM object can be intercepted.

Thank you.

erionpc
  • 368
  • 3
  • 15
  • 1
    Short answer is no. You could use detours/hook libraries, ex: https://github.com/microsoft/Detours, https://github.com/stevemk14ebr/PolyHook_2_0 but that's very hard. – Simon Mourier Feb 21 '20 at 08:14

1 Answers1

1

As Simon Mourier writes, no, there is no build in interception mechanism for COM methods, they (and the properties too) are just jump to addresses managed in virtual method tables.

Some try might be writing code which by Reflection inspects the COM classes and generates some .NET wrapper classes, either in memory (Reflection.Emit) or by generating C# code. Than using that classes instead of the original ones. But thats a long way to go...

Another idea that might or might not work is trying some aspect oriented post compiler (like PostSharp) to add logging aspects to the code you try to analyse.

Good luck!

protix
  • 99
  • 2