We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5390 - use GetModuleHandleW() to retrieve kernel32.dll handle.
Summary: use GetModuleHandleW() to retrieve kernel32.dll handle.
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: *don't know* (show other bugs)
Version: HG 2.0
Hardware: All Windows (All)
: P2 normal
Assignee: Ozkan Sezer
QA Contact: Sam Lantinga
URL:
Keywords: target-2.0.16
Depends on:
Blocks:
 
Reported: 2020-12-11 00:53 UTC by Ozkan Sezer
Modified: 2020-12-25 14:11 UTC (History)
1 user (show)

See Also:


Attachments
GetModuleHandle patch (3.11 KB, patch)
2020-12-11 00:53 UTC, Ozkan Sezer
Details | Diff
GetModuleHandle patch (3.09 KB, patch)
2020-12-11 00:58 UTC, Ozkan Sezer
Details | Diff
WinRT patch (1.58 KB, patch)
2020-12-24 01:01 UTC, Ozkan Sezer
Details | Diff
Statically link synchronization APIs on WINRT (11.16 KB, patch)
2020-12-24 10:57 UTC, Joel Linn
Details | Diff
Remove synchronization.lib from WinPhone81 (2.90 KB, patch)
2020-12-25 06:16 UTC, Joel Linn
Details | Diff
Disable WaitOnAddress SDL_sem implementation on Windows Phone (1.75 KB, patch)
2020-12-25 12:22 UTC, Joel Linn
Details | Diff
Disable WaitOnAddress on Windows Phone -- V2 (1.94 KB, patch)
2020-12-25 13:20 UTC, Ozkan Sezer
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ozkan Sezer 2020-12-11 00:53:02 UTC
Created attachment 4573 [details]
GetModuleHandle patch

SDL_systhread.c and SDL_syslocale.c used to call LoadLibrary() without
calling FreeLibrary() later.  GetModuleHandleW() should always succeed
because GetModuleHandleW() itself is imported from kernel32.dll and we
don't need to bother releasing it.
Comment 1 Ozkan Sezer 2020-12-11 00:58:17 UTC
Created attachment 4574 [details]
GetModuleHandle patch

(updated patch to fix commit message.)
Comment 2 Ozkan Sezer 2020-12-22 14:10:56 UTC
Pushed the patch as https://hg.libsdl.org/SDL/rev/df2a7472308f
Closing as fixed.
Comment 3 Sam Lantinga 2020-12-24 00:23:52 UTC
This doesn't appear to work with the older Windows Phone SDK:
1>..\..\src\thread\windows\SDL_syssem.c(396): warning C4013: 'GetModuleHandleW' undefined; assuming extern returning int
1>..\..\src\thread\windows\SDL_syssem.c(396): warning C4047: 'initializing' : 'HMODULE' differs in levels of indirection from 'int'
1>..\..\src\thread\windows\SDL_sysmutex.c(295): warning C4013: 'GetModuleHandleW' undefined; assuming extern returning int
1>..\..\src\thread\windows\SDL_sysmutex.c(295): warning C4047: 'initializing' : 'HMODULE' differs in levels of indirection from 'int'
Comment 4 Sam Lantinga 2020-12-24 00:24:07 UTC
Ozkan, can you look at this?
Comment 5 Ozkan Sezer 2020-12-24 00:51:08 UTC
I do not know anything about Windows Phone, but aren't the project
files supposed to use winrt/* sources instead of windows/*.c ones?
If they have to use windows/*.c sources, can we conditionalize the
GetModuleHandleW usage using __WINRT__ ifdefs?
Comment 6 Ozkan Sezer 2020-12-24 01:01:17 UTC
Created attachment 4601 [details]
WinRT patch

OK, does the attached patch work correctly?
Comment 7 Ozkan Sezer 2020-12-24 01:04:15 UTC
Added Joel Linn to the CC list: those are the files touched by him.
Comment 8 Joel Linn 2020-12-24 10:57:34 UTC
Created attachment 4602 [details]
Statically link synchronization APIs on WINRT

I'm unable to find a VS2013 Community download but I think you are referring to the `SDL-WinPhone81` and `SDL-WinRT81` projects. They are special in that they do not use the `stdcpp` implementations like the `SDL-UWP` project does.

The way I understand the Windows header `#if`s, all the APIs we load dynamically should be available in `WINAPI_PARTITION_APP` which is part of `WINAPI_FAMILY_PHONE_APP`.

If possible I would just skip dynamic loading and link statically. I made a patch for that, can't test it for the WinPhone target personally though...
Comment 9 Ozkan Sezer 2020-12-25 01:00:34 UTC
(In reply to Joel Linn from comment #8)
> Created attachment 4602 [details]
> Statically link synchronization APIs on WINRT
> 
> I'm unable to find a VS2013 Community download but I think you are referring
> to the `SDL-WinPhone81` and `SDL-WinRT81` projects. They are special in that
> they do not use the `stdcpp` implementations like the `SDL-UWP` project does.
> 
> The way I understand the Windows header `#if`s, all the APIs we load
> dynamically should be available in `WINAPI_PARTITION_APP` which is part of
> `WINAPI_FAMILY_PHONE_APP`.
> 
> If possible I would just skip dynamic loading and link statically. I made a
> patch for that, can't test it for the WinPhone target personally though...

I applied this patch as https://hg.libsdl.org/SDL/rev/879390cc2733
Let's see how buildbot reacts to it.
Comment 10 Ozkan Sezer 2020-12-25 01:04:02 UTC
No warnings at all, but linkage fails with
1>LINK : fatal error LNK1181: cannot open input file 'synchronization.lib'

https://buildbot.libsdl.org/#/builders/8/builds/408
Comment 11 Joel Linn 2020-12-25 01:13:33 UTC
(In reply to Ozkan Sezer from comment #10)
> No warnings at all, but linkage fails with
> 1>LINK : fatal error LNK1181: cannot open input file 'synchronization.lib'
> 
> https://buildbot.libsdl.org/#/builders/8/builds/408

Oh wow. On Desktop `WaitOnAddress` is imported from that lib so I added it.
Let me see, maybe I can dig up an old VS2013 and check with a phone project...
Comment 12 Joel Linn 2020-12-25 06:16:30 UTC
Created attachment 4603 [details]
Remove synchronization.lib from WinPhone81

I checked with VS2013. While linking to `synchronization.lib` is required (and correct) for Desktop and UWP apps (https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitonaddress), the required functions of the synch API seem to be implicitly available on Windows Phone 8.1 without setting additional dependencies.
Comment 13 Ozkan Sezer 2020-12-25 07:44:00 UTC
Thanks.  Does WinRT81_VS2013 project not need the same?
Comment 14 Joel Linn 2020-12-25 12:15:28 UTC
(In reply to Ozkan Sezer from comment #13)
> Thanks.  Does WinRT81_VS2013 project not need the same?

No, it is correct (and required) to link against that library for Desktop and Desktop Store Apps.
Comment 15 Joel Linn 2020-12-25 12:22:14 UTC
Created attachment 4604 [details]
Disable WaitOnAddress SDL_sem implementation  on Windows Phone

Although it links on WinPhone now, the APIs are not actually legal to use :/
https://www.microsoft.com/en-us/download/details.aspx?id=47328

Output from appcert.exe for a test APP that links to the APIs in question:

```
<REQUIREMENT NUMBER="15" TITLE="Supported API test" RATIONALE="The application should only refer to the APIs allowed by the Windows SDK for Windows Store Apps.">
      <TEST INDEX="72" NAME="Supported APIs" DESCRIPTION="Windows Phone App must only use supported platform APIs." EXECUTIONTIME="00h:00m:01s.11ms" OPTIONAL="FALSE">
        <MESSAGES>
          <MESSAGE TEXT="This API is not supported for this application type - Api=WaitOnAddress. Module=api-ms-win-core-synch-l1-2-0.dll. File=App1.exe." />
          <MESSAGE TEXT="This API is not supported for this application type - Api=WakeByAddressSingle. Module=api-ms-win-core-synch-l1-2-0.dll. File=App1.exe." />
        </MESSAGES>
        <RESULT><![CDATA[FAIL]]></RESULT>
      </TEST>
    </REQUIREMENT>
```
Comment 16 Ozkan Sezer 2020-12-25 13:20:05 UTC
Created attachment 4607 [details]
Disable WaitOnAddress on Windows Phone -- V2

(In reply to Joel Linn from comment #15)
> Created attachment 4604 [details]
> Disable WaitOnAddress SDL_sem implementation  on Windows Phone
> 
> Although it links on WinPhone now, the APIs are not actually legal to use :/
> https://www.microsoft.com/en-us/download/details.aspx?id=47328

Modified your patch for compatibility: not all SDKs have
WINAPI_FAMILY_PHONE_APP (or even have winapifamily.h for
that matter.)  Please confirm / review.
Comment 17 Joel Linn 2020-12-25 13:32:08 UTC
(In reply to Ozkan Sezer from comment #16)
> Created attachment 4607 [details]
> Disable WaitOnAddress on Windows Phone -- V2
> 
> (In reply to Joel Linn from comment #15)
> > Created attachment 4604 [details]
> > Disable WaitOnAddress SDL_sem implementation  on Windows Phone
> > 
> > Although it links on WinPhone now, the APIs are not actually legal to use :/
> > https://www.microsoft.com/en-us/download/details.aspx?id=47328
> 
> Modified your patch for compatibility: not all SDKs have
> WINAPI_FAMILY_PHONE_APP (or even have winapifamily.h for
> that matter.)  Please confirm / review.

Builds fine for VS2013 WinRT81/WinPhone81 and VS2019 CMake
Comment 18 Ozkan Sezer 2020-12-25 14:10:49 UTC
OK then, should be fixed by https://hg.libsdl.org/SDL/rev/b69400b305a5
Comment 19 Ozkan Sezer 2020-12-25 14:11:41 UTC
Closing.