Forum > Windows

The declaration of WriteFile, ReadFile win32 api may be incorrect.

(1/7) > >>

marunguy:
The declaration of WriteFile, ReadFile win32 api may be incorrect.

Window 10 64bit, Lazarus 2.2.0 x86_64-win64-win32/win64, FPC 3.2.2

* original declaration

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// lazarus\fpc\3.2.2\source\rtl\win\wininc\redef.incfunction WriteFile(hFile: THandle; const Buffer; nNumberOfBytesToWrite: DWORD; var lpNumberOfBytesWritten: DWORD; lpOverlapped: POverlapped): BOOL; external 'kernel32' name 'WriteFile';function ReadFile(hFile: THandle; var Buffer; nNumberOfBytesToRead: DWORD; var lpNumberOfBytesRead: DWORD; lpOverlapped: POverlapped): BOOL; external 'kernel32' name 'ReadFile';
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Windows.WriteFile(my_hdl, buf, buf_len, written_bytes, nil)Windows.ReadFile(my_hdl, buf, buf_len, read_bytes, nil);
Invalid Buffer value is passed in win64 build mode.
The access viloation exception occurs in win32 build mode when ReadFile is called.

* first fix - change type of Buffer.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// mykernel32.pasfunction WriteFile(hFile: THandle; const Buffer: Pointer; const nNumberOfBytesToWrite: DWORD; var lpNumberOfBytesWritten: DWORD; lpOverlapped: POverlapped): BOOL; external 'kernel32' name 'WriteFile';function ReadFile(hFile: THandle; Buffer: Pointer; const nNumberOfBytesToRead: DWORD; var lpNumberOfBytesRead: DWORD; lpOverlapped: POverlapped): BOOL; external 'kernel32' name 'ReadFile';
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---mykernel32.WriteFile(my_hdl, buf, buf_len, written_bytes, nil)mykernel32.ReadFile(my_hdl, buf, buf_len, read_bytes, nil);
It works well in win64 build mode.
Sometimes and exception occurs in win32 build mode

* second fix - change type of Buffer, lpNumberOfBytesWritten and lpNumberOfBytesRead, add stdcall

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// mykernel32.pasfunction WriteFile(hFile: THandle; const Buffer: Pointer; const nNumberOfBytesToWrite: DWORD; lpNumberOfBytesWritten: PDWORD; lpOverlapped: POverlapped): BOOL; stdcall; external 'kernel32' name 'WriteFile';function ReadFile(hFile: THandle; Buffer: Pointer; const nNumberOfBytesToRead: DWORD; lpNumberOfBytesRead: PDWORD; lpOverlapped: POverlapped): BOOL; stdcall; external 'kernel32' name 'ReadFile';
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---mykernel32.WriteFile(my_hdl, buf, buf_len, @written_bytes, nil)mykernel32.ReadFile(my_hdl, buf, buf_len, @read_bytes, nil);
It works well in both win32 and win64 build mode.

marcov:
A change that requires calls to be changed (like @) is not acceptable,
moreover the current declaration is Delphi compatible. (redef.inc was for delphi compatibility)

Note that the windows unit  (rtl/win<xxx>/windows.pp contains {$calling stdcall} which changes the whole unit's calling convention to stdcall.

I'm also not sure what this would exactly improve. The declarations are the same, pascal call by var is equivalent to call with a pointer.

These calls have been declared like this since probably 1997, and are not exactly the rarest used calls.


AlexTP:
Maybe post small example app, which shows access-violations for the current FPC code?

440bx:

--- Quote from: marunguy on May 19, 2022, 10:25:54 am ---The declaration of WriteFile, ReadFile win32 api may be incorrect.

--- End quote ---
It is incorrect but, you won't get it fixed.  The incorrect definition has been in use for years and correcting it would likely break a fair amount of code, it's not going to happen.

I reported that problem over a year ago and had a long and, totally fruitless, argument about it.  That's all you can get. <chuckle> if you care to read "the entertaining" part of the discussion, it's at : https://forum.lazarus.freepascal.org/index.php/topic,46185.msg329022.html#msg329022

Your consolation prize is, you're right.  Unfortunately, it doesn't matter.

trev:
From the previous thread - PascalDragon:

--- Quote ---What I do grant you however is that we don't have the equivalent declaration as an overload. Those should in my opinion definitely be there as those declarations with untyped parameters or typed var parameters are considered "convenience" declarations and thus an "add on".
--- End quote ---

If that were done would it not make everyone happy? I guess someone needs to submit a patch...

Navigation

[0] Message Index

[#] Next page

Go to full version