You are here

Programming

How to delete the first / last element in a sorted mysql table

Not so frequently asked questions and stuff: 

This will delete the element with the biggest notice_id from the table notices.

DELETE FROM notices WHERE notice_id = (

    SELECT notice_id FROM (

        SELECT notice_id FROM notices ORDER BY notice_id DESC LIMIT 1

    ) AS noticesa

)

Mysql can't do subqueries inside the same table, hence the noticesa.

Verifying If Windows Firewall is Enabled in Delphi

Not so frequently asked questions and stuff: 
function IsWindowsFireWallEnabled : boolean;
var
  HNetCfg: OleVariant;
  ShellApplication: OleVariant;
begin
 ShellApplication := CreateOleObject('Shell.Application');
 if not ShellApplication.IsServiceRunning('SharedAccess') then
   begin
     ShellApplication.ServiceStart('SharedAccess', true);
     Sleep(2000);
   end;

 HNetCfg := CreateOLEObject('hnetcfg.fwmgr');
 Result := HNetCfg.LocalPolicy.CurrentProfile.FirewallEnabled;
end;

Source:
* http://msdn.microsoft.com/en-us/library/aa366442%28VS.85%29.aspx

How to read a REG_MULTI_SZ value into the registry in Delphi

Not so frequently asked questions and stuff: 
procedure ReadREG_MULTI_SZ(const CurrentKey: HKey; const Subkey, ValueName: string;
  Strings: TStrings);
var
  valueType: DWORD;
  valueLen: DWORD;
  p, buffer: PChar;
  key: HKEY;
begin
  // Clear TStrings
  // TStrings leeren
  Strings.Clear;
  // open the specified key
  // CurrentKey Schlüssel öffnen
  if RegOpenKeyEx(CurrentKey,
                  PChar(Subkey),
                  0, KEY_READ, key) = ERROR_SUCCESS then
  begin
    // retrieve the type and data for a specified value name
    // Den Typ und Wert des Eintrags Ermitteln.
    SetLastError(RegQueryValueEx(key,
                 PChar(ValueName),
                 nil,
                 @valueType,
                 nil,
                 @valueLen));
    if GetLastError = ERROR_SUCCESS then
      if valueType = REG_MULTI_SZ then
      begin
        GetMem(buffer, valueLen);
        try
          // receive the value's data (in an array).
          // Ein Array von Null-terminierten Strings
          // wird zurückgegeben
          RegQueryValueEx(key,
                          PChar(ValueName),
                          nil,
                          nil,
                          PBYTE(buffer),
                          @valueLen);
          // Add values to stringlist
          // Werte in String Liste einfügen
          p := buffer;
          while p^  #0 do
          begin
            Strings.Add(p);
            Inc(p, lstrlen(p) + 1)
          end
        finally
          FreeMem(buffer)
        end
      end
      else
        raise ERegistryException.Create('Stringlist expected/ String Liste erwartet...')
    else
      raise ERegistryException.Create('Cannot Read MULTI_SZ Value/'+
        'Kann den MULTI_SZ Wert nicht lesen...');
  end;
end;

How to write a REG_MULTI_SZ value into the registry in Delphi

Not so frequently asked questions and stuff: 
procedure kg_WriteStrings(const valueName: string; aStrings: TStrings; oReg:TRegistry) ;
var
  p, buffer : PChar ;
  i         : Integer ;
  size      : DWORD ;
begin
  size := 0 ;
  for i := 0 to aStrings.Count - 1 do
    Inc(size, Length(aStrings [i]) + 1) ;
  Inc (size) ;
  GetMem(buffer, size) ;
  try
    p := buffer ;
    for i := 0 to aStrings.count - 1 do
    begin
      lstrcpy(p, PChar(aStrings [i])) ;
      Inc(p, lstrlen (p) + 1)
    end;
    p^ := #0 ;
    SetLastError(RegSetValueEx(oReg.CurrentKey, PChar(valueName), 0, REG_MULTI_SZ, buffer, size)) ;
    if GetLastError  ERROR_SUCCESS then
      raise Exception.Create('Unable to write MULTI_SZ value') ;
  finally
    FreeMem(buffer)
  end
end;

Force ShellExecute to run with elevated privileges in Delphi

Not so frequently asked questions and stuff: 

Using ShellExecute to start process running as administrator. The Application.Handle delays elevation if the application is minimized preventing the secure desktop to steel focus for the consent dialog when the user is doing something else. Use Application.Handle (or MainForm.Handle if you have applied other patches that make it the application windows)

If you do not use a handle UAC will always give direct foreground elevation.

function RunAsAdmin(hWnd: HWND; filename: string; Parameters: string): Boolean;
{
    See Step 3: Redesign for UAC Compatibility (UAC)
    http://msdn.microsoft.com/en-us/library/bb756922.aspx
}
var
    sei: TShellExecuteInfo;
begin
    ZeroMemory(@sei, SizeOf(sei));
    sei.cbSize := SizeOf(TShellExecuteInfo);
    sei.Wnd := hwnd;
    sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
    sei.lpVerb := PChar('runas');
    sei.lpFile := PChar(Filename); // PAnsiChar;
    if parameters  '' then
        sei.lpParameters := PChar(parameters); // PAnsiChar;
    sei.nShow := SW_SHOWNORMAL; //Integer;

    Result := ShellExecuteEx(@sei);
end;

Source:
* http://edn.embarcadero.com/article/33942

Force ShellExecute to run and wait for completion with elevated privileges in Delphi

Not so frequently asked questions and stuff: 
function RunAsAdminAndWaitForCompletion(hWnd: HWND; filename: string; Parameters: string): Boolean;
{
    See Step 3: Redesign for UAC Compatibility (UAC)
    http://msdn.microsoft.com/en-us/library/bb756922.aspx
}
var
    sei: TShellExecuteInfo;
    ExitCode: DWORD;
begin
    ZeroMemory(@sei, SizeOf(sei));
    sei.cbSize := SizeOf(TShellExecuteInfo);
    sei.Wnd := hwnd;
    sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI or SEE_MASK_NOCLOSEPROCESS;
    sei.lpVerb := PChar('runas');
    sei.lpFile := PChar(Filename); // PAnsiChar;
    if parameters  '' then
        sei.lpParameters := PChar(parameters); // PAnsiChar;
    sei.nShow := SW_SHOWNORMAL; //Integer;

   if ShellExecuteEx(@sei) then begin
     repeat
       Application.ProcessMessages;
       GetExitCodeProcess(sei.hProcess, ExitCode) ;
     until (ExitCode  STILL_ACTIVE) or  Application.Terminated;
   end;
end;

Source:
* http://delphi.about.com/od/windowsshellapi/a/executeprogram.htm

How to send a file via HTTP specifying a custom filename, using PHP

Not so frequently asked questions and stuff: 
\n";
} else {
 srand((double)microtime()*1000000);
 $boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
    
    $data = "";
 
    $header = "";
    $header .= "POST $service HTTP/1.1\r\n";
    $header .= "Host: $host\r\n";
    $header .= "Content-Type: multipart/form-data; boundary=$boundary\r\n"; 
    
    $APOST = array("a" => "b", "c" => "d");
    
        foreach($APOST AS $index => $value){
            $data .="--$boundary\r\n";
            $data .= "Content-Disposition: form-data; name=\"".$index."\"\r\n";
            $data .= "\r\n".$value."\r\n";
            $data .="--$boundary\r\n";
        } 
    
  $data .= "--$boundary\r\n";
   $content_file = join("", file($filetosend));
    $content_type = "";
 $file_name = $filenametosend;
   $data .="Content-Disposition: form-data; name=\"userfile\"; filename=\"$file_name\"\r\n";
 $data .= "Content-Type: $content_type\r\n\r\n";
 $data .= "".$content_file."\r\n";
 $data .="--$boundary--\r\n";
        
    $header .= "Content-length: " . strlen($data); 
    $header .= "\r\n\r\n";
    $out = $header.$data;
    
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 4096);
    }
    fclose($fp);
}
?>

Pages

Subscribe to Programming