System.DllNotFoundException using EasyHook in Visual Studio 2010
I've the following code:
try
{
Debug.WriteLine("Hook Start");
RecvHook = LocalHook.Create(
LocalHook.Get开发者_运维技巧ProcAddress("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...
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?
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.
精彩评论