Advertisement
krishean

simple_window.php

Mar 5th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.32 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4. set_time_limit(120);
  5.  
  6. $kernel32 = FFI::cdef("
  7. typedef const char *    LPCSTR;
  8. typedef void *          HINSTANCE;
  9. typedef HINSTANCE       HMODULE;
  10. HMODULE GetModuleHandleA(LPCSTR lpModuleName);
  11. ", "kernel32.dll");
  12.  
  13. $user32 = FFI::cdef("
  14. typedef int             BOOL;
  15. typedef void            VOID;
  16. typedef VOID            *LPVOID;
  17. typedef void *          HANDLE;
  18. typedef void *          HINSTANCE;
  19. typedef HANDLE          HMENU;
  20. typedef HANDLE          HWND;
  21. typedef const char *    LPCSTR;
  22. typedef unsigned int    UINT;
  23. typedef unsigned char   BYTE;
  24. typedef long            LONG;
  25. typedef unsigned short  WORD;
  26. typedef unsigned long   DWORD;
  27. typedef WORD            ATOM;
  28. typedef unsigned int    UINT_PTR, *PUINT_PTR;
  29. typedef long            LONG_PTR, *PLONG_PTR;
  30. typedef UINT_PTR        WPARAM;
  31. typedef LONG_PTR        LPARAM;
  32. typedef LONG_PTR        LRESULT;
  33. typedef HANDLE          HBRUSH;
  34. typedef HANDLE          HICON;
  35. typedef HICON           HCURSOR;
  36. typedef LONG (__stdcall* WNDPROC)(HWND, UINT, WPARAM, LONG);
  37. typedef struct tagPOINT {
  38.    LONG x;
  39.    LONG y;
  40. } POINT, *PPOINT;
  41. typedef struct tagMSG {
  42.    HWND   hwnd;
  43.    UINT   message;
  44.    WPARAM wParam;
  45.    LPARAM lParam;
  46.    DWORD  time;
  47.    POINT  pt;
  48.    DWORD  lPrivate;
  49. } MSG, *PMSG, *NPMSG, *LPMSG;
  50. typedef struct tagWNDCLASSEXA {
  51.    UINT      cbSize;
  52.    UINT      style;
  53.    WNDPROC   lpfnWndProc;
  54.    int       cbClsExtra;
  55.    int       cbWndExtra;
  56.    HINSTANCE hInstance;
  57.    HICON     hIcon;
  58.    HCURSOR   hCursor;
  59.    UINT      hbrBackground;
  60.    LPCSTR    lpszMenuName;
  61.    HANDLE    lpszClassName;
  62.    HICON     hIconSm;
  63. } WNDCLASSEXA, *PWNDCLASSEXA, *NPWNDCLASSEXA, *LPWNDCLASSEXA;
  64. HWND CreateWindowExA(DWORD,LPCSTR,LPCSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID);
  65. LRESULT DefWindowProcA(HWND,UINT,WPARAM,LPARAM);
  66. BOOL DestroyWindow(HWND);
  67. LRESULT DispatchMessageA(const MSG*);
  68. BOOL GetMessageA(PMSG,HWND,UINT,UINT);
  69. HICON LoadIconA(HINSTANCE,long);
  70. HCURSOR LoadCursorA(HINSTANCE,long);
  71. int MessageBoxA(HWND,LPCSTR,LPCSTR,UINT);
  72. BOOL PostMessageA(HWND,UINT,WPARAM,LPARAM);
  73. void PostQuitMessage(int);
  74. ATOM RegisterClassExA(const WNDCLASSEXA*);
  75. BOOL UpdateWindow(HWND);
  76. BOOL ShowWindow(HWND,int);
  77. BOOL TranslateMessage(const MSG*);
  78. ", "user32.dll");
  79.  
  80. define('IDI_APPLICATION', 32512);
  81. define('CS_OWNDC', 0x0020);
  82. define('IDI_WINLOGO', 32517);
  83. define('IDC_ARROW', 32512);
  84. define('MB_OK', 0);
  85. define('WM_PAINT', 15);
  86. define('WM_SIZE', 5);
  87. define('WM_CHAR', 258);
  88. define('WM_CLOSE', 16);
  89. define('WM_DESTROY', 2);
  90. define('WS_OVERLAPPEDWINDOW', 0xcf0000);
  91. define('WS_CLIPSIBLINGS', 0x4000000);
  92. define('WS_CLIPCHILDREN', 0x2000000);
  93. define('MB_ICONEXCLAMATION', 0x30);
  94. define('WS_EX_CLIENTEDGE', 512);
  95. define('CW_USEDEFAULT', 0x80000000);
  96. define('COLOR_WINDOW', 5);
  97. define('SW_SHOW', 5);
  98. define('SW_SHOWDEFAULT', 10);
  99. define('SW_SHOWNORMAL', 1);
  100.  
  101. // Step 4: the Window Procedure
  102. $WndProc = function($hwnd, $msg, $wParam, $lParam){
  103.     global $user32;
  104.     switch($msg){
  105.         case WM_CLOSE:
  106.             $user32->DestroyWindow($hwnd);
  107.             break;
  108.         case WM_DESTROY:
  109.             $user32->PostQuitMessage(0);
  110.             break;
  111.         default:
  112.             return $user32->DefWindowProcA($hwnd, $msg, $wParam, $lParam);
  113.     }
  114.     return 0;
  115. };
  116.  
  117. exit(main());
  118. function main(){
  119.     global $kernel32, $user32, $WndProc;
  120.    
  121.     $g_szClassName = "myWindowClass";
  122.     $szClassName = FFI::new(FFI::arrayType(FFI::type("char"), [strlen($g_szClassName)]));
  123.     FFI::memcpy($szClassName, $g_szClassName, strlen($g_szClassName));
  124.    
  125.     $hInstance = $kernel32->GetModuleHandleA(NULL);
  126.    
  127.     //$nCmdShow = SW_SHOW;
  128.     //$nCmdShow = SW_SHOWDEFAULT;
  129.     $nCmdShow = SW_SHOWNORMAL;
  130.    
  131.     $wc = $user32->new("WNDCLASSEXA");
  132.     $msg = $user32->new("MSG");
  133.    
  134.     //Step 1: Registering the Window Class
  135.     $wc->cbSize        = FFI::sizeof($wc);
  136.     $wc->style         = 0;
  137.     $wc->lpfnWndProc   = $WndProc;
  138.     $wc->cbClsExtra    = 0;
  139.     $wc->cbWndExtra    = 0;
  140.     $wc->hInstance     = $hInstance;
  141.     $wc->hIcon         = $user32->LoadIconA(NULL, IDI_APPLICATION);
  142.     $wc->hCursor       = $user32->LoadCursorA(NULL, IDC_ARROW);
  143.     $wc->hbrBackground = (COLOR_WINDOW+1);
  144.     $wc->lpszMenuName  = NULL;
  145.     $wc->lpszClassName = FFI::addr($szClassName);
  146.     $wc->hIconSm       = $user32->LoadIconA(NULL, IDI_APPLICATION);
  147.    
  148.     if(!$user32->RegisterClassExA(FFI::addr($wc))){
  149.         $user32->MessageBoxA(NULL, "Window Registration Failed!", "Error!",
  150.             MB_ICONEXCLAMATION | MB_OK);
  151.         return 0;
  152.     }
  153.    
  154.     // Step 2: Creating the Window
  155.     $hwnd = $user32->CreateWindowExA(
  156.         WS_EX_CLIENTEDGE,
  157.         $g_szClassName,
  158.         "The title of my window",
  159.         WS_OVERLAPPEDWINDOW,
  160.         CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
  161.         NULL, NULL, $hInstance, NULL);
  162.    
  163.     if($hwnd == NULL){
  164.         $user32->MessageBoxA(NULL, "Window Creation Failed!", "Error!",
  165.             MB_ICONEXCLAMATION | MB_OK);
  166.         return 0;
  167.     }
  168.    
  169.     $user32->ShowWindow($hwnd, $nCmdShow);
  170.     $user32->UpdateWindow($hwnd);
  171.    
  172.     // Step 3: The Message Loop
  173.    while($user32->GetMessageA(FFI::addr($msg), NULL, 0, 0) > 0){
  174.         $user32->TranslateMessage(FFI::addr($msg));
  175.         $user32->DispatchMessageA(FFI::addr($msg));
  176.     }
  177.    
  178.     return $msg->wParam;
  179. }
  180.  
  181. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement