27 Nisan 2020 Pazartesi

MZ Executable Format - DOS Tarafından Kullanılırdı

Giriş
Açıklaması şöyle
Executable content is identified by its MZ and PE signatures.
MZ imzası dosyanın başında bulunur. Açıklaması şöyle.
Most large files (over 64KiB) with a .COM extension are really MZ executables; the DOS loader doesn’t care whether the extension is .EXE or .COM, it uses the MZ signature to identify the format. This is the only documented way for a .COM file larger than 64KiB to work, so it’s the only approach which can be relied upon.
MZ formatında uygulamanın hangi işlemcide çalıştığı bilgisi bulunmaz. Açıklaması şöyle
The original DOS "MZ" type executable header do not contain such information about what kind of code it contains or what CPU type it needs. It just contains a binary image that is loaded to memory and information about how to start it in real mode, so there are no separate 16-bit or 32-bit binaries.

RegisterClassEx metodu

Giriş
WNDCLASSEX tipinde parametre alır
Örnek
WNDCLASSEX yapısını doldurduktan sonra şöyle yaparız
if (!RegisterClassEx(&win_class))
{
  OutputDebugString(L"Registering Class Failed");
  return false;
}
Bu çağrıdan sorna CreateWindow() ve ShowWindow() çağrılarını da yaparsak penceremiz gösterilir.
Şöyle yaparız
LPCWSTR m_wintitle =  L"DirectX";
DWORD m_winstyles = WS_OVERLAPPEDWINDOW;

RECT r = {0,0,m_winwidth,m_winheight};

AdjustWindowRect(&r, m_winstyles, FALSE);

uint_fast16_t f_width = r.right - r.left;
uint_fast16_t f_height = r.bottom - r.top;

uint_fast16_t f_width_final = GetSystemMetrics(SM_CXSCREEN) / 2 - f_width / 2);
uint_fast16_t f_height_final = (GetSystemMetrics(SM_CYSCREEN) / 2 - f_height / 2);

HWND hwnd = CreateWindow(L"dx_main_class", m_wintitle, m_winstyles, f_width_final,
f_height_final, f_width, f_height, NULL,   NULL, m_hinstance, NULL);

ShowWindow(m_hwnd, SW_SHOW);
WNDCLASSEX Alanları
Şöyle yaparız
WNDCLASSEX win_class;
win_class.cbSize = sizeof(WNDCLASSEX);
win_class.style = CS_HREDRAW | CS_VREDRAW;
win_class.lpfnWndProc = WINDOWPROC;
win_class.cbClsExtra = 0;
win_class.cbWndExtra = 0;
win_class.hInstance = m_hinstance;
win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
win_class.hCursor = 0;
win_class.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
win_class.lpszMenuName = NULL;
win_class.lpszClassName = L"dx_main_class";
win_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
cbSize Alanı
Açıklaması şöyle
The WNDCLASSEX structure is similar to the WNDCLASS structure. There are two differences. WNDCLASSEX includes the cbSize member, which specifies the size of the structure [...]
Örnek
Şöyle yaparız.
// Register the windows class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";


WNDCLASSEX wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.cbSize = sizeof wc;

RegisterClassEx(&wc);
hInstance Alanı
WinMain metoduna geçen parametre olmalıdır
Örnek
Şöyle yaparız
int WINAPI wWinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    PWSTR pCmdLine,
    int nCmdShow)
{

  WNDCLASSEX wc = {};
  wc.hInstance = hInstance;
  ...
  RegisterClassEx(&wc);
  ...
  return 0;
}
lpfnWndProc Alanı
Gelen mesajları işleyen koddur. WindowProc CallBack yazısına taşıdım

23 Nisan 2020 Perşembe

WriteFile metodu

Giriş
İmzası şöyle.
 WriteFile(HANDLE hFile,
           LPCVOID lpBuffer,
           DWORD nNumberOfBytesToWrite,
           LPDWORD lpNumberOfBytesWritten,
           LPOVERLAPPED lpOverlapped
          );
Açıklaması şöyle
Don't use the WIN API WriteFile to try and securely delete. Instead use a secure delete tool like SysInternals sdelete.

If you just use WriteFile the operating system/file system has the option of writing the new data (e.g., a bunch of null bytes or whatever) to a new block on the disk and then updating the master file table to point to the new block not the old block. You will not be assured that you have overwritten the original file data (unless you use WriteFile to, say, write a file as large as the entire disk).
Örnek
Elimizde şöyle bir kod olsun.
typedef struct
{
    int len;
    char data[1];
}Message;
Şöyle yaparız.
OVERLAPPED osWriteS = { 0 };
osWriteS.Offset = 0;
osWriteS.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

DWORD bytesSent;

if (WriteFile(serialPortHandle,message.data,message.len,&bytesSent, &osWriteS) == false)
{
  _cprintf("bytes sent before overlap: %x\n", bytesSent);
  ...
}