在 Visual Studio 2010 中使用 EasyHook 时出现 System.DllNotFoundException
我有以下代码:
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 的引用......
I've the following code:
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);
}
When i run the project, I get the following error:
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
Any suggestion on what may cause that error? I added a reference to all the dll needed...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最有可能:尝试以管理员身份运行 VS 2010。实际上,我将 VS 开始菜单快捷方式设置为“以管理员身份运行”,因此我不必记住。
或者:Inject 方法的 EasyHook 文档提到:“如果您将库注入任何目标进程,请记住您的工作目录将被切换。EasyHook 将自动添加目录注入应用程序的目录作为目标 PATH 环境变量的第一个目录,因此请确保所有必需的依赖项都位于注入应用程序的目录、系统目录或 PATH 变量中默认包含的任何目录中。”
绝望的最后手段:有些错误至少是半良性的,所以你可以进入 VS 菜单“调试”-->“异常...”并取消选中有问题的错误,除非这样它就不会在那里中断。我遇到过一两个案例,当我告诉 VS 不要因该错误而中断时,代码实际上运行良好。
顺便说一句:您包含了哪些二进制文件,您的系统架构和操作系统是什么?
Most likely: Try running VS 2010 as an administrator. I actually made my VS start menu shortcut "Run as Administrator" so I don't have to remember.
Alternatively: The EasyHook documentation for the Inject method mentions that: "If you inject a library into any target process please keep in mind that your working directory will be switched. EasyHook will automatically add the directory of the injecting application as first directory of the target's PATH environment variable. So make sure that all required dependencies are either located within the injecting application's directory, a system directory or any directory defaultly contained in the PATH variable"
Desperate Last Resort: some errors are at least semi-benign, so you can go the VS menu Debug-->Exceptions... and uncheck the offending error except so it won't break there. I've had a case or two where the code actually ran fine once I told VS not to break on that error.
Btw: which binaries have you included, what's your system architecture and OS?
我无法给您确切的解决方案,但您可能遇到这些问题...
您是否在项目/bin 文件夹中添加了 dll?如果是,请将该文件夹复制到 System32,然后再次添加为该文件夹的引用
或者尝试此操作,打开 Visual studio 命令提示符并运行此命令regsvr32 yourDLLLocation
然后立即将其添加为参考。
希望它能解决问题,并确保您的系统类型和 dll 类型,两者都应该适用于 win32 应用程序。
I can't give you exact solution, but might you having these issues...
Are you adding the dll in your project/bin folder? If yes, copy that folder to System32 and and again add as reference to that folder
Or try this, open Visual studio command prompt and run this command regsvr32 yourDLLLocation
then add it as refernce now.
Hope it will solve the issue, and be sure your system type and dll type, both should be for win32 apps.