Aldhard Oswine Posted March 13, 2017 Posted March 13, 2017 (edited) I have following code: #include <Windows.h> #include <cstdio> __declspec(thread) int a; void NTAPI on_tls_callback(PVOID h, DWORD dwReason, PVOID pv) { a = 43; MessageBox(nullptr, L"aaaa", L"bbb", MB_OK); } #pragma data_seg(".CRT$XLB") PIMAGE_TLS_CALLBACK p_thread_callback = on_tls_callback; #pragma data_seg() int main() { printf("%d\n", a); getchar(); } I want to execute on_tls_callback() before main, but it does not work on MSVC 2015. Can you explain how can I use TLS callback? Edited March 13, 2017 by Aldhard Oswine
kao Posted March 13, 2017 Posted March 13, 2017 From what I read, you need to use specific name for the variable, "p_thread_callback" is not good. And you need to use specific linker switches. Reference:http://stackoverflow.com/a/36891752http://www.nynaeve.net/?p=183https://blog.quiscalusmexicanus.org/2010/05/20/tidying-up-tls/ 2
Aldhard Oswine Posted March 13, 2017 Author Posted March 13, 2017 This works: #include <Windows.h> #include <cstdio> __declspec(thread) int a; void NTAPI on_tls_callback(void* dll, DWORD reason, void* reserved) { a = 543; MessageBox(nullptr, L"aaaa", L"bbb", MB_OK); } #pragma comment (linker, "/INCLUDE:__tls_used") #pragma comment (linker, "/INCLUDE:__xl_b") #pragma data_seg(".CRT$XLB") EXTERN_C PIMAGE_TLS_CALLBACK _xl_b = on_tls_callback; #pragma data_seg() int main() { printf("%d\n", a); getchar(); } 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now