1

我有以下代码:

       try
        {
            Debug.WriteLine("Hook Start");
            RecvHook = LocalHook.Create(
                LocalHook.GetProcAddress("ws2_32.dll", "recv"),
                new Drecv(recv_Hooked),
                this);


            RecvHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
        }
        catch (Exception ExtInfo)
        {
            Debug.WriteLine("Error creating Hook");
        }
...
        [DllImport("ws2_32.dll")]
        static extern int recv(
                    IntPtr socketHandle,
                    IntPtr buf,
                    int count,
                    int socketFlags
            );


        [UnmanagedFunctionPointer(CallingConvention.StdCall,
            CharSet = CharSet.Unicode,
            SetLastError = true)]


        delegate int Drecv(
                    IntPtr socketHandle,
                    IntPtr buf,
                    int count,
                    int socketFlags
            );


        static int recv_Hooked(
                    IntPtr socketHandle,
                    IntPtr buf,
                    int count,
                    int socketFlags)
        {
            byte[] test = new byte[count];
            Marshal.Copy(buf, test, 0, count);
            IntPtr ptr = IntPtr.Zero;

            ptr = Marshal.AllocHGlobal(count);
            Marshal.Copy(test, 0, ptr, count);


            string s = System.Text.UnicodeEncoding.Unicode.GetString(test);
            Debug.WriteLine(s);
            System.IO.StreamWriter file = new System.IO.StreamWriter("log.txt");
            file.WriteLine(s);


            file.Close();
            return recv(socketHandle, buf, count, socketFlags);

        }

当我运行该项目时,我收到以下错误:

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll
Inizio Hook
A first chance exception of type 'System.DllNotFoundException' occurred in EasyHook.dll
A first chance exception of type 'System.DllNotFoundException' occurred in EasyHook.dll
Error creating Hook

关于可能导致该错误的任何建议?我添加了对所有需要的 dll 的引用...

4

2 回答 2

0

最有可能:尝试以管理员身份运行 VS 2010。我实际上让我的 VS 开始菜单快捷方式“以管理员身份运行”,所以我不必记住。

或者:Inject 方法的 EasyHook 文档提到:“如果您将库注入任何目标进程,请记住您的工作目录将被切换。EasyHook 将自动将注入应用程序的目录添加为目标的第一个目录PATH 环境变量。因此,请确保所有必需的依赖项都位于注入应用程序的目录、系统目录或默认包含在 PATH 变量中的任何目录中”

绝望的最后手段:一些错误至少是半良性的,所以你可以去 VS 菜单 Debug-->Exceptions... 并取消选中有问题的错误,除非它不会在那里中断。我有一两个案例,一旦我告诉 VS 不要因该错误而中断,代码实际上运行良好。

顺便说一句:你包含了哪些二进制文件,你的系统架构和操作系统是什么?

于 2011-01-25T09:24:11.707 回答
0

我不能给你确切的解决方案,但你可能有这些问题......

  • 您是否在项目/bin 文件夹中添加 dll?如果是,将该文件夹复制到 System32 并再次添加作为对该文件夹的引用

  • 或者试试这个,打开 Visual Studio 命令提示符并运行此命令 regsvr32 yourDLLLocation 然后现在将其添加为引用。

希望它能解决问题,并确保您的系统类型和 dll 类型都应该适用于 win32 应用程序。

于 2011-01-25T09:48:11.547 回答