[fpc-devel] definition of "THREADNAME_INFO = record" IN procedure RaiseMSVCExceptionMethod in rtl\win\systhrd.inc

Martin Frb lazarus at mfriebe.de
Wed May 4 01:30:13 CEST 2022


https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-raiseexception
> |void RaiseException( [in] DWORD dwExceptionCode, [in] DWORD 
> dwExceptionFlags, [in] DWORD nNumberOfArguments, [in] const ULONG_PTR 
> *lpArguments );|

In the below code some fields are 32bit (DWord). But according to msdn 
they are  "ULONG_PTR" (64 bit on Win64).
(Same in ExceptionRecord, which is delivered to an attached debugger: 
https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-exception_record 
)

Well, the fields may be 32bit (if the data stored is that size), as long 
as they each align on the next 8 byte boundary.
Only they do not.
- dwType (offset 0) => ok
- szName (offset 8) => still in the correct location.
- dwThreadId too (offset 16)
- dwflags (offset 20)  overrides the upper (empty) DWord of dwThreadId
   Yes, I checked => dwFlags is aligned at offset 20. But it should be 24.

Further more "div SizeOf(DWord)" => gives 6 params (the record has a 
size of 24). But there are only 4 params.


https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2022
Microsoft  also has DWord as type for the fields. But I am not sure how 
they will be aligned.
However they do "sizeof(info) / sizeof(ULONG_PTR)"

-------------------------------------

     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;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20220504/f6a5ad62/attachment.htm>


More information about the fpc-devel mailing list