[Windows Phone 7 Development and Hacking] spavlin:
using System;

using System.Runtime.InteropServices;

namespace Wockets.Utils

{

public class Process

{

const int SYS_HANDLE_BASE = 64;

const int SH_WIN32 = 0;

const int SH_CURTHREAD = 1;

const int SH_CURPROC = 2;

const uint SYSHANDLE_OFFSET = 0x004;

const uint PUserKDataARM = 0xFFFFC800;

const uint PUserKDataX86 = 0x00005800;

public static int GetCurrentProcessID()

{

uint PUserKData = GetCPUArchitecture() == PROCESSOR_ARCHITECTURE.PROCESSOR_ARCHITECTURE_ARM ? PUserKDataARM : PUserKDataX86;

IntPtr pBase = new IntPtr((int)(PUserKData + SYSHANDLE_OFFSET));

return Marshal.ReadInt32(pBase, SH_CURTHREAD * 4);

}

public static int GetCurrentThreadID()

{

uint PUserKData = GetCPUArchitecture() == PROCESSOR_ARCHITECTURE.PROCESSOR_ARCHITECTURE_ARM ? PUserKDataARM : PUserKDataX86;

IntPtr pBase = new IntPtr((int)(PUserKData + SYSHANDLE_OFFSET));

return Marshal.ReadInt32(pBase, SH_CURPROC * 4);

}

public static IntPtr GetCurrentProcess()

{

return new IntPtr(SH_CURPROC + SYS_HANDLE_BASE);

}

public static IntPtr GetCurrentThread()

{

return new IntPtr(SH_CURTHREAD + SYS_HANDLE_BASE);

}

public static PROCESSOR_ARCHITECTURE GetCPUArchitecture()

{

SYSTEM_INFO si = new SYSTEM_INFO();

GetSystemInfo(ref si);

return (PROCESSOR_ARCHITECTURE)si.wProcessorArchitecture;

}

public enum PROCESSOR_ARCHITECTURE

{

PROCESSOR_ARCHITECTURE_INTEL = 0,

PROCESSOR_ARCHITECTURE_MIPS = 1,

PROCESSOR_ARCHITECTURE_ALPHA = 2,

PROCESSOR_ARCHITECTURE_PPC = 3,

PROCESSOR_ARCHITECTURE_SHX = 4,

PROCESSOR_ARCHITECTURE_ARM = 5,

PROCESSOR_ARCHITECTURE_IA64 = 6,

PROCESSOR_ARCHITECTURE_ALPHA64 = 7,

PROCESSOR_ARCHITECTURE_UNKNOWN = 0xFFFF,

}

[DllImport("Coredll")]

public static extern void GetSystemInfo(ref SYSTEM_INFO SystemInfo);

[DllImport("Coredll")]

public static extern int GetThreadPriority(

IntPtr hThread);

[DllImport("Coredll")]

public static extern bool GetThreadTimes(

IntPtr hThread,

out long lpCreationTime,

out long lpExitTime,

out long lpKernelTime,

out long lpUserTime);

}

public struct SYSTEM_INFO

{

public ushort wProcessorArchitecture;

public ushort wReserved;

public uint dwPageSize;

public int lpMinimumApplicationAddress;

public int lpMaximumApplicationAddress;

public uint dwActiveProcessorMask;

public uint dwNumberOfProcessors;

public uint dwProcessorType;

public uint dwAllocationGranularity;

public ushort wProcessorLevel;

public ushort wProcessorRevision;

}

}

---------- Post added at 03:22 PM ---------- Previous post was at 03:19 PM ----------

using System;

using System.IO;

using System.Text;

using System.Runtime.InteropServices;

namespace Wockets.Utils

{

public static class Platform

{

#region Platform Invokes

public static class NativeMethods

{

[DllImport("coredll.dll")]

private static extern int SystemParametersInfo(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWiniIni);

private const uint SPI_GETPLATFORMTYPE = 257;

private const uint SPI_GETOEMINFO = 258;

private static string GetSystemParameter(uint uiParam)

{

StringBuilder sb = new StringBuilder(128);

if (SystemParametersInfo(uiParam, (uint)sb.Capacity, sb, 0) == 0)

throw new ApplicationException("Failed to get system parameter");

return sb.ToString();

}

public static string GetPlatformType()

{

return GetSystemParameter(SPI_GETPLATFORMTYPE);

}

public static string GetOEMInfo()

{

return GetSystemParameter(SPI_GETOEMINFO);

}

// Returns true if the device has a cellular radio

public static bool GetHasRadio()

{

return File.Exists(@"\Windows\Phone.dll");

}

}

#endregion

// Returns true if the application is running on a

// desktop version of the Windows operating system

public static bool IsDesktopWindows

{

get { return Environment.OSVersion.Platform != PlatformID.WinCE; }

}

// Returns true if the application is running on a

// device powered by some form of WIndows CE based

// operating system

public static bool IsWindowsCE

{

get { return Environment.OSVersion.Platform == PlatformID.WinCE; }

}

// Returns true if the application is running on a

// Windows Mobile Standard (i.e. Smartphone) device

public static bool IsSmartPhone

{

get { return IsWindowsCE && (NativeMethods.GetPlatformType() == "SmartPhone"); }

}

// Returns true if the application is running on a

// Windows Mobile Classic (i.e. Pocket PC without

// cellphone) device.

public static bool IsPocketPC

{

get { return IsWindowsCE && !NativeMethods.GetHasRadio() && (NativeMethods.GetPlatformType() == "PocketPC"); }

}

// Returns true if the application is running on a

// Windows Mobile Professional (i.e. Pocket PC with

// cellphone) device.

public static bool IsWindowsMobileProfessional

{

get { return IsWindowsCE && NativeMethods.GetHasRadio() && (NativeMethods.GetPlatformType() == "PocketPC"); }

}

// Returns true if the application is running

// within the Device Emulator on a desktop machine

public static bool IsEmulator

{

get { return IsWindowsCE && (NativeMethods.GetOEMInfo() == "Microsoft DeviceEmulator"); }

}

}

}

---------- Post added at 03:24 PM ---------- Previous post was at 03:22 PM ----------

using System;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using System.Text;

using System.ComponentModel;

namespace Wockets.Utils

{

public class ProcessInfo

{

[DllImport("coredll.dll", SetLastError = true)]

private static extern int GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, int nSize);

private const int INVALID_HANDLE_VALUE = -1;

private IntPtr _pid;

private int _threadCount;

private int _baseAddress;

private int _parentProcessID;

private string _fullPath;

internal ProcessInfo(IntPtr pid, int threadcount, int baseaddress, int parentid)

{

_pid = pid;

_threadCount = threadcount;

_baseAddress = baseaddress;

_parentProcessID = parentid;

StringBuilder sb = new StringBuilder(1024);

GetModuleFileName(_pid, sb, sb.Capacity);

_fullPath = sb.ToString();

}

/// <summary>

/// Returns the full path to the process .EXE file.

/// </summary>

/// <example>"\Program Files\Acme\main.exe"</example>

public override string ToString()

{

return _fullPath;

}

public int BaseAddress

{

get { return _baseAddress; }

}

public int ThreadCount

{

get { return _threadCount; }

}

/// <summary>

/// Returns the Process Id.

/// </summary>

public IntPtr Pid

{

get { return _pid; }

}

/// <summary>

/// Returns the full path to the process .EXE file.

/// </summary>

/// <example>"\Program Files\Acme\main.exe"</example>

public string FullPath

{

get { return _fullPath; }

}

public int ParentProcessID

{

get { return _parentProcessID; }

}

/// <summary>

/// Kills the process.

/// </summary>

/// <exception cref="Win32Exception">Thrown when killing the process fails.</exception>

public void Kill()

{

ProcessCE.Kill(_pid);

}

}

public static class ProcessCE

{

private const int MAX_PATH = 260;

private const int TH32CS_SNAPPROCESS = 0x00000002;

private const int TH32CS_SNAPNOHEAPS = 0x40000000;

private const int INVALID_HANDLE_VALUE = -1;

private const int PROCESS_TERMINATE = 1;

/// <summary>

/// Returns an array with information about running processes.

/// </summary>

///<exception cref="Win32Exception">Thrown when enumerating the processes fails.</exception>

public static ProcessInfo[] GetProcesses()

{

List<ProcessInfo> procList = new List<ProcessInfo>();

IntPtr handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);

if ((Int32)handle == INVALID_HANDLE_VALUE)

throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateToolhelp32Snapshot error.");

try

{

PROCESSENTRY processentry = new PROCESSENTRY();

processentry.dwSize = (uint)Marshal.SizeOf(processentry);

//Get the first process

int retval = Process32First(handle, ref processentry);

while (retval == 1)

{

procList.Add(new ProcessInfo(new IntPtr((int)processentry.th32ProcessID), (int)processentry.cntThreads, (int)processentry.th32MemoryBase, (int)processentry.th32ParentProcessID));

retval = Process32Next(handle, ref processentry);

}

}

finally

{

CloseToolhelp32Snapshot(handle);

}

return procList.ToArray();

}

/// <summary>

/// Checks if the specified .EXE is running.

/// </summary>

/// <param name="fullpath">The full path to an .EXE file.</param>

/// <returns>Returns true is the process is running.</returns>

/// <exception cref="Win32Exception">Thrown when taking a system snapshot fails.</exception>

public static bool IsRunning(string fullpath)

{

return (FindProcessPID(fullpath) != IntPtr.Zero);

}

/// <summary>

/// Finds and kills if the process for the specified .EXE file is running.

/// </summary>

/// <param name="fullpath">The full path to an .EXE file.</param>

/// <returns>True if the process was terminated. False if the process was not found.</returns>

/// <exception cref="Win32Exception">Thrown when opening or killing the process fails.</exception>

public static bool FindAndKill(string fullpath)

{

IntPtr pid = FindProcessPID(fullpath);

if (pid == IntPtr.Zero)

return false;

Kill(pid);

return true;

}

/// <summary>

/// Terminates the process with the specified Process Id.

/// </summary>

/// <param name="pid">The Process Id of the process to kill.</param>

/// <exception cref="Win32Exception">Thrown when opening or killing the process fails.</exception>

internal static void Kill(IntPtr pid)

{

IntPtr process_handle = OpenProcess(PROCESS_TERMINATE, false, (int)pid);

if (process_handle == (IntPtr)INVALID_HANDLE_VALUE)

throw new Win32Exception(Marshal.GetLastWin32Error(), "OpenProcess failed.");

try

{

bool result = TerminateProcess(process_handle, 0);

if (result == false)

throw new Win32Exception(Marshal.GetLastWin32Error(), "TerminateProcess failed.");

}

finally

{

CloseHandle(process_handle);

}

}

/// <summary>

/// Finds the Process Id of the specified .EXE file.

/// </summary>

/// <param name="fullpath">The full path to an .EXE file.</param>

/// <returns>The Process Id to the process found. Return IntPtr.Zero if the process is not running.</returns>

///<exception cref="Win32Exception">Thrown when taking a system snapshot fails.</exception>

public static IntPtr FindProcessPID(string fullpath)

{

fullpath = fullpath.ToLower();

IntPtr snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);

if ((Int32)snapshot_handle == INVALID_HANDLE_VALUE)

return IntPtr.Zero;

try

{

PROCESSENTRY processentry = new PROCESSENTRY();

processentry.dwSize = (uint)Marshal.SizeOf(processentry);

StringBuilder fullexepath = new StringBuilder(1024);

int retval = Process32First(snapshot_handle, ref processentry);

while (retval == 1)

{

IntPtr pid = new IntPtr((int)processentry.th32ProcessID);

// Writes the full path to the process into a StringBuilder object.

// Note: If first parameter is IntPtr.Zero it returns the path to the current process.

GetModuleFileName(pid, fullexepath, fullexepath.Capacity);

if (fullexepath.ToString().ToLower() == fullpath)

return pid;

retval = Process32Next(snapshot_handle, ref processentry);

}

}

finally

{

CloseToolhelp32Snapshot(snapshot_handle);

}

return IntPtr.Zero;

}

[DllImport("toolhelp.dll", SetLastError = true)]

private static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processID);

[DllImport("toolhelp.dll")]

private static extern int CloseToolhelp32Snapshot(IntPtr snapshot);

[DllImport("toolhelp.dll")]

private static extern int Process32First(IntPtr snapshot, ref PROCESSENTRY processEntry);

[DllImport("toolhelp.dll")]

private static extern int Process32Next(IntPtr snapshot, ref PROCESSENTRY processEntry);

[DllImport("coredll.dll", SetLastError = true)]

private static extern int GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, int nSize);

[DllImport("coredll.dll", SetLastError = true)]

private static extern bool TerminateProcess(IntPtr hProcess, uint ExitCode);

[DllImport("coredll.dll", SetLastError = true)]

private static extern IntPtr OpenProcess(int flags, bool fInherit, int PID);

[DllImport("coredll.dll")]

private static extern bool CloseHandle(IntPtr handle);

private struct PROCESSENTRY

{

public uint dwSize;

public uint cntUsage;

public uint th32ProcessID;

public uint th32DefaultHeapID;

public uint th32ModuleID;

public uint cntThreads;

public uint th32ParentProcessID;

public int pcPriClassBase;

public uint dwFlags;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]

public string szExeFile;

public uint th32MemoryBase;

public uint th32AccessKey;

}

} // end of class

}