Does WinRT _really_ support thread local storage functions (Tls*)? - windows-runtime

I'm porting legacy Windows code to Windows Runtime (WinRT), specifically Windows Phone 8.1. The code contains calls to thread local storage functions like TlsAlloc, TlsFree, TlsGetValue, and TlsSetValue. According to MSDN's "Win32 and COM for Windows Runtime apps (system)" page, these four TLS functions are supported under WinRT. Reading the TlsAlloc documentation, for example, one reads:
Windows Phone 8.1: This function is supported for Windows Phone Store
apps on Windows Phone 8.1 and later. When a Windows Phone Store app
calls this function, it is replaced with an inline call to FlsAlloc.
Refer to FlsAlloc for function documentation.
When I #include the indicated header file, Processthreadsapi.h, in my legacy code, the compile fails:
error C2039: 'TlsAlloc' : is not a member of '`global namespace''
Examining Processthreadsapi.h shows why it doesn't help me:
/***********************************************************************************
* *
* processthreadsapi.h -- ApiSet Contract for api-ms-win-core-processthreads-l1 *
* *
* Copyright (c) Microsoft Corporation. All rights reserved. *
* *
***********************************************************************************/
. . .
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
. . .
#ifndef FLS_OUT_OF_INDEXES
#define FLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
#endif
#define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
_Must_inspect_result_
WINBASEAPI
DWORD
WINAPI
TlsAlloc(
VOID
);
WINBASEAPI
LPVOID
WINAPI
TlsGetValue(
_In_ DWORD dwTlsIndex
);
WINBASEAPI
BOOL
WINAPI
TlsSetValue(
_In_ DWORD dwTlsIndex,
_In_opt_ LPVOID lpTlsValue
);
WINBASEAPI
BOOL
WINAPI
TlsFree(
_In_ DWORD dwTlsIndex
);
. . .
#endif /* WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) */
. . .
The problem is clear: WinRT belongs to the world of WINAPI_PARTITION_APP, not WINAPI_PARTITION_DESKTOP. Therefore, when I compile my app for WinRT, I do not get any of these symbols and function declarations.
Is this only a Windows header problem, where MS should have included the Tls* functions for WinRT? Or is thread local storage not supported for WinRT, contrary to the documentation?

Yes, it truly does. Copy/pasted from c:\Program Files (x86)\Windows Phone Kits\8.1\Include\minwin\processthreadsapi.h, line #411:
// TlsAlloc, TlsFree, TlsGetValue and TlsSetValue are defined as inlines
// only for the store apps, not for desktop apps
#pragma region Application Family
#if WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP
#include <fibersapi.h>
FORCEINLINE
_Must_inspect_result_
WINBASEAPI
DWORD
WINAPI
TlsAlloc(
VOID
)
{
return FlsAlloc(NULL);
}
// etc..
The file that you describe is the 8.0 version of the file. No idea how this happened of course. Perhaps you have an early beta, perhaps you copied files to solve a problem. The landing page is here, I did download the 8.1 Update 1 emulators but don't know if that also updated the SDK headers.

This is only added in VS 2013 Update 4. If you have an earlier install (including Update 4 RC) you won't have the updated headers.

An alternative is __declspec(thread), which may be easier to use. It likely maps to the same functionality.

Related

how to lock the screen programmatically in windows phone 8?

We can prevent the screen to lock using the below code
PhoneApplicationService.Current.ApplicationIdleMode = IdleDetectionMode.Disabled
and
PhoneApplicationService.Current.UserIdleDetectionMode= IdleDetectionMode.Disabled
but how to lock the screen from my app. Like the below app
http://www.windowsphone.com/en-us/store/app/one-touch-lockscreen/a3b1220b-1f9a-4bf0-93bc-21ed02792279
Thanks in advance
It's pretty hacky. It's not in the official API, so it could stop working at any time, just like the volume control API. Anyway, it you want to do it, you need to use this external method:
[System.Runtime.InteropServices.DllImport("ShellChromeAPI.dll")]
private extern static void Shell_TurnScreenOn(bool value);
For WP8.0 app this needs to be in a Windows Runtime Component (you should reference its output, as the project cannot be referenced).
From what I understand, though, this won't work on WP8.1 devices, so you'll need a separate WP8.1 app and I think it needs to be a XAML (Windows Store) app.
What #yasen wrote is correct.
[DllImport("ShellChromeAPI.dll")]
private extern static void Shell_TurnScreenOn(bool value);
I've tried the following cases:
Runtime 8.1 C# (Passed store certification)
Runtime 8.1 C++ with Runtime Component 8.1 C# (Haven't tried to publish this in store)
Silverlight/DirectX 8.0 C++ (Passed store certification)
Here's the link to my app that's using the last solution mentioned above.
http://www.windowsphone.com/s?appid=38bf5918-025e-4f23-b515-2cac451a84ab
And I've heard about cases in store using Silverlight that supports 8.0 and 8.1.
You can get Screen is locked or not by Windows.Phone.System.SystemProtection.ScreenLocked
but Unfortunately There is no way to lock the screen via code in Windows Phone 7.x or 8.

How to create file using C++ on windows phone 8?

I want to create a file using C++ on windows phone, but I'm not familar with Win32 API, could you give some code snippets how to use them? By the way, here also I want to know that whether the spead of creating a local file with c++ is faster than c# on windows phone platform? Thanks!
The main challenge in C++ is finding the root directory that you can write into.
You don't need to use the Win32 protocol from C++ (and that probably the least portable option too). You also could use standard C FILE* APIs, standard C++ stream APIs or the new WinRT async file APIs.
For example using FILE* (from https://stackoverflow.com/a/15988970/694641).
void SaveToFile()
{
// get local folder (= isolated storage)
auto local = Windows::Storage::ApplicationData::Current->LocalFolder;
auto localFileNamePlatformString = local->Path + "\\game.sav";
FILE* pFile;
auto f = _wfopen_s(&pFile, localFileNamePlatformString->Data(), L"w");
auto res1 = fprintf(pFile, "123456789");
auto res2 = fclose(pFile);
}
I wouldn't worry about the speed of the file unless you're trying to fix a known performance problem.

Disable Windows 8 edge gestures/hot corners for multi-touch applications while running in full screen

I have a full screen AS3 game maby with Adobe AIR that runs in Windows 7.
In this game it may not be easy to exit (think about kiosk mode, only exit by pressing esc and enter a password).
Now I want this game to run in Windows 8. The game is working like expected but the anoying things are these edge gestures/hot corners (left, top, right, bottom) and the shortcuts.
I've read articles but none helped me. People talk about registery edits, but I dont get this working + the user needs to restart his/hers computer.
I want to open my game, turn off gestures/hot corners and when the game closes the gestures/hot corners need to come back available again.
I have seen some applications doing the same what I want to accomplish.
I found this so I am able to detect the gestures. But how to ignore they're actions?
I also read ASUS Smart Gestures but this is for the touch-pad.
And I have tried Classic Shell but I need to disable the edge gestures/hot corners without such programs, just on-the-fly.
I also found this but I don't know how to implement this.
HRESULT SetTouchDisableProperty(HWND hwnd, BOOL fDisableTouch)
{
IPropertyStore* pPropStore;
HRESULT hrReturnValue = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&pPropStore));
if (SUCCEEDED(hrReturnValue))
{
PROPVARIANT var;
var.vt = VT_BOOL;
var.boolVal = fDisableTouch ? VARIANT_TRUE : VARIANT_FALSE;
hrReturnValue = pPropStore->SetValue(PKEY_EdgeGesture_DisableTouchWhenFullscreen, var);
pPropStore->Release();
}
return hrReturnValue;
}
Does anyone know how I can do this? Or point me into the right direction?
I have tried some in C# and C++, but I aint a skilled C#/C++ developer. Also the game is made in AS3 so it will be hard to implement this in C#/C++.
I work on the Lenovo aio (All in one) with Windows 8.
I am making a kiosk application for windows 8 using C#,
I succesfully disabled the swipe gestures by killing the explorer process
when the program starts up. I don't know how it's done in C++
but in C# it's like this.
var explorers = Process.GetProcessesByName("explorer");
foreach (var explorer in explorers) {
explorer.Kill();
}
On the closing event of my program I just start the process again.
Process.Start("explorer.exe");
Before this works you might have to edit your register to prevent explorer
from restarting after you kill it.
In regedit go to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
Set the AutoRestartShell to 0.
(as long as your running off AIR - which it sounds like) You can use AIR 2.0's NativeProcess to execute it.
From there I can think of two solutions:
If there are command line options in Classic Shell, call it from within your AS3 and toggle the gestures on/off through it.
Write a C++ application that toggles the gestures on/off manually.
If you can get that example working in C++, add a bit to the C++ to handle a on/off command line argument and call the C++ exe on initialization and exit of your AS3 application to toggle.
I don't have C++ installed here (at work), but it looks like that C++ function just requires the window handle of your application (hWnd). This tutorial should get you compiled and running, but the window handle you see there will be for the C++ application's window.
Accessing the flash window's handle will be slightly more difficult:
for a direct solution; you'll need to use .NET - Process.MainWindowHandle,
or alternatively without .NET -
How to get main window handle from process id?
Either way you'll need to use the Process name or ID to identify it as your AS3 process.
This solution also depends on your ability to install things other than the AIR app on the client PC. (ie .NET runtimes, C++ runtimes, Classic Shell installer)
I'm trying to do the same thing. The C++ below disables Win8 Gestures on every running application/window. As JonoRR mentioned, the next step would be calling it only on the HWND you would like to target. If the targeted application is closed, then re-opened, the gestures will return.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <propsys.h>
#include <propkey.h>
using namespace std;
HWND windowHandle;
HRESULT SetTouchDisableProperty(HWND hwnd, BOOL fDisableTouch)
{
IPropertyStore* pPropStore;
HRESULT hrReturnValue = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&pPropStore));
if (SUCCEEDED(hrReturnValue))
{
PROPVARIANT var;
var.vt = VT_BOOL;
var.boolVal = fDisableTouch ? VARIANT_TRUE : VARIANT_FALSE;
hrReturnValue = pPropStore->SetValue(PKEY_EdgeGesture_DisableTouchWhenFullscreen, var);
pPropStore->Release();
}
return hrReturnValue;
}
BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
TCHAR title[500];
ZeroMemory(title, sizeof(title));
GetWindowText(hWnd, title, sizeof(title)/sizeof(title[0]));
SetTouchDisableProperty(hWnd,true);
_tprintf(_T("Value is %s\n"), title);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumWindows(MyEnumProc, 0);
return 0;
}
The answer below is based on Dovogja's brilliant answer, but it uses a batch file to do it. This way you just have to launch with a launch.bat
Windows charms bar is operated by explorer.exe. So if your app can run without it then you can hack around it by first disabling the autorestart of explorer.exe via (run as administrator):
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "AutoRestartShell" /t REG_DWORD /d 0
Then the lines below represent my launch.bat - which works in the end as expected:
;; kill explorer (this disables all windows functionalities
taskkill /f /im explorer.exe
;; start your kiosk app - should block the batch execution (so explorer.exe doesn't get executed at the end)
"\path\to\your\app.exe"
;; after you close both the app window and the opened chrome window relaunch explorer.exe to give back the functionality to windows
explorer.exe
I use the approach outlined above to let a keyboardless kiosk app run. Because with a keyboard you can still close the app with alt+f4.

FireBreath does not compile with boost process header file included

I wanted to launch the process from firebreath,so I included "boost/process.hpp". Just including this header alone throws compile error.
could some one help?
FireBreath 1.6.1, FireBreath 1.7.0,windows 8
Error:
2>C:\Projects\plugin\FireBreath\src\3rdParty\boost\boost/process/detail/pipe.hpp(129): error C2665: 'boost::system::system_error::system_error' : none of the 7 overloads could convert all the argument types
2> C:\Projects\plugin\FireBreath\src\3rdParty\boost\boost/system/system_error.hpp(39): could be 'boost::system::system_error::system_error(int,const boost::system::error_category &,const std::string &)'
2> C:\Projects\plugin\FireBreath\src\3rdParty\boost\boost/system/system_error.hpp(43): or 'boost::system::system_error::system_error(int,const boost::system::error_category &,const char *)'
2> while trying to match the argument list '(DWORD, overloaded-function, const char [54])'
I faced same problem
boost: 1.47
OS: Windows XP (32 bit VM)
But not with FireBreath
I changed boost::system::system_category to boost::system::system_category() in pipe.hpp line 129
the boost::process library that was in FireBreath has compilation errors in them. I have fixed those errors and it now compiles for me, at least when including . I haven't actually tried to use it on windows yet, nor I suspect has anyone else.
Good luck, hope that helps.

CUDA plugin dlopen

I've written a cuda plugin (dynamic library), and I have a program written in C which uses dlopen() to load this plugin. I am using dlsym() to get the functions from this plugin. For my application it is very important that any time of loading plugin the program gets a new handle with dlopen() calling (the library file may modified subsequently).
Therefore after the using of functions from my plugin I invoke the dlclose(). The invocations dlopen() - dlsym() - dlclose() are occur during my program execution (in the loop).
If I working on the computer with NVIDIA driver 256.35 (CUDA 3.0 or 3.1) I have a memory leak (I use in my plugin cudaMemGetInfo() calling for the diagnostics).
If I working on the computer with NVIDIA driver 195.36.15 (CUDA 3.0) I have an error after some time of the program execution: “NVIDIA: could not open the device file /dev/nvidia0 (Too many open files).”
If I don't use the dlclose() invocation the program is working fine, but in this case I can't replace the plugin on a new one's during my program execution.
Anyone encountered this problem?
Thanks.
Nobody wrote plugins on CUDA?
I've found the similar example on CUDA SDK: matrixMulDynlinkJIT. I've done small correction in the code. In particular, in the file cuda_drvapi_dynlink.c I've corrected cuInit() function:
CUDADRIVER CudaDrvLib = NULL;
CUresult CUDAAPI cuInit(unsigned int Flags)
{
//CUDADRIVER CudaDrvLib;
CUresult result;
int driverVer;
if (CudaDrvLib != NULL) {
dlclose (CudaDrvLib);
CudaDrvLib = NULL;
}
.......
}
And in the file matrixMulDynlinkJIT.cpp I've added loop in the main() function:
int main(int argc, char** argv)
{
printf("[ %s ]\n", sSDKsample);
while (1) {
// initialize CUDA
CUfunction matrixMul = NULL;
cutilDrvSafeCallNoSync(initCUDA(&matrixMul, argc, argv));
.....
}//while (1)
cutilExit();
}
So, I have the same problem like in my program (after some time execution): “NVIDIA: could not open the device file /dev/nvidia0 (Too many open files).”
But when I comment out the dlclose() in the cuda_drvapi_dynlink.c file – all works fine
I can't understand this behavior...
Any ideas?