Creating service-server problem

Jan 19, 2010 at 1:40 AM

Hello Chriss,

I have to admit that I seriously admire your work. It is one of few 100% completed open source projects I have ever seen. On to the problem i have now, I try to make some kind of anti-cheat engine for games, trying to learn how your excellent project works. I altered the code of your Process Monitor injected dll (ProcMonInject.dll) and can succesfully use it with your ProcessMonitor.exe. Though i seem to have a problem when I try to change your ProcessMonitor.exe with a service of mine. I succesfully register the .exe and the dll to GAC with your register function, Service succesfully installs with installutil.exe though when I try to inject the dll to lets say explorer.exe I get an error message "Exception has been thrown be the target of an invocation". I have created a strong name file and inserted into my service, made the service "COM visible" but I have no clue what is missing. After reporting that error message the service continues in the "Started" status.

I hambly apologise for my bad english.

Thank you in advance for your answer.

Ps: Below is the code of my service that compiles succesully, I have some unneeded namespaces/code because i was trying to figure out what is going wrong, but this is only a test.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;


using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Security.Principal;
using System.Security;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Threading;
using EasyHook;

namespace WugncpSvc
{
    public partial class Service1 : ServiceBase
    {
        [Serializable]
        public class ProcessInfo
        {
            public String FileName;
            public Int32 Id;
            public Boolean Is64Bit;
            public String User;
        }
        private List<Process> LastProcesses = new List<Process>();
        static internal List<Int32> HookedProcesses = new List<Int32>();
        private String ChannelName = null;
        private IpcServerChannel DemoServer;

        /*
         * Please note that we have obtained this information with system privileges.
         * So if you get client requests with a process ID don't try to open the process
         * as this will fail in some cases. Just search the ID in the following list and
         * extract information that is already there...
         *
         * Of course you can change the way this list is implemented and the information
         * it contains but you should keep the code semantic.
         */
        internal static List<ProcessInfo> ProcessList = new List<ProcessInfo>();
        private static List<Int32> ActivePIDList = new List<Int32>();
        private static System.Threading.Timer ProcessTimer = null;

        /*
         * In general you shouldn't access the GUI directly from other threads,
         * so we are using an asynchronous approach!
         */
        static internal Queue<MonitorEntry> MonitorQueue = new Queue<MonitorEntry>();
        static internal Boolean IsMonitoring = true;
        public Service1()
        {
            InitializeComponent();


            TIMER_Tick(null, null);
            DemoServer = RemoteHooking.IpcCreateServer<DemoInterface>(
                ref ChannelName,
                WellKnownObjectMode.Singleton);
        }
        public static ProcessInfo[] EnumProcesses()
        {




            List<ProcessInfo> Result = new List<ProcessInfo>();
            Process[] ProcList = Process.GetProcesses();

            for (int i = 0; i < ProcList.Length; i++)
            {
                Process Proc = ProcList[i];

                try
                {
                    ProcessInfo Info = new ProcessInfo();
                    //MessageBox.Show(Proc.MainModule.FileName);
                    Info.FileName = Proc.MainModule.FileName;
                    Info.Id = Proc.Id;
                    Info.Is64Bit = RemoteHooking.IsX64Process(Proc.Id);
                    Info.User = RemoteHooking.GetProcessIdentity(Proc.Id).Name;

                    Result.Add(Info);
                }
                catch(Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                }
            }
            return Result.ToArray();
        }
        protected override void OnStart(string[] args)
        {
            try
            {
                Config.Register(
                "A simple ProcessMonitor based on EasyHook!",
                "C:\\Windows\\system32\\ProcMonInject.dll",
                "C:\\Windows\\system32\\WugncpSvc.exe");
            }
            catch (Exception exreg)
            {
                MessageBox.Show("Error while registering " + exreg.Message);
            }
            ProcessInfo[] Result = EnumProcesses();
            for (int i = 0; i < Result.Length; i++)
            {
                try
                {
                    if (Result[i].FileName.ToLower().Contains("explorer.exe"))
                    {
                        HookedProcesses.Add(Result[i].Id);

                        try
                        {
                            MessageBox.Show("Injecting "+Result[i].FileName.ToLower() + " " + Result[i].Id.ToString());
                            RemoteHooking.Inject(
                                Result[i].Id,
                                "C:\\Windows\\system32\\ProcMonInject.dll", // 32-bit version (the same because AnyCPU)
                                "C:\\Windows\\system32\\ProcMonInject.dll", // 64-bit version (the same because AnyCPU)
                                // the optional parameter list...
                                ChannelName);
                        }
                        catch (Exception exinject)
                        {
                            MessageBox.Show("Error while injecting " + exinject.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("myError 100 : "+ex.Message);
                }
            }
            /*
            
             */
        }

        protected override void OnStop()
        {
        }
    }
}

Jan 20, 2010 at 12:04 AM
Edited Jan 20, 2010 at 12:09 AM

Hello again,

I have also seen this post http://easyhook.codeplex.com/Thread/View.aspx?ThreadId=48863 , but the problem still continues, i have installed the service as user too, copied everything in the system32 folder, installed manually in GAC easyhook.dll but all I get from eventviewer is that it cant find easyhook.dll and that easyhooksvc32 is faulting with that error message.

Error messages from eventviewer are

for EasyHook :

[error]: System.Runtime.Serialization.SerializationException: Unable to find assembly 'EasyHook, Version=2.5.0.0, Culture=neutral, PublicKeyToken=4b580fca19d0b0c5'.
   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap.Create(String name, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at EasyHook.InjectionLoader.Main(String InParam)

for my service :

Service cannot be started. System.ApplicationException: Unknown error code (-1073741502): The user defined managed entry point failed in the target process. Make sure that EasyHook is registered in the GAC. Refer to event logs for more information. (Code: 13)
   at EasyHook.NativeAPI.Force(Int32 InErrorCode)
   at EasyHook.RemoteHooking.InjectEx(Int32 InHostPID, Int32 InTargetPID, Int32 InWakeUpTID, Int32 InNativeOptions, String InLibraryPath_x86, String InLibraryPath_x64, Boolean InCanBypassWOW64, Boolean InCanCreateService, Object[] InPassThruArgs)
   at EasyHook.RemoteHooking.Inject(Int32 InTargetPID, String InLibraryPath_x86, String InLibraryPath_x64, Object[] InPassThruArgs)
   at WugncpSvc.Service1.OnStart(String[] args)
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

 

Let me remind again that easyhook is even manually installed in GAC and that my injected dll works fine with processmonitor.exe of the examples, and is strongly based on ProcMonInject.dll

Any help would be much appreciated.

Jack.

Jan 22, 2010 at 12:30 PM

anyone out there that could possibly help please?

Jan 27, 2010 at 6:06 PM

Hello,

Today after finding some time again to spend on easyhook I finally found what was missing.

With all friendly ways, if you want your project to find its respectfull place in the global community, there is only 1 think you need to improve, your support.

Thank you again, and keep up the good work on this excellent project.

May 25, 2010 at 2:31 PM

Could you post the solution.

 

Thanks

Jun 19, 2012 at 10:57 AM

Hi there, has anyone found a solution to this issue. It is driving me banana's!

I am using the new version of Easyhook 2.7 Alpha 3 from within a Windows Service. I have set the dependancy path:

Config.DependencyPath = @"C:\Program Files\CompanyName\Program name\";

and registered in the GAC:

 

string[] arr = new string[2];               

arr[0] = @"C:\Program Files\CompanyName\Program name\Program name.exe";               

arr[1] = @"C:\Program Files\CompanyName\Program name\FileMonInject.dll";               

Config.DependencyPath = @"C:\Program Files\CompanyName\Program name\";               

Config.Register("Program name",arr);

 

Then when I try to hook:

RemoteHooking.Inject((int)procId,  

@"C:\Program Files\CompanyName\Program name\FileMonInject.dll", // 32-bit version (the same because AnyCPU)               

@"C:\Program Files\CompanyName\Program name\FileMonInject.dll", // 64-bit version (the same because AnyCPU)               

ChannelName// the optional parameter list...               

);

 

I get the error: "Exception has been thrown by the target of an invocation."

 

Any thoughts?