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 ドキュメントには次のように記載されています。 PATH 環境変数。必要なすべての依存関係が、注入アプリケーションのディレクトリ、システム ディレクトリ、またはデフォルトで PATH 変数に含まれる任意のディレクトリ内にあることを確認してください。"

Desperate Last Resort : 一部のエラーは少なくとも半良性であるため、VS メニューの [Debug] -> [Exceptions] に移動して、問題のあるエラーのチェックを外して、そこで壊れないようにすることができます。VSにそのエラーで中断しないように指示すると、コードが実際に正常に実行されるケースが1つか2つありました。

ところで: どのバイナリを含めましたか? システム アーキテクチャと OS は何ですか?

于 2011-01-25T09:24:11.707 に答える
0

正確な解決策を提供することはできませんが、これらの問題を抱えている可能性があります...

  • project/bin フォルダーに dll を追加していますか? はいの場合は、そのフォルダーを System32 にコピーし、そのフォルダーへの参照として再度追加します。

  • または、これを試して、Visual Studio コマンド プロンプトを開き、このコマンド regsvr32 yourDLLLocationを実行 してから、今すぐ参照として追加します。

問題が解決することを願っています。システムの種類と dll の種類がどちらも win32 アプリ用であることを確認してください。

于 2011-01-25T09:48:11.547 に答える