Bug 122034 - Replace deprecated WinAPI functions with their recommended substitutions
Summary: Replace deprecated WinAPI functions with their recommended substitutions
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: reviewed:2024
Keywords: difficultyMedium, easyHack, skillCpp, skillWindows, topicCleanup
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2018-12-12 08:01 UTC by Mike Kaganski
Modified: 2024-01-05 16:03 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:


Attachments
Imports from modules in all DLL files inside instdir/program (1.34 MB, application/x-gzip)
2024-01-05 15:48 UTC, Hossein
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Kaganski 2018-12-12 08:01:41 UTC
Throughout the Win-specific part of the codebase, we have some deprecated WinAPI functions used. For example, CreateDIBPatternBrush [1] is used in GDI code. This might have different degree of consequences, from performance (deprecated functions are most probably internally implemented as wrappers over up-to-date API, with inevitable overhead; some obsolete API requires more processing, like using GlobalAlloc to obtain memory handle for CreateDIBPatternBrush), to possible loss of compatibility/functionality (some of obsolete functions may not support some newer OS functions).

This easy hack is about looking through the WinAPI functions used in the code, and checking if they are obsoleted by some newer/recommended API, and ultimately replacing those deprecated functions (with required modifications around that).

This requires first obtaining the list of such functions. This might be done e.g. using tools like Dependencies, [2] which can list imports of a module:

> C:\Dependencies_x64_Release\Dependencies.exe -imports c:\lo\core\instdir\program\gdipluscanvaslo.dll

and analyzing the output related to system libraries (KERNEL32.DLL, GDI32.DLL, etc).

Then, reading of the identified functions' documentation is required. [3] For each deprecated function found, the place where it's used should be found in the codebase. Some of the usages might come from external libraries; this easy hack is only about internal usages.

Not every usage of a deprecated function might require replacement; I cannot come with an example when it might be undesirable, but it is possible. So when reading the documentation, one must pay close attention to details about differences in behavior of the obsolete and recommended APIs.

[1] https://docs.microsoft.com/en-us/windows/desktop/api/wingdi/nf-wingdi-createdibpatternbrush
[2] https://github.com/lucasg/Dependencies
[3] https://docs.microsoft.com/en-us/windows/desktop/api/index
Comment 1 Hossein 2024-01-05 15:48:37 UTC
Created attachment 191780 [details]
Imports from modules in all DLL files inside instdir/program

Re-evaluating the EasyHack in 2024

This issue is still relevant, as there are many places in the code that need change. For example, consider these results

$ Dependencies.exe -imports instdir/program/gdipluscanvaslo.dll

In the output, these are the external DLL modules:

Windows API modules:
GDI32.dll
gdiplus.dll
USER32.dll
KERNEL32.dll

Visual C++ runtime modules:
MSVCP140D.dll
VCRUNTIME140D.dll
VCRUNTIME140_1D.dll
ucrtbased.dll

One should focus on the first set, which belongs to Windows API. For the first set, there are many imports, and the EasyHacker should have a list of deprecated functions to search among them.

The attached is the output of running "Dependencies.exe" tool over all the DLL files in instdir/program. It is created using this script:

for i in instdir/program/*.dll
do
    Dependencies.exe -imports $i >> dependency.txt
done
gzip dependency.txt

In a text editor, for example you can search for "CreateDIBPatternBrush ", and then backward search for "Import listing for file" to see the instdir/program/vclplug_winlo.dll, which is part of LibreOffice. Then, if you search for the CreateDIBPatternBrush (which is from GDI32.dll), you will find that in vcl/win/gdi/gdiimpl.cxx:

HBRUSH Make16BitDIBPatternBrush(Color nColor);
https://opengrok.libreoffice.org/xref/core/vcl/win/gdi/gdiimpl.cxx?r=956dc23f#1454

The replacement function is usually mentioned in the documentation. For example, in link [1] provided by Mike one can see the CreateDIBPatternBrushPt function introduced as a replacement:

"Note: This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the CreateDIBPatternBrushPt function."
[1] https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createdibpatternbrush

I mark this EasyHack as difficultyMedium.

The attachment is created using the latest LO dev master sources:

Version: 24.8.0.0.alpha0+ (X86_64) / LibreOffice Community
Build ID: 1bde4e6ff4bfcc922c7117d9d0528ce0c7f1adc5
CPU threads: 20; OS: Windows 10.0 Build 22631; UI render: Skia/Raster; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: CL threaded
Comment 2 Mike Kaganski 2024-01-05 16:02:33 UTC
(In reply to Hossein from comment #1)
> ... and the EasyHacker should have a list of
> deprecated functions to search among them.

Unfortunately, I don't know of such a list. The initial description has the suggested method of handling this task: get a list of dependencies using tools like https://github.com/lucasg/Dependencies - and then walk through the resulting list, reading documentation from MS, finding which of the functions are marked deprecated, ...