I am writing a program to monitor WM_NOTIFY messages in an application so that I can be alerted to updates in the data being displayed. I am currently having problems with my code to write my native DLL to the remote process.
I have the following for my P/Invoke section:
/// <summary>/// The GetModuleHandleEx function retrieves a module handle for the specified module if the file has been mapped into the address space of the calling process.
/// </summary>
/// <param name="dwFlags">This parameter can be one or more of the following values.</param>
/// <param name="lpModuleName">Pointer to a null-terminated string that contains the name of the module (either a .dll or .exe file), or a pointer to an address in the module (if dwFlags is GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS).</param>
/// <param name="phModule">Handle to the specified module.</param>
/// <returns>
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero.
/// </returns>
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, ThrowOnUnmappableChar = true)]
public static extern bool GetModuleHandleEx(UInt32 dwFlags,string lpModuleName,out IntPtr phModule); /// <summary>
/// The GetProcAddress function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
/// </summary>
/// <param name="hModule">Handle to the DLL module that contains the function or variable.</param>
/// <param name="lpProcName">Pointer to a null-terminated string that specifies the function or variable name, or the function's ordinal value.</param>
/// <returns>If the function succeeds, the return value is the address of the exported function or variable.</returns>
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, ThrowOnUnmappableChar = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
My code for using this is as follows:
bResult = GetModuleHandleEx(
GetModuleFlags.GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "Kernel32", out _hKernel32);if (!bResult || _hKernel32 == IntPtr.Zero) throw new ApplicationException("Unable to obtain the handle to the module Kernel32.");
_hLoadLibrary = GetProcAddress(_hKernel32, "LoadLibraryW");
if (_hLoadLibrary == IntPtr.Zero) throw new ApplicationException("Unable to find the LoadLibraryW method in the module Kernel32.");
My problem is that GetProcAddress keeps returning a null pointer. Just in case it's relevant, I am using VS2005 and executing this on WindowsXP.

Problems with GetProcAddress
Inaki Ayucar
Mark,
I am trying to monitor the Windows Message thread on a control in a remote process.
~Jordan
domw001
Hi,
if you just want to monitor the windows messages you can override the WndProc method in your application and watch the message from there. Is that an option for you
Mark.
arikve
Seems as though I found my own solution.
http://pinvoke.net/default.aspx/kernel32.GetProcAddress
I had the method listed as a CharSet.Auto invocation.
One-man high-five!