Skip to content

Issues with RaiseMSVCExceptionMethod

Compare with https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2022

For 64 bit:

  • The code needs to force alignment to 8. #pragma pack(push, 8)
    Currently dwThreadId and dwFlag end up in the same QWord
  • The code needs thrdinfo := Default(THREADNAME_INFO);. Otherwise if compiled with -gt the gaps will not be 0x00.
    However RaiseException takes a list of ULONG_PTR. So for RaiseExceptions the gaps in the record are part of the data. (Currently setting thread-dwFlags to 0 will make sure that dwThreadId is ok, but that is a "fix by double bug". That said, the sample code on the linked page, does not clear the record either...
  • SizeOf(thrdinfo) div SizeOf(DWord) should be SizeOf(thrdinfo) div SizeOf(PtrUInt). Otherwise the Count is way to high.

From rtl\win\systhrd.inc

    procedure RaiseMSVCExceptionMethod(threadHandle: TThreadID; const ThreadName: AnsiString);
    const
      MS_VC_EXCEPTION: DWord = $406D1388;
    type
      THREADNAME_INFO = record
        dwType: DWord; // Must be 0x1000.
        szName: PAnsiChar; // Pointer to name (in user addr space).
        dwThreadID: DWord; // Thread ID (-1=caller thread).
        dwFlags: DWord; // Reserved for future use, must be zero.
      end;
    var
      thrdinfo: THREADNAME_INFO;
    begin
      thrdinfo.dwType:=$1000;
      thrdinfo.szName:=@ThreadName[1];
      thrdinfo.dwThreadID:=threadHandle;
      thrdinfo.dwFlags:=0;
      try
        RaiseException(MS_VC_EXCEPTION, 0, SizeOf(thrdinfo) div SizeOf(DWord), @thrdinfo);
      except
        {do nothing}
      end;
    end;
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information