How to get entry assembly path?
/// <summary>
/// Simulate Assembly.GetEntryAssembly that is not supported by the Compact Framework.
/// </summary>
/// <returns>Path and name of first executable as a string.</returns>
public static string GetEntryAssembly()
{
StringBuilder sb = null;
IntPtr moduleHandler = GetCurrentModuleHandle();
if (IntPtr.Zero != moduleHandler)
{
sb = new StringBuilder(255);
if (0 == SafeGetModuleFileName(moduleHandler, sb, sb.Capacity))
{
sb = null;
}
}
return sb.ToString();
}
/// <summary>
/// Safe wrapper for PINVOKE GetModuleHandle.
/// </summary>
/// <returns>A handle to the current module indicates success.</returns>
private static IntPtr GetCurrentModuleHandle()
{
IntPtr moduleName = IntPtr.Zero;
return GetModuleHandle(moduleName);
}
/// <summary>
/// Safe wrapper for PINVOKE GetModuleFileName.
/// </summary>
/// <param name="moduleHandler">Handle to the module whose executable file name is being requested.</param>
/// <param name="moduleName">Pointer to a buffer that is filled in with the path and file name of the given module. </param>
/// <param name="capacity"> Specifies the length, in characters, of the ModuleName buffer.</param>
/// <returns>The length, in characters, of the string copied to the buffer indicates success. </returns>
private static Int32 SafeGetModuleFileName(IntPtr moduleHandler, StringBuilder moduleName, Int32 capacity)
{
if (capacity > 255)
{
capacity = 255;
}
return GetModuleFileName(moduleHandler, moduleName, capacity);
}
/// <summary>
/// PINVOKE that gets a module handle for the specified module if the file has been mapped into the address space of the calling process.
/// </summary>
/// <param name="moduleName">Pointer to a null-terminated string that contains the name of the module, which must be a DLL file.</param>
/// <returns>A handle to the specified module indicates success.</returns>
[System.Runtime.InteropServices.DllImport("CoreDll.dll", SetLastError = true)]
public static extern IntPtr GetModuleHandle(IntPtr moduleName);
/// <summary>
/// PINVOKE that gets a module file name.
/// </summary>
/// <param name="moduleHandler">Handle to the module whose executable file name is being requested.</param>
/// <param name="moduleName">Pointer to a buffer that is filled in with the path and file name of the given module. </param>
/// <param name="capacity"> Specifies the length, in characters, of the ModuleName buffer.</param>
/// <returns>The length, in characters, of the string copied to the buffer indicates success. </returns>
[System.Runtime.InteropServices.DllImport("CoreDll.dll", SetLastError = true)]
public static extern Int32 GetModuleFileName(IntPtr moduleHandler, StringBuilder moduleName, Int32 capacity);
How to set a form foreground?
public static void SetForegroundWindow(Form form)
{
form.Capture = true;
IntPtr hwnd = GetCapture();
form.Capture = false;
SetForegroundWindow(hwnd);
}
[System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError = true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr GetCapture();
How to get memory status?
/// <summary>
/// Retrieves the memory status of the device
/// </summary>
public static MemoryStatus GlobalMemoryStatus()
{
MemoryStatus ms = new MemoryStatus();
GlobalMemoryStatusCE(out ms);
return ms;
}
[System.Runtime.InteropServices.DllImport("coredll", EntryPoint = "GlobalMemoryStatus", SetLastError = false)]
public static extern void GlobalMemoryStatusCE(out MemoryStatus msce);
Below is the MemoryStatus structure used above
/// <summary>
/// Memory Status Structure
/// </summary>
public struct MemoryStatus
{
/// <summary>
/// Length of Structure
/// </summary>
internal int Length;
/// <summary>
/// Specifies a number between 0 and 100 that gives a general idea of current memory utilization, in which 0 indicates no memory use and 100 indicates full memory use.
/// </summary>
public int MemoryLoad;
/// <summary>
/// Indicates the total number of bytes of physical memory.
/// </summary>
public int TotalPhysical;
/// <summary>
/// Indicates the number of bytes of physical memory available.
/// </summary>
public int AvailablePhysical;
/// <summary>
/// Indicates the total number of bytes that can be stored in the paging file. Note that this number does not represent the actual physical size of the paging file on disk.
/// </summary>
public int TotalPageFile;
/// <summary>
/// Indicates the number of bytes available in the paging file.
/// </summary>
public int AvailablePageFile;
/// <summary>
/// Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.
/// </summary>
public int TotalVirtual;
/// <summary>
/// Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.
/// </summary>
public int AvailableVirtual;
}
How to prevent device fall asleep?
/// <summary>
/// This function resets a system timer that controls whether or not the
/// device will automatically go into a suspended state.
/// </summary>
[DllImport("CoreDll.dll")]
public static extern void SystemIdleTimerReset();
No comments:
Post a Comment