28 Aralık 2018 Cuma

FindWindow metodu

Örnek
Şöyle yaparız.
HWND hwnd = FindWindowA(NULL, "C:\\Example\\App.exe");
Örnek
Şöyle yaparız.
DWORD get_process_id_by_window_title(const char* window_title_){
  // get a handle to window using the window name
  HWND window_handle = FindWindow(NULL, window_title_);
  if (window_handle == NULL){
    return NULL;
  }

  // return the process id of the window handle we found
  DWORD process_id;
  GetWindowThreadProcessId(window_handle, &process_id);
  return process_id;
}

10 Ekim 2018 Çarşamba

getnameinfo metodu

Örnek
Şöyle yaparız.
struct sockaddr_in saGNI;
char servInfo[NI_MAXSERV];

saGNI.sin_family = AF_INET;
saGNI.sin_addr.s_addr = inet_addr("10.80.101.162");
saGNI.sin_port = htons(64);

char host[512]; // <-- information to obtain
getnameinfo((struct sockaddr *) &saGNI, sizeof(struct sockaddr),
 host, NI_MAXHOST, servInfo, NI_MAXSERV, NI_NUMERICSERV);
Örnek
Şöyle yaparız.
std::string computer_name;
computer_name.resize(NI_MAXHOST);
struct sockaddr_in socket_address = ...;
char service_info[NI_MAXSERV] = {};


getnameinfo((struct sockaddr *) &socket_address,
            sizeof(socket_address),
            &computer_name[0],
            NI_MAXHOST, service_info, NI_MAXSERV, NI_NUMERICSERV);

InetPton metodu

Giriş
Socket programlama için kullanılır.

Örnek
Şöyle yaparız.
const std::string ip_address = ...;
struct sockaddr_in socket_address;
socket_address.sin_family = AF_INET;
const auto result = InetPtonA(AF_INET, &ip_address[0], &socket_address.sin_addr.s_addr);

WSAStartup metodu

Giriş
Socket programlama için kullanılır.

Örnek
Şöyle yaparız.
WSADATA wsa_data;
WSAStartup(MAKEWORD(2, 2), &wsa_data);

1 Ağustos 2018 Çarşamba

GetFileAttributes metodu

Giriş
Şu satırı dahil ederiz.
#include <Windows.h>
Bu metodun tersini SetFileAttributes yapar.

Örnek
Şöyle yaparız.
const char * sharedFolder = "...";
DWORD attFolder = GetFileAttributes(sharedFolder);

LockWorkStation metodu

Örnek
Şöyle yaparız.
using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern bool LockWorkStation();

21 Haziran 2018 Perşembe

RegCreateKeyEx metodu

Giriş
İlk parametre KEY_CURRENT_USER,HKEY_LOCAL_MACHINE olabilir.

Örnek
Şöyle yaparız.
HKEY hKey;
LPCTSTR sk = TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion
\\Image File Execution Options\\notepad.exe"); //notepad.exe is the key I want to create

LONG createResKey = RegCreateKeyEx(HKEY_LOCAL_MACHINE, sk, 0, NULL,
 REG_OPTION_BACKUP_RESTORE, KEY_ALL_ACCESS, NULL, &hKey, NULL);

if (createResKey == ERROR_SUCCESS) {
    qDebug() << "Success creating key.";
}

18 Haziran 2018 Pazartesi

RegFlushKey metodu

Giriş
Açıklaması şöyle. Bu metodu çağırmak pahalı bir işlem.
Calling RegFlushKey is an expensive operation that significantly affects system-wide performance as it consumes disk bandwidth and blocks modifications to all keys by all processes in the registry hive that is being flushed until the flush operation completes. RegFlushKey should only be called explicitly when an application must guarantee that registry changes are persisted to disk immediately after modification. All modifications made to keys are visible to other processes without the need to flush them to disk.
Alternatively, the registry has a 'lazy flush' mechanism that flushes registry modifications to disk at regular intervals of time. In addition to this regular flush operation, registry changes are also flushed to disk at system shutdown. Allowing the 'lazy flush' to flush registry changes is the most efficient way to manage registry writes to the registry store on disk.

4 Haziran 2018 Pazartesi

SetWindowsHookEx metodu

Giriş
Bu metodun tersini UnhookWindowsHookEx yapar.

Örnek - WH_KEYBOARD_LL
Şöyle yaparız.
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;
  ...
  return CallNextHookEx(NULL, nCode, wParam, lParam);
}

HOOKPROC LockerPraq()
{
  HHOOK hook_install = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc,
    NULL, 0);
  if (NULL == hook_install)
    return(0);

  MSG msg;
  while (GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  UnhookWindowsHookEx(hook_install);

  return(0);
}
Örnek
Elimizde şöyle bir kod olsun.
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  if (nCode == HC_ACTION)
  {
    auto &ms = * (const MOUSEHOOKSTRUCT *) lParam;
    if (wParam == WM_MOUSEMOVE)
    {
      cout << "X : " << ms.pt.x << " Y: " << ms.pt.y << "\n";
    }
  }
  return CallNextHookEx(hMSHook, nCode, wParam, lParam);
}
Şöyle yaparız.
int _tmain() {
  HMODULE hInstance = GetModuleHandle(NULL);
  hMSHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, NULL);

  MSG Msg;

  while (GetMessage(&Msg, NULL, 0, 0)) { DispatchMessage(&Msg); }
  ::ReleaseDC(0, dc);

  return 0;
}