MissingMethodException: "Constructor on type 'FileMonInjection.Main' not found"

Feb 20, 2013 at 5:54 AM
Edited Feb 20, 2013 at 6:14 AM
Hi, everyone. I'm learning to use EasyHook recently, and I've gotten a problem at the beginning.
The example of 'FileMon' is programmed in C#, so I convert it into VB. And here's the code of Injector:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Threading
Imports System.Runtime.InteropServices
Imports EasyHook

Public Class Main
    Implements EasyHook.IEntryPoint
    Private [Interface] As FileMon.FileMonInterface
    Private CreateFileHook As LocalHook
    Private Queue As Stack(Of String) = New Stack(Of String)()

    Public Sub New(ByVal InContext As RemoteHooking.IContext, ByVal InChannelName As String)
        ' connect to host...
        [Interface] = RemoteHooking.IpcConnectClient(Of FileMon.FileMonInterface)(InChannelName)
    End Sub

    Public Sub Run(ByVal InContext As RemoteHooking.IContext, ByVal InChannelName As String)
        ' install hook...
        Try
            CreateFileHook = LocalHook.Create(LocalHook.GetProcAddress("kernel32.dll", "CreateFileW"), New DCreateFile(AddressOf CreateFile_Hooked), Me)
            CreateFileHook.ThreadACL.SetExclusiveACL(New Int32() {0})

        Catch ExtInfo As Exception
            [Interface].ReportException(ExtInfo)

            Return
        End Try

        [Interface].IsInstalled(RemoteHooking.GetCurrentProcessId())

        ' wait for host process termination...
        Try
            While (True)

                Thread.Sleep(500)
                ' transmit newly monitored file accesses...
                If (Queue.Count > 0) Then

                    Dim Package As String() = Nothing
                    SyncLock Queue
                        Package = Queue.ToArray()
                        Queue.Clear()
                    End SyncLock
                    [Interface].OnCreateFile(RemoteHooking.GetCurrentProcessId(), Package)
                Else
                    [Interface].Ping()
                End If
            End While
        Catch
            ' NET Remoting will raise an exception if host is unreachable
        End Try
    End Sub

    <UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Delegate Function DCreateFile(ByVal InFileName As String, ByVal InDesiredAccess As UInt32, ByVal InShareMode As UInt32, ByVal InSecurityAttributes As IntPtr,
                                  ByVal InCreationDisposition As UInt32, ByVal InFlagsAndAttributes As UInt32, ByVal InTemplateFile As IntPtr) As IntPtr

    ' just use a P-Invoke implementation to get native API access from C# (this step is not necessary for C++.NET)
    <DllImport("kernel32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
    Private Shared Function CreateFile(ByVal InFileName As String, ByVal InDesiredAccess As UInt32, ByVal InShareMode As UInt32, ByVal InSecurityAttributes As IntPtr,
                                           ByVal InCreationDisposition As UInt32, ByVal InFlagsAndAttributes As UInt32, ByVal InTemplateFile As IntPtr) As IntPtr
    End Function

    ' this is where we are intercepting all file accesses!
    Private Shared Function CreateFile_Hooked(ByVal InFileName As String, ByVal InDesiredAccess As UInt32, ByVal InShareMode As UInt32,
                                              ByVal InSecurityAttributes As IntPtr, ByVal InCreationDisposition As UInt32,
                                              ByVal InFlagsAndAttributes As UInt32, ByVal InTemplateFile As IntPtr) As IntPtr
        Try
            Dim This As Main = DirectCast(HookRuntimeInfo.Callback, Main)
            SyncLock This.Queue
                This.Queue.Push(InFileName)
            End SyncLock
        Catch

        End Try
        ' call original API...
        Return CreateFile(InFileName, InDesiredAccess, InShareMode, InSecurityAttributes, InCreationDisposition, InFlagsAndAttributes, InTemplateFile)
    End Function
End Class
Then the FileMon.exe:
Imports System
Imports System.Collections.Generic
Imports System.Runtime.Remoting
Imports System.Text
Imports EasyHook

Public Class FileMonInterface
    Inherits MarshalByRefObject

    Public Sub IsInstalled(ByVal InClientPID As Int32)
        Console.WriteLine("FileMon has been installed in target{0}.\r\n", InClientPID)
    End Sub

    Public Sub OnCreateFile(ByVal InClientPID As Int32, ByVal InFileNames As String())
        For i As Integer = 0 To InFileNames.Length - 1
            Console.WriteLine(InFileNames(i))
        Next
    End Sub

    Public Sub ReportException(ByVal InInfo As Exception)
        Console.WriteLine("The target process has reported an error:\r\n" & InInfo.ToString())
    End Sub

    Public Sub Ping()

    End Sub
End Class

Class Program
    Shared ChannelName As String = Nothing

    Shared Sub Main(ByVal args As String())
        'Try
        Config.Register("A FileMon like demo application.", "FileMon.exe", "FileMonInjection.dll")
        RemoteHooking.IpcCreateServer(Of FileMonInterface)(ChannelName, WellKnownObjectMode.SingleCall)
        RemoteHooking.Inject(Int32.Parse(args(0)), "FileMonInjection.dll", "FileMonInjection.dll", New Object() {ChannelName})  ‘error happens
        Console.ReadLine()

        'Catch ExtInfo As Exception
        'Console.WriteLine("There was an error while connecting to target:\r\n{0}", ExtInfo.ToString())
        'End Try
    End Sub
End Class
Here's my problem: When I debug it, it occurs an error at the code:
‘RemoteHooking.Inject(Int32.Parse(args(0)), "FileMonInjection.dll", "FileMonInjection.dll", New Object() {ChannelName})
and throws MissingMethodException: "Constructor on type 'FileMonInjection.Main' not found".

'FileMonInjection.Main' has an Constructor as you see, but why does the program catch such an exception? Thanks anyway and apologize for my poor English.
Coordinator
Feb 21, 2013 at 8:57 AM
Thanks for the info, at a glance I cannot see anything wrong with your code. I will setup a project to test the above in the next few days.

Cheers,
Justin
Feb 24, 2013 at 8:59 AM
Edited Feb 24, 2013 at 9:23 AM
Oh, I'm really grateful for your reading and replying.
I used Framework 3.5 as platform, would that be a problem? Maybe I should try to switch to 2.0 instead when I came back home.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
I just found a computer and rebuilt the project with Framework 2.0 and it works!
It seems that EasyHook is incompatible with higher Framework version? Hoping this info is useful to you.