[GH-ISSUE #155] Native Hosting sample compile error in Windows #94

Closed
opened 2026-05-05 11:04:22 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @geigertom on GitHub (Aug 3, 2019).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/155

I tried to run the native hosting sample project in Windows 7. Could not compile it, because of this error:

CoreHost.cpp:26:23: error: no matching function for call to 'GetModuleHandleW'
libloaderapi.h:274:26: note: expanded from macro 'GetModuleHandle'
libloaderapi.h:269:1: note: candidate function not viable: no known conversion from 'QByteArray' to 'LPCWSTR' (aka 'const wchar_t *') for 1st argument

I had to change line 26 in src\native\QmlNet\HostingCoreHost,cpp :

HMODULE library = GetModuleHandle(nativeModule.isNull() || nativeModule.isEmpty() ? nullptr   : nativeModule.toLocal8Bit());

to

HMODULE library = GetModuleHandle(nativeModule.isNull() || nativeModule.isEmpty() ? nullptr :
 (LPCWSTR)nativeModule.utf16());

to get it working.

Seems to be related to the number of bytes used per character.

Is this a valid fix for the error?

Originally created by @geigertom on GitHub (Aug 3, 2019). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/155 I tried to run the native hosting sample project in Windows 7. Could not compile it, because of this error: ``` CoreHost.cpp:26:23: error: no matching function for call to 'GetModuleHandleW' libloaderapi.h:274:26: note: expanded from macro 'GetModuleHandle' libloaderapi.h:269:1: note: candidate function not viable: no known conversion from 'QByteArray' to 'LPCWSTR' (aka 'const wchar_t *') for 1st argument ``` I had to change line 26 in src\native\QmlNet\HostingCoreHost,cpp : ``` HMODULE library = GetModuleHandle(nativeModule.isNull() || nativeModule.isEmpty() ? nullptr : nativeModule.toLocal8Bit()); ``` to ``` HMODULE library = GetModuleHandle(nativeModule.isNull() || nativeModule.isEmpty() ? nullptr : (LPCWSTR)nativeModule.utf16()); ``` to get it working. Seems to be related to the number of bytes used per character. Is this a valid fix for the error?
Author
Owner

@pauldotknopf commented on GitHub (Aug 3, 2019):

There are two methods, GetModuleHandleA, and GetModuleHandleW. One uses wide chars (unicode), the other uses ansci chars. The method GetModuleHandle is aliased to one of the two. I believe the header file on Windows has some ifdefs to determine which alias should be used.

I don't have my Windows machine with me, but my guess is that when I initially ran this on Windows, the GetModuleHandleA was being used. On your machine, it looks like GetModuleHandleW is being used. I think the correct method would be to just use GetModuleHandleW directly, instead of the GetModuleHandle alias.

<!-- gh-comment-id:517962710 --> @pauldotknopf commented on GitHub (Aug 3, 2019): There are two methods, ```GetModuleHandleA```, and ```GetModuleHandleW```. One uses wide chars (unicode), the other uses ansci chars. The method ```GetModuleHandle``` is aliased to one of the two. I believe the header file on Windows has some ```ifdef```s to determine which alias should be used. I don't have my Windows machine with me, but my guess is that when I initially ran this on Windows, the ```GetModuleHandleA``` was being used. On your machine, it looks like ```GetModuleHandleW``` is being used. I think the correct method would be to just use ```GetModuleHandleW``` directly, instead of the ```GetModuleHandle``` alias.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/qmlnet#94
No description provided.