I'm writing a shared library in c++ that needs to work in both standard Win32 apps and Windows Store apps.
However for a particular bit of code I need to know if the DLL is loaded inside of a Windows Store app or not so that I can create a temporary file in the appropriate way.
I'm aware of several compile-time checks using code like #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
and the like, but is there a way to make this check at runtime?
Assuming by "Windows Store apps" you mean UWP apps
If you are going to use Win32 desktop APIs in the DLL, you cannot do the check at runtime. The WACK Windows Store submission tool examines the exports and imports for all EXEs and DLLs and will fail any use of APIs that are not in the Windows Store API partition.
You should read this article for recommendations on handling this kind of multi-targeting problem.
Ideally you'll get most of your DLL code to work 'as is' for Windows Store and then can use the same code for the Win32 desktop version, but likely you are also concerned about 'down-level' versions of the OS as well which necessitates using _WIN32_WINNT version guards in some cases. For example, you can't use CreateFile for Windows Store, and you can't use CreateFile2 on Windows 7.
#if (_WIN32_WINNT >= 0x0602)
HANDLE hFile = CreateFile2( szFile, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr );
#else
HANDLE hFile = CreateFile( szFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr );
#endif
if ( !hFile )
{
return HRESULT_FROM_WIN32( GetLastError() );
}
Or you can use the CRT functions like fopen_s which abstracts this.
You can take a look at DirectXTex, DirectXTK, and DirectXMesh as code samples that utilize these techniques to build code for Windows Store for Windows 8.0, Windows Store for Windows 8.1, Windows Phone 8.0, Windows Phone 8.1, and Xbox One as well as Win32 desktop for Windows 8.x, Windows 7, and Windows Vista.
Remember also that Windows Store apps have a strict CRT requirement. Windows Store apps for Windows 8.0 must use VS 2012 CRT, and Windows Store apps for Windows 8.1 must use VS 2013 CRT.
Universal Windows Platform apps are similar to Windows Store, but have a few more APIs added back and some new alternatives for older Windows desktop functionality. You must use VS 2015 CRT, specifically the Universal CRT.
Assuming by "Windows Store apps" you mean a Win32 desktop application using the Desktop Bridge packaging for the Windows Store
See Microsoft Docs.
I would still suggest using the code pattern above in your source so as you move your minimum platform requirements over time, you prefer to use the API supported across multiple API families.