PInvoke Declarations for My Sample Code
Thanks to Ed from Comcast for pointing out that my 1/17/06 post regarding Windows Forms ID's was missing the NativeMethods.cs file. Sorry for the inconvenience, Ed. Here's the NativeMethods.cs file that will help you get the code up and running.
using System;
using System.Runtime.InteropServices;
namespace GetWinFormsId
{
/// <summary>
/// Summary description for NativeMethods.
/// </summary>
public class NativeMethods
{
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle,
uint dwProcessId);
[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
UIntPtr dwSize, uint flAllocationType, PageProtection flProtect);
[DllImport("user32.dll", SetLastError=true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("kernel32.dll")]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
UIntPtr dwSize, uint dwFreeType);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint
dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow,
UIntPtr dwNumberOfBytesToMap);
[DllImport("kernel32.dll")]
public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr CreateFileMapping(IntPtr hFile,
IntPtr lpFileMappingAttributes, PageProtection flProtect, int dwMaximumSizeHigh,
int dwMaximumSizeLow, string lpName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
[Out] byte [] lpBuffer, UIntPtr nSize, IntPtr lpNumberOfBytesRead);
[DllImport("Kernel32.dll", EntryPoint="RtlMoveMemory", SetLastError=false)]
public static extern void MoveMemoryFromByte(IntPtr dest, ref byte src, int size);
[DllImport("Kernel32.dll", EntryPoint="RtlMoveMemory", SetLastError=false)]
public static extern void MoveMemoryToByte(ref byte dest, IntPtr src, int size);
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern int RegisterWindowMessage(string lpString);
//=========== Win95/98/ME Shared memory staff===============
public const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
public const short SECTION_QUERY = 0x1;
public const short SECTION_MAP_WRITE = 0x2;
public const short SECTION_MAP_READ = 0x4;
public const short SECTION_MAP_EXECUTE = 0x8;
public const short SECTION_EXTEND_SIZE = 0x10;
public const int SECTION_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_WRITE | SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_EXTEND_SIZE;
public const int FILE_MAP_ALL_ACCESS = SECTION_ALL_ACCESS;
//============NT Shared memory constant======================
public const short PROCESS_VM_OPERATION = 0x8;
public const short PROCESS_VM_READ = 0x10;
public const short PROCESS_VM_WRITE = 0x20;
public const long PROCESS_ALL_ACCESS = 0x1F0FFF;
public const short MEM_COMMIT = 0x1000;
public const short MEM_RESERVE = 0x2000;
public const short MEM_DECOMMIT = 0x4000;
public const int MEM_RELEASE = 0x8000;
public const int MEM_FREE = 0x10000;
public const int MEM_PRIVATE = 0x20000;
public const int MEM_MAPPED = 0x40000;
public const int MEM_TOP_DOWN = 0x100000;
public const int INVALID_HANDLE_VALUE = -1;
}
[Flags]
public enum PageProtection : uint
{
NoAccess = 0x01,
Readonly = 0x02,
ReadWrite = 0x04,
WriteCopy = 0x08,
Execute = 0x10,
ExecuteRead = 0x20,
ExecuteReadWrite = 0x40,
ExecuteWriteCopy = 0x80,
Guard = 0x100,
NoCache = 0x200,
WriteCombine = 0x400,
}
}
Comments
- Anonymous
November 15, 2006
does this work under win64 environment?id : venukunchala@gmail.comThanks - Anonymous
January 19, 2007
Yes, this code is 64-bit-enabled.Sorry, I didn't see this question earlier. I think I have it set up to email me when comments are posted now.-Bri