Search

Using Base Api (Win32 API) in C#

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.Compatibility;
using Microsoft.VisualBasic.Compatibility.VB6;

namespace IndiLogiX
{
 namespace Native32
 {
  public class Native32
  {

   //=========================================================
   // === External Consts: ===
   public const int vbString = 8;

   public static IntPtr GetByteFromString(String Buf)
   {
    IntPtr pBuf = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(Buf);
    return pBuf;
   }
   public static void GetStringFromByte(ref String Buf, IntPtr pBuf)
   {
    Buf = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(pBuf);
   }

   // General API functions.

   [System.Runtime.InteropServices.DllImport("shell32.dll", EntryPoint = "ShellAboutA")]
   private static extern unsafe int ShellAbout(int hwnd, IntPtr szApp, IntPtr szOtherStuff, int hIcon);
   private unsafe int ShellAboutWrp(int hwnd, string szApp, string szOtherStuff, int hIcon)
   {
    int ret;
    IntPtr pszApp = GetByteFromString(szApp);
    IntPtr pszOtherStuff = GetByteFromString(szOtherStuff);

    ret = ShellAbout(hwnd, pszApp, pszOtherStuff, hIcon);

    //VBtoConverter.GetStringFromByte(ref szApp, pszApp);
    //VBtoConverter.GetStringFromByte(ref szOtherStuff, pszOtherStuff);

    return ret;
   }



   const int HWND_TOPMOST = -1;
   const int SWP_NOACTIVATE = 0x10;
   const int SWP_SHOWWINDOW = 0x40;
   const int SWP_HIDEWINDOW = 0x80;
   const int SWP_NOZORDER = 0x4;
   const int SWP_NOMOVE = 0x2;
   const int SWP_NOREPOSITION = 0x200;
   const int SWP_NOSIZE = 0x1;

   [System.Runtime.InteropServices.DllImport("user32")]
   private static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int wFlags);

   [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "FindWindowA")]
   private static extern unsafe int FindWindow(IntPtr lpClassName, IntPtr lpWindowName);
   private unsafe int FindWindowWrp(string lpClassName, string lpWindowName)
   {
    int ret;
    IntPtr plpClassName = GetByteFromString(lpClassName);
    IntPtr plpWindowName = GetByteFromString(lpWindowName);

    ret = FindWindow(plpClassName, plpWindowName);

    //VBtoConverter.GetStringFromByte(ref lpClassName, plpClassName);
    //VBtoConverter.GetStringFromByte(ref lpWindowName, plpWindowName);

    return ret;
   }


   [System.Runtime.InteropServices.DllImport("user32")]
   private static extern int GetForegroundWindow();

   [System.Runtime.InteropServices.DllImport("user32")]
   private static extern int GetParent(int hwnd);

   [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetWindowTextLengthA")]
   private static extern int GetWindowTextLength(int hwnd);

   [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetWindowTextA")]
   private static extern unsafe int GetWindowText(int hwnd, IntPtr lpString, int cch);
   private unsafe int GetWindowTextWrp(int hwnd, ref string lpString, int cch)
   {
    int ret;
    IntPtr plpString = GetByteFromString(lpString);

    ret = GetWindowText(hwnd, plpString, cch);

    GetStringFromByte(ref lpString, plpString);

    return ret;
   }


   [System.Runtime.InteropServices.DllImport("advapi32.dll")]
   private static extern unsafe int GetUserNameA(IntPtr lpBuffer, int* nSize);
   private unsafe int GetUserNameAWrp(ref string lpBuffer, ref int nSize)
   {
    int ret;
    IntPtr plpBuffer = GetByteFromString(lpBuffer);

    fixed (int* pnSize = &nSize)
    {
     ret = GetUserNameA(plpBuffer, pnSize);
    }

    GetStringFromByte(ref lpBuffer, plpBuffer);

    return ret;
   }


   private int TaskBarhWnd;


   // Exit's windows with one of the following results.
   // dwReserved = 0
   [System.Runtime.InteropServices.DllImport("user32")]
   private static extern int ExitWindowsEx(int uFlags, int dwReserved);

   const int EXIT_LOGOFF = 0;
   const int EXIT_SHUTDOWN = 1;
   const int EXIT_REBOOT = 2;

   [System.Runtime.InteropServices.DllImport("kernel32")]
   private static extern unsafe int GetComputerNameA(IntPtr lpBuffer, int* nSize);
   private unsafe int GetComputerNameAWrp(ref string lpBuffer, ref int nSize)
   {
    int ret;
    IntPtr plpBuffer = GetByteFromString(lpBuffer);

    fixed (int* pnSize = &nSize)
    {
     ret = GetComputerNameA(plpBuffer, pnSize);
    }

    GetStringFromByte(ref lpBuffer, plpBuffer);

    return ret;
   }


   // General API functions. (with no VBasic wrapper)

   // Puts the app to sleep for the given number of milliseconds
   [System.Runtime.InteropServices.DllImport("kernel32")]
   private static extern void Sleep(int dwMilliseconds);

   public void ExitWindows(int uFlags)
   {
#if def_ExitWindows
   ExitWindowsEx(uFlags, 0);
#endif // def_ExitWindows
   }


   public string GetUserName()
   {
    string GetUserName = "";
#if def_GetUserName
    char[] UserName = new char[255];

   GetUserNameAWrp(ref UserName, ref 255);
   GetUserName = Strings.Left(UserName, Strings.InStr(UserName, Chr(0), CompareMethod.Text)-1);
#endif // def_GetUserName
    return GetUserName;
   }
   // 
   // Returns the computer's name
   // 
   public string GetComputerName()
   {
    string GetComputerName = "";
#if def_GetComputerName
    char[] UserName = new char[255];

   GetComputerNameAWrp(ref UserName, ref 255);
   GetComputerName = Strings.Left(UserName, Strings.InStr(UserName, Chr(0), CompareMethod.Text)-1);
#endif // def_GetComputerName
    return GetComputerName;
   }

   // 
   // Returns the title of the active window.
   // if GetParent = true then the parent window is
   // returned.
   // 
   public string GetActiveWindowTitle(bool ReturnParent)
   {
    string GetActiveWindowTitle = "";
#if def_GetActiveWindowTitle
    int i;
    int j;

   i = GetForegroundWindow;


   if (ReturnParent) {
    while (i!=0) {
     j = i;
     i = GetParent(i);
    }

    i = j;
   }

   GetActiveWindowTitle = GetWindowTitle(i);
#endif // def_GetActiveWindowTitle
    return GetActiveWindowTitle;
   }

   public void HideTaskBar()
   {
#if def_HideTaskBar
   TaskBarhWnd = FindWindowWrp("Shell_traywnd", "");
   if (TaskBarhWnd!=0) {
    SetWindowPos(TaskBarhWnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);
   }
#endif // def_HideTaskBar
   }
   public void ShowTaskBar()
   {
#if def_ShowTaskBar
   if (TaskBarhWnd!=0) {
    SetWindowPos(TaskBarhWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
   }
#endif // def_ShowTaskBar
   }
   // 
   // Returns the handle of the active window.
   // if GetParent = true then the parent window is
   // returned.
   // 
   public int GetActiveWindow(bool ReturnParent)
   {
    int GetActiveWindow = 0;
#if def_GetActiveWindow
    int i;
    int j;

   i = GetForegroundWindow;


   if (ReturnParent) {
    while (i!=0) {
     j = i;
     i = GetParent(i);
    }

    i = j;
   }

   GetActiveWindow = i;
#endif // def_GetActiveWindow
    return GetActiveWindow;
   }


   public string GetWindowTitle(int hwnd)
   {
    string GetWindowTitle = "";
#if def_GetWindowTitle
    int l;
    string s = "";

   l = GetWindowTextLength(hwnd);
   s = Strings.Space(l+1);

   GetWindowTextWrp(hwnd, ref s, l+1);

   GetWindowTitle = Strings.Left(s, l);
#endif // def_GetWindowTitle
    return GetWindowTitle;
   }

   // 
   // Makes a form the top window if top = True.  When top = False it removes
   // this property.
   // 
   // Public Sub TopMostForm(f As Form, Top As Boolean)
   // If Top Then
   // SetWindowPos f.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE
   // Else
   // SetWindowPos f.hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE
   // End If
   // End Sub

   // 
   // Sleeps for a given number of seconds.
   // 
   public void Pause(float seconds)
   {
#if def_Pause
   Sleep(Math.Floor(Convert.ToDouble(seconds*1000.0)));
#endif // def_Pause
   }

   // 
   // Generates a standard windows About box.
   // 
   public void AboutBox(object frm)
   {
    //AboutBox(frm, null);
   }
   public void AboutBox(object frm, ref object copyright)
   {
#if def_AboutBox
   if (VarType(copyright)==VBtoConverter.vbString) {
    ShellAboutWrp(frm.hwnd, App.ProductName, copyright, frm.Icon);
   } else {
    ShellAboutWrp(frm.hwnd, App.ProductName, "", frm.Icon);
   }
#endif // def_AboutBox
   }






  }
 }
}