Search

Clipboard WIn32 API for C#

PInvoke using the C#, this is a list of Win32 API to work with Clipboard
[DllImport("user32.dll")]
private static extern int OpenClipboard (int hwnd);
[DllImport("user32.dll")]
private static extern int GetClipboardData (int wFormat);
[DllImport("user32.dll", EntryPoint="GetClipboardFormatNameA")]
private static extern int GetClipboardFormatName (int wFormat, string lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetClipboardOwner ();
[DllImport("user32.dll")]
private static extern int GetClipboardSequenceNumber ();
[DllImport("user32.dll")]
private static extern int GetClipboardViewer ();
[DllImport("kernel32.dll")]
private static extern int GlobalAlloc (int wFlags, int dwBytes);
[DllImport("kernel32.dll")]
private static extern int GlobalLock (int hMem);
[DllImport("kernel32.dll", EntryPoint="lstrcpyA")]
private static extern int lstrcpy (string lpString1, string lpString2);
[DllImport("kernel32.dll")]
private static extern int GlobalUnlock (int hMem);
[DllImport("user32.dll")]
private static extern int CloseClipboard ();
[DllImport("user32.dll")]
private static extern int SetClipboardData (int wFormat, int hMem);
[DllImport("user32.dll")]
private static extern int EmptyClipboard ();

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
   }






  }
 }
}

Finding Prime number in C#

static bool Primer(ulong x)
{
 bool result = true;
 ulong i = 2;

 while (i <= x / 2)
 {
  if (x % i == 0)
  {
   result = false;
   break;
  }
  else{
   i++;
  }
  if (x % i == 0)
  result = false;
 return result;
 }
}

webcam access through win32 API in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace mycam
{
public class Cam
{
[DllImport("kernel32.dll")]
private static extern void Sleep(int dwMilliseconds);
[DllImport("kernel32.dll")]
private static extern int Beep(int dwFreq, int dwDuration);
[DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowW")]
private static extern int capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);
private const int WS_VISIBLE = 0x10000000;
private const int WS_CHILD = 0x40000000;
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
private static extern int SendMessageFL(int hwnd, int wMsg, int wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
private static extern int SendMessageSD(int hwnd, int wMsg, string wParam, int lParam);
private const int WM_USER = 0x400;
private const int WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10);
private const int WM_CAP_START = WM_USER;
private const int WM_CAP_FILE_SAVEDIBA = (WM_CAP_START + 25);
private const int WM_CAP_SET_SCALE = WM_USER + 53;
private const int WM_CAP_SET_PREVIEW = WM_USER + 50;
private const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
private const int WM_CAP_FILE_SAVEDIB = WM_USER + 25;
private const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;
private string _camtitle;// = "HDCam";// pointer
private int hWebcam;
private const int nDevice = 0;
private const int nFPS = 50;
private string _filename;// = "IMAGE.BMP";
public int getCAM(string cam_title,int cam_x,int cam_y,int cam_width,int cam_height,IntPtr HWNDparent,int cam_ID)
{
//Beep(2000, 50);
_camtitle = cam_title;
hWebcam = capCreateCaptureWindow(cam_title, WS_VISIBLE + WS_CHILD, cam_x, cam_y,cam_width,cam_height, HWNDparent.ToInt32(), cam_ID);
//Sleep(5000);
//inSequence(sender ,e);
return hWebcam;
}
public void makeBEEP(int FREQUENCY)
{
Beep(FREQUENCY, 50);
}
public string NewFileNAME()
{
DateTime DT = new DateTime();
DT.Date.ToString();
return "File" + DT.Date.ToString();
}
public void startCAM()
{
//makeBEEP(3000);
SendMessage(hWebcam, WM_CAP_DRIVER_CONNECT, nDevice, 0);
SendMessage(hWebcam, WM_CAP_SET_SCALE, 1, 0);
SendMessage(hWebcam, WM_CAP_SET_PREVIEWRATE, nFPS, 0);
SendMessage(hWebcam, WM_CAP_SET_PREVIEW, 1, 0);
}
public void captureCAM(string BMPfilename)
{
_filename = BMPfilename;
//string flnm = NewFileNAME();
//this.Text = flnm;
//makeBEEP(3000);
//SendMessageS(hWebcam, WM_CAP_FILE_SAVEDIBA, 0,_filename);
SendMessageFL(hWebcam, WM_CAP_FILE_SAVEDIBA, 0, _filename);
makeBEEP(3500);
//}
}
public void stopCAM()
{
//SendMessage(hWebcam, WM_CAP_DRIVER_DISCONNECT, _camtitle, 0);
SendMessageSD(hWebcam, WM_CAP_DRIVER_DISCONNECT, _camtitle, 0);
}
}
}

HTTP GET Request generator (C++)

HTTP GET Request generator isa a C++ code to generate the GET request
// HTTP GET Request generator (C++)
#include
#include
#include
BOOL InitConnection(SOCKET *wSock, char *SERV);
BOOL InitWSA();
BOOL InitSocket(SOCKET *wSock);
DWORD WINAPI RecvData(LPVOID* wSock);
int main(int argc, char** argv)
{
if(argc != 3)
{
std::cout << "Usage: www.site.com [number_of_attacks]n";
std::cout << "Example: main.exe www.cowgirls.com 100n";
exit(1);
}
SOCKET tehRock;
int number = atoi(argv[2]);
char *site = argv[1];
char buffert[65000];
//CreateThread(NULL, 0, LPTHREAD_START_ROUTINE(RecvData), (LPVOID)tehRock, 0, NULL);
for(int i = 0; i < number; i++)
{
if(InitWSA() == true)
std::cout << "WS2_32.DLL loadedn";
Sleep(20);
if(InitSocket(&tehRock) == true)
std::cout << "Socket created.n";
Sleep(20);
if(InitConnection((SOCKET*)tehRock, site) == true)
std::cout << "Connected.n";
Sleep(20);
send(tehRock, "GET / HTTP/1.0rnrn", 19, 0);
std::cout << "Sending HTTP GET REQUESTn";
while(recv(tehRock, buffert, sizeof(buffert), 0) > 0)
std::cout << buffert;
}
}
DWORD WINAPI RecvData(LPVOID* wSock)
{
SOCKET socket = (SOCKET)wSock;
char data[65356];
ZeroMemory(&data, sizeof(data));
std::cout << "Thread successfully created.n";
while(1)
{
if(recv(socket, data, sizeof(data), 0) > 0)
std::cout << data;
Sleep(1);
}
}
BOOL InitConnection(SOCKET *wSock, char *SERV)
{
int port = 80;
struct hostent *host;
struct sockaddr_in sin;
int error;
host = gethostbyname(SERV);
memset( &sin, 0, sizeof sin );
sin.sin_family = AF_INET;
sin.sin_addr = *((in_addr *)host->h_addr);
sin.sin_port = htons(port);
error = connect((SOCKET)wSock, (sockaddr*)&sin, sizeof sin);
if(error != 0)
return false;
return true;
}
BOOL InitSocket(SOCKET *wSock)
{
*wSock = socket(AF_INET, SOCK_STREAM, 0);
if((SOCKET)wSock == INVALID_SOCKET)
return false;
return true;
}
BOOL InitWSA()
{
WSADATA wsaData;
WORD version;
version = MAKEWORD(2, 2);
int error;
error = WSAStartup(version, &wsaData);
if(error != 0)
return false;
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
return false;
}
return true;
}

WMI: Get User Name

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo "User Name = " & objComputer.UserName _
& VBNewLine & "Computer Name = " & objComputer.Name
WScript.Echo objComputer.UserName
Next