0

I'm trying to hook a simple process using EasyHook. I'm programming/debugging in a x64 environment and compiling my DLL with AnyCpu config on Visual Studio C#.

The problem is that when trying to use this DLL (in another project, same Solution, compiling with AnyCpu too) in the inject library function:

RemoteHooking.Inject(TargetPID, "DivisionInject.dll", "DivisionInject.dll", ChannelName); (2nd parameter is for 32-bit system. 3rd is for 64 bit.)

I don't know why this line is throwing an exception: System.ArgumentException: the given 64-Bit library does not exist!

I thought that AnyCpu used to create my DLL, i could use the same file (DLL) in 32bits system and on 64 bits too. Is this wrong?

Thanks.

codechurn
  • 3,870
  • 4
  • 45
  • 65
tetsuoito
  • 1
  • 1
  • 1
  • 1
    Yes, it's wrong. The version that is available for use will be the one for the process that is using it (32-bit for a 32-bit process, 64-bit for a 64-bit process). You can't have a single process that is both 32 and 64 bits during the same execution (run), so only one version of the DLL is available to load from that process. – Ken White Aug 15 '14 at 16:20
  • Is this anything more than a simple "file not found" error? Use SysInternals' Process Monitor to find out where it looked. Or use a full path name to the DLL so it doesn't have to guess at it. – Hans Passant Aug 15 '14 at 16:28
  • Nope. Its not a file not found error. The thing is, "DivisionInject.dll" is compiled with AnyCpu. And the executable copiled too. How is it possible that its throwing this exception if "DivisionInject.dll" is suposed to be loaded at execution time as a 64 bit dll. (im on a 64bit enviroment). I have used DUMPBIN on the DLL and it shows on the HEADER value "machine (x86)". Does this mean this is a 32bit dll only? That would explain the problem, but as im using AnyCpu i thought it could load as a 64bit dll on a 64bit enviroment. – tetsuoito Aug 15 '14 at 18:03

2 Answers2

1

The AnyCpu setting basically means that the determination of how the process or library will be loaded or executed will be based at/on the point of execution/load. A library compiled as AnyCpu will be loadable by both a 32bit and a 64bit calling process. An executable compiled as AnyCpu will run a 64bit process on a 64bit machine and 32bit on a 32bit machine. See this thread for a discussion on the concept of the AnyCpu target.

If your caller is compiled as 64bit and set to AnyCpu, it will be attempting to load the 64bit library.

Community
  • 1
  • 1
codechurn
  • 3,870
  • 4
  • 45
  • 65
  • Thanks for your answer. My caller (in the same solution) is compiled in a 64bit enviroment and set to AnyCpu. I think its attempting to load the 64bit library. That happen to be compiled as AnyCpu too. I donk know why its throwing that exception. The dll is the same =/ – tetsuoito Aug 15 '14 at 18:21
  • The file not found error you are getting could be a path issue. Perhaps check the working path for the assembly via reflection and verify the dependent assembly is there. – codechurn Aug 15 '14 at 20:22
0

While it is now less common, it is possible to install Windows 32-bit on a 64-bit machine. In this case, only exe's built for 32-bit/AnyCpu will run. The converse is obvious. 32-bit machines can only run Windows 32-bit. Thus the O/S bitness is the determining factor, not the hardware.

Windows 32-bit on a 64-bit machine was more common in enterprise shops, that didn't want to support both O/S's or 32-bit legacy applications running on Windows-64.

Andrew Dennison
  • 1,069
  • 11
  • 9