Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalHook.Create throwing AccessViolationException #47

Open
arshrp opened this issue Oct 2, 2015 · 4 comments
Open

LocalHook.Create throwing AccessViolationException #47

arshrp opened this issue Oct 2, 2015 · 4 comments

Comments

@arshrp
Copy link

arshrp commented Oct 2, 2015

Hi EasyHookers,

Your product is working wonderfully for us in most situations. But we are experiencing an issue on Windows 2008 Terminal Services machine. The call to GetProcAddress is throwing this exception:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at EasyHook.LocalHook.Create(IntPtr InTargetProc, Delegate InNewProc, Object InCallback)

Your documentation clearly states that this exception will be thrown when "Access to target process denied or the current user is not an administrator", but we are running our application as an administrator (using the "Run as administrator" File Manager menu option), so it should have the necessary permissions.

Below is a summary of the code we are using. Are we doing anything wrong that might cause this error? ANY suggestions at all that you can provide would be most appreciated!

Andrew

public class Injector : EasyHook.IEntryPoint {

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
delegate bool DExtTextOutW(IntPtr hdc, int X, int Y, uint fuOptions, IntPtr lprc, IntPtr lpString, uint cbCount, IntPtr lpDx);

public void InstallHook(NxKitInterface iface) {
LocalHook.Create(
LocalHook.GetProcAddress("Gdi32.dll", "ExtTextOutW"),
new DExtTextOutW(ExtTextOutW_Hooked),
this
);
}

static bool ExtTextOutW_Hooked(IntPtr hdc, int X, int Y, uint fuOptions, IntPtr lprc, IntPtr lpString, uint cbCount, IntPtr lpDx) {
}

}

@justinstenning
Copy link
Member

Are you injecting into another process?

The problem appears to be less EasyHook specific, and more about the environment you are working within. LocalHook.GetProcAddress is simply calling the native kernel32.dll GetModuleHandle and GetProcAddress.

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr GetModuleHandle(String InPath);

[DllImport("kernel32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr GetProcAddress(IntPtr InModule, String InProcName);

@justinstenning
Copy link
Member

Can you try using manual calls to GetModuleHandle, and GetProcAddress?

@arshrp
Copy link
Author

arshrp commented Nov 6, 2015

Yeah, this is what I did:

IntPtr hand = GetModuleHandle(dll);
IntPtr pAdd = GetProcAddress(hand, function);
IntPtr ptr = LocalHook.GetProcAddress(dll, function);
LocalHook h = LocalHook.Create(LocalHook.GetProcAddress(dll, function), proc, this);

With log statements after each one. The only one that failed was the Create call. Thanks for the responses.

@justinstenning
Copy link
Member

@arshrp sorry for the long delay, did the LocalHook.Create work for you when specifying the address using the manual call to GetProcAddress? If so I am thinking of closing this issue, otherwise I may need a simple sample app to test properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants