The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,861 other subscribers

Archive for the ‘Windows Development’ Category

Breaking in the Delphi debugger without a breakpoint

Posted by jpluimers on 2019/02/20

You can fire a debugger breakpoint using either of these two:

  • asm int 3 end which is the x86 debug interrupt
  • DebugBreak() which is the Windows API function wrapping the above interrupt

I’m not sure how accurate it is (in the past it would fail under some debuggers other than the Delphi IDE), but as of Delphi 2, there is a DebugHook variable that is non-zero when running under the Delphi debugger, so you can protect your code.

Via [WayBackI remember some time ago, Jeroen Pluijmers posted a snippet of how to place a breakpoint directly in the Delphi source without relying on the F5 key. – Alberto Paganini – Google+

Related:

–jeroen

Posted in Delphi, Development, Software Development, Windows Development | Leave a Comment »

A 90-byte “whereis” program – The Old New Thing

Posted by jpluimers on 2018/11/23

I needed a “get only the first result” of WHERE (which is present after Windows 2000, so XP, Server 2003 and up), so based on [WayBackA 90-byte “whereis” program – The Old New Thing I came up with this:

@echo off
:: based on https://blogs.msdn.microsoft.com/oldnewthing/20050120-00/?p=36653
::for %%f in (%1) do @echo.%%~$PATH:f
for %%e in (%PATHEXT%) do @for %%i in (%1 %~n1%%e) do (
  @if NOT "%%~$PATH:i"=="" (
    echo %%~$PATH:i
    goto :eof
  )
)
:: note: WHERE lists all occurrences of a file on the PATH in PATH order
goto :eof

Two changes:

  • it takes into account the extension if you specify it (unlike WHERE.EXE)
  • it bails out at the first match (like WHERE.EXE)

References:

–jeroen

Posted in Batch-Files, Development, Power User, Scripting, Software Development, The Old New Thing, Windows, Windows Development | Leave a Comment »

Solution for Delphi – post-build event with multiple if/copy combinations only execute if first file does not exist – Stack Overflow

Posted by jpluimers on 2018/11/15

My solution in [WayBack] delphi – post-build event with multiple if/copy combinations only execute if first file does not exist – Stack Overflow is an addendum to my 2014 post Delphi prebuild/prelink/postbuild events.

Here we go:

Read the rest of this entry »

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development, The Old New Thing, Windows Development | Leave a Comment »

Batch files and parentheses

Posted by jpluimers on 2018/10/31

Answering [WayBack] delphi – post-build event with multiple if/copy combinations only execute if first file does not exist – Stack Overflow made me do a quick search for parentheses handling in batch files. TL;DR: it is a mess.

But it reveals some interesting links:

–jeroen

Posted in Batch-Files, Conference Topics, Conferences, Development, Event, Scripting, Software Development, Windows Development | Leave a Comment »

Finding your program with an “Access Denied” (Error code 5) after lunch break…

Posted by jpluimers on 2018/10/24

Via: [WayBack] I just returned from lunch break and found my program faulted with an “Access Denied” (Error code 5) error in a call to Mouse.GetCoursorPos and was wond… – Thomas Mueller (dummzeuch) – Google+:

All of [WayBackGetCursorPos, [WayBackGetCursorInfo and [WayBack] GetKeyState can cause an “Access Denied” (Error code 5) when they do not have permission for the current desktop (for instance the logon desktop when a screen-saver has kicked in).

Solution: write a wrapper around it then [WayBack] patch calls going to the original into the patch [WayBack] delphi – Explain errors from GetKeyState / GetCursorPos – Stack Overflow

–jeroen

Posted in .NET, C#, C++, Delphi, Development, Software Development, Windows Development | Leave a Comment »

Using Windows API function MoveFile to check if a filename is valid…  – via Stack Overflow

Posted by jpluimers on 2018/10/17

This has a really neat Windows API trick [WayBackdelphi – How can I sanitize a string for use as a filename? – Stack Overflow by [WayBack] Alex.

Via: [WayBack] Interesting use of MoveFile, with NIL as the first parameter. https://stackoverflow.com/a/961500/49925 – Thomas Mueller (dummzeuch) – Google+

  function IsValidFilePath(const FileName: String): Boolean;
  var
    S: String;
    I: Integer;
  begin
    Result := False;
    S := FileName;
    repeat
      I := LastDelimiter('\/', S);
      MoveFile(nil, PChar(S));
      if (GetLastError = ERROR_ALREADY_EXISTS) or
         (
           (GetFileAttributes(PChar(Copy(S, I + 1, MaxInt))) = INVALID_FILE_ATTRIBUTES)
           and
           (GetLastError=ERROR_INVALID_NAME)
         ) then
        Exit;
      if I>0 then
        S := Copy(S,1,I-1);
    until I = 0;
    Result := True;
  end;

–jeroen

Posted in Delphi, Development, Software Development, Windows Development | 3 Comments »

Windows service states and service state transitions

Posted by jpluimers on 2018/09/12

States via [WayBackSERVICE_STATUS structure (Windows)

Value Hex Meaning
SERVICE_CONTINUE_PENDING 0x00000005 The service continue is pending.
SERVICE_PAUSE_PENDING 0x00000006 The service pause is pending.
SERVICE_PAUSED 0x00000007 The service is paused.
SERVICE_RUNNING 0x00000004 The service is running.
SERVICE_START_PENDING 0x00000002 The service is starting.
SERVICE_STOP_PENDING 0x00000003 The service is stopping.
SERVICE_STOPPED 0x00000001 The service is not running.

 

State transition overview

It’s complex as a table, so there are two.

  • The initial state is SERVICE_STOPPED.
  • Most logical targets are in italic.

Common transitions:

From state To state
SERVICE_STOPPED SERVICE_START_PENDING
SERVICE_START_PENDING SERVICE_RUNNING
SERVICE_START_PENDING SERVICE_STOPPED
SERVICE_START_PENDING SERVICE_STOP_PENDING
SERVICE_RUNNING SERVICE_STOPPED
SERVICE_RUNNING SERVICE_STOP_PENDING
SERVICE_STOP_PENDING SERVICE_STOPPED

Infrequent transitions:

From state To state
SERVICE_RUNNING SERVICE_PAUSE_PENDING
SERVICE_RUNNING SERVICE_PAUSED
SERVICE_PAUSE_PENDING SERVICE_PAUSED
SERVICE_PAUSE_PENDING SERVICE_STOP_PENDING
SERVICE_PAUSE_PENDING SERVICE_STOPPED
SERVICE_PAUSED SERVICE_RUNNING
SERVICE_PAUSED SERVICE_CONTINUE_PENDING
 SERVICE_PAUSED SERVICE_STOP_PENDING
 SERVICE_PAUSED SERVICE_STOPPED
SERVICE_CONTINUE_PENDING SERVICE_RUNNING
 SERVICE_CONTINUE_PENDING  SERVICE_STOP_PENDING
 SERVICE_CONTINUE_PENDING  SERVICE_STOPPED

Read the rest of this entry »

Posted in Development, Software Development, Windows Development | Leave a Comment »

A refefernce to 6502 by “Remember that in a stack trace, the addresses are return addresses, not call addresses – The Old New Thing”

Posted by jpluimers on 2018/09/11

On x86/x64/ARM/…:

It’s where the function is going to return to, not where it came from.

And:

Bonus chatter: This reminds me of a quirk of the 6502 processor: When it pushed the return address onto the stack, it actually pushed the return address minus one. This is an artifact of the way the 6502 is implemented, but it results in the nice feature that the stack trace gives you the line number of the call instruction.

Of course, this is all hypothetical, because 6502 debuggers didn’t have fancy features like stack traces or line numbers.

Source: [WayBackRemember that in a stack trace, the addresses are return addresses, not call addresses – The Old New Thing

Which resulted in these comments at [WayBack] CC +mos6502 – Jeroen Wiert Pluimers – Google+:

  • mos6502: And don’t forget the crucial difference in PC on 6502 between RTS and RTI!
  • Jeroen Wiert Pluimers: +mos6502 I totally forgot about that one. Thanks for reminding me
    <<Note that unlike RTS, the return address on the stack is the actual address rather than the address-1.>>

References:

[WayBack6502.org: Tutorials and Aids – RTI

RTI retrieves the Processor Status Word (flags) and the Program Counter from the stack in that order (interrupts push the PC first and then the PSW).

Note that unlike RTS, the return address on the stack is the actual address rather than the address-1.

[WayBack6502.org: Tutorials and Aids – RTS

RTS pulls the top two bytes off the stack (low byte first) and transfers program control to that address+1. It is used, as expected, to exit a subroutine invoked via JSR which pushed the address-1.

RTS is frequently used to implement a jump table where addresses-1 are pushed onto the stack and accessed via RTS eg. to access the second of four routines.

–jeroen

Posted in 6502, 6502 Assembly, Assembly Language, Development, History, Software Development, The Old New Thing, Windows Development, x64, x86 | Leave a Comment »

Reading files that are locked by other references: c# – Notepad beats them all? – Stack Overflow

Posted by jpluimers on 2018/08/16

Cool feature borrowed from Notepad, which can read files locked by other references (for instance a process having the handle open): [WayBackc# – Notepad beats them all? – Stack Overflow.

The example from the answer is in .NET, but can be used in a native environment as well (Notepad is a native application).

Notepad reads files by first mapping them into memory, rather than using the “usual” file reading mechanisms presumably used by the other editors you tried. This method allows reading of files even if they have an exclusive range-based locks.

You can achieve the same in C# with something along the lines of:

using (var f = new FileStream(processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var m = MemoryMappedFile.CreateFromFile(f, null, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, true))
using (var s = m.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
using (var r = new StreamReader(s))
{
    var l = r.ReadToEnd();
    Console.WriteLine(l);
}

Via: [WayBack] Maintaining Notepad is not a full-time job, but it’s not an empty job either – The Old New Thing

–jeroen

Posted in .NET, Delphi, Development, Software Development, The Old New Thing, Windows Development | Leave a Comment »

Some links on how Windows detects if a program “is not responding”

Posted by jpluimers on 2018/05/09

For my research list:

–jeroen

Posted in Development, Software Development, The Old New Thing, Windows Development | Leave a Comment »