bitnick
bitnick

Reputation: 2073

How to import windows.h as module correctly

mybar.ixx

export module mybar;

#include "windows.h"

export
double trywinapi() {
    MEMORYSTATUSEX memInfo;
    memInfo.dwLength = sizeof(MEMORYSTATUSEX);
    GlobalMemoryStatusEx(&memInfo);
    return memInfo.ullTotalPageFile;
}

main.cpp

import mybar;

#include "windows.h"

void main() {
    trywinapi();
}

And visual studio 2019 compile error:

error LNK2019: unresolved external symbol __imp__GlobalMemoryStatusEx@4::<!mybar> referenced in function "double __cdecl trywinapi(void)" (?trywinapi@@YANXZ::<!mybar>)

Upvotes: 3

Views: 3948

Answers (2)

rbento
rbento

Reputation: 11678

I still found many warnings by including <windows.h> in a C++ module and building for Debug/x64, which were dealt with like so:

Ignore external library warnings

mybar.cpp

module;
#pragma warning(push, 0)
#include <windows.h>
#pragma pop(0)
export module mybar;

This would still output a warning:

C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\winbase.h(9531,5): warning C5105: macro expansion producing 'defined' has undefined behavior

Update Windows SDK

The warning above apparently was a regression issue which was resolved by updating the SDK to Windows 10 SDK (10.0.20348.0), version 2104 and making sure the Visual Studio project utilized the last Windows SDK in:

Project > Properties > Configuration Properties > General > Windows SDK Version

Done.

My Environment

Edition Windows 11 Home
Update 21H2
OS build 22000.51
Experience Windows Feature Experience Pack 421.16300.0.3

Microsoft Visual Studio Community 2022 Preview
Version 17.0.0 Preview 1.1
VisualStudio.17.Preview/17.0.0-pre.1.1+31423.177
Version 4.8.04161

Upvotes: 3

Davis Herring
Davis Herring

Reputation: 40033

The global module fragment goes before the module name:

module;
#include<windows.h>
export module mybar;
export double trywinapi() {…}

Upvotes: 4

Related Questions