[fpc-pascal] teventobject.create fails with error 161

Tomas Hajny XHajT03 at hajny.biz
Fri Oct 14 19:27:37 CEST 2016

On Fri, October 14, 2016 17:44, Snorkl e wrote:


> Hi, actually getlasterror only applies to the calling thread,
> getlastOSerror is the one not to use.
> Anyway I think I found the solution.

I'm glad that you found potential reason of the problem, but your
assumption regarding GetLastError and GetLastOSError is not entirely
correct. You're right that GetLastError returns the last error in the
current thread, because that is written in the respective MSDN
documentation. I don't think that this is in contradiction to the
information from Jonas though.

Moreover, the only difference between GetLastError and GetLastOSError is
the fact that GetLastError is direct part of the MS Windows API (and thus
it behaves exactly as described in MSDN, but it is platform-specific),
whereas GetLastOSError is the FPC RTL abstraction for returning error
information from the underlying operating system (available across
different platforms). As such, you should rely on the behaviour of
GetLastOSError only to the level documented in the FPC documentation (in
other words, certain aspects described in MSDN documentation for
GetLastError may not be valid for GetLastOSError on all FPC supported
platforms due to differences in error handling on different platforms).

As an example, direct calls to Windows API bypassing FPC RTL and resulting
in an error would change the value returned by GetLastOSError on MS
Windows, whereas direct calls to OS/2 API bypassing FPC RTL and resulting
in an error would _not_ change the value returned by GetLastOSError on

The implementation of GetLastOSError for MS Windows (Win32 or Win64)
consists of a direct call to GetLastError and thus the same caveats apply
for them on that platform.


> On Oct 14, 2016 9:23 AM, "Jonas Maebe" <jonas.maebe at elis.ugent.be> wrote:
>> On 14/10/16 02:43, Snorkl e wrote:
>>> Anyway, I am using a third party SFTP server lib and it creates a
>>> thread
>>> inherited from tthread and in the create event of the tthread
>>> descendant
>>> it does this:
>>> (It uses this thread to read and write data on the socket)
>>> FDataAvailable := TEvent.Create(nil, True, False, '');  //tevent maps
>>> to
>>> teventobject
>>>   lasterr:=GetLastError;
>> GetLastError() will return the error code of the last Windows API call
>> that has been performed. I doubt TEvent.Create() guarantees not to call
>> any
>> Windows API's between creating an event using a Windows event and
>> returning. E.g., it might allocate or free memory in between, or the FPC
>> RTL may have done that in the context of handling the constructor call.
>> You should only call GetLastError() immediately after calling a Windows
>> API function yourself. In any other case, you cannot know what may have
>> transpired in between.
>> Jonas

More information about the fpc-pascal mailing list