2018年11月13日 星期二

Undocumented MessageBoxTimeOut function 時間 倒數 訊息


http://www.delphibasics.info/home/delphibasicssnippets

Undocumented MessageBoxTimeOut function
function MessageBoxTimeOut( 
      hWnd: HWND; lpText: PChar; lpCaption: PChar; 
      uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall;
function MessageBoxTimeOutA( 
      hWnd: HWND; lpText: PChar; lpCaption: PChar; 
      uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall;

function MessageBoxTimeOutW( 
      hWnd: HWND; lpText: PWideChar; lpCaption: PWideChar; 
      uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall;

implementation 

// this const is not defined in Windows.pas 
const 
  MB_TIMEDOUT = 32000; 

function MessageBoxTimeOut; externaluser32 name 'MessageBoxTimeoutA'; 
function MessageBoxTimeOutA; external user32 name 'MessageBoxTimeoutA'; 
function MessageBoxTimeOutW; external user32 name 'MessageBoxTimeoutW';


var 
  iResult: Integer; 
  iFlags: Integer; 
begin 
  // Define a MessagBox  with an OK button and a timeout of 2 seconds 
  iFlags  := MB_OK or MB_SETFOREGROUND or MB_SYSTEMMODAL or MB_ICONINFORMATION;
  iResult := MessageBoxTimeout( 
   Application.Handle, 
   'Test a timeout of 2 seconds.', 
   'MessageBoxTimeout Test', iFlags, 0, 2000); 

  // iResult will = 1 (IDOK) 
  ShowMessage(IntToStr(iRet)); 

  // Define a MessageBox with a Yes and No button and a timeout of 5 seconds 
  iFlags  := MB_YESNO or MB_SETFOREGROUND or MB_SYSTEMMODAL or MB_ICONINFORMATION; 

  iResult := MessageBoxTimeout( 
    Application.Handle, 
    'Test a timeout of 5 seconds.', 
    'MessageBoxTimeout Test', iFlags, 0, 5000); 

  // iResult = MB_TIMEDOUT if no buttons clicked, otherwise 
  // iResult will return the value of the button clicked 

  case iResult of 
  IDYES:  // Pressed Yes button 
    ShowMessage('Yes'); 

  IDNO:  // Pressed the No button 
    ShowMessage('No'); 

  MB_TIMEDOUT: // MessageBox timed out 
    ShowMessage('TimedOut'); 
  end; 
end;

Using NtDeleteFile from Delphi 刪除 檔案



Using NtDeleteFile from Delphi

The required structures (not defined in Delphi) are UNICODE_STRING and OBJECT_ATTRIBUTES.
function NtDeleteFile(ObjectAttributes:POBJECT_ATTRIBUTES):DWORD; stdcall; external 'ntdll.dll'; RtlDosPathNameToNtPathName_U