출처: http://hi.baidu.com/tjt999/blog/item/116ef30099a219e008fa939f.html

VC in the TLS callback, there are always some problems, basically as follows:
1, VC6 does not support.
2, VS2005 in Debug version of the normal, Release version not normal.
3, VS2005 in Release version of the normal, Debug version is not normal.
VC6 because VC6 does not support the TLSSUP.OBJ a problem with, it has defined the callback table first, and callback to 0,0 means the end of the table, so we added functions will not be called . [INDENT] For the first two issues, I have not encountered, it touches on problems encountered in the first three. On the issue in your research and found the problem: 在 Link middle segment of the process. CRT $ XLA and. CRT $ XLB Hebing O'clock, should be was alphabetical Wu gaps merge, but the output in the DEBUG version of facts is not so, the order of Yes, but it had a big gap, gap filling 0, equivalent to the table in our callback number preceded by 0 0, it is the end of the callback list in advance, perhaps BUG. For the second case, I did not have, do not know whether it is for this reason, if it is, I think should be the LINK of BUG.
    In response to these problems, I wanted to use VS2008 to tlssup.obj, but it's not compatible with VC6, change up too much trouble, then I suddenly thought, maybe we can create a tlssup.obj, based on this idea, write their own The tlssup, the current results show that it can be compatible with VC6, VS2005, VS2008.

/ * File Name: tlssup.c, required to compile the C way, if your project is CPP project, please cancel for this source file precompiled header * /

(1) to establish a console project

(2) create tlssup.c file, the code below

(3) by adding the file works

(4) English version: Right-click on the tlssup.c file, select the Setting-> C / C + + -> Gategory-> Precomliled Headers-> Not using precompiled headers. English Version: Right-click on the tlssup.c File -> Settings -> C / C + + -> precompiled header file -> do not use pre-compensation for the header -> OK

/ / Tlssup.c file code:
# Include <windows.h>
# Include <winnt.h>

int _tls_index = 0;

# Pragma data_seg (". Tls")
int _tls_start = 0;
# Pragma data_seg (". Tls $ ZZZ")
int _tls_end = 0;
# Pragma data_seg (". CRT $ XLA")
int __xl_a = 0;
# Pragma data_seg (". CRT $ XLZ")
int __xl_z = 0;

# Pragma data_seg (". Rdata $ T")

extern PIMAGE_TLS_CALLBACK my_tls_callbacktbl [];

IMAGE_TLS_DIRECTORY32 _tls_used = ((DWORD) & _tls_start, (DWORD) & _tls_end, (DWORD) & _tls_index, (DWORD) my_tls_callbacktbl, 0,0);

/ * Tlssup.c end * /


    Then, we define my_tls_callbacktbl other CPP files as you can:
extern "C" PIMAGE_TLS_CALLBACK my_tls_callbacktbl [] = (my_tls_callback1, 0); / / can have multiple callbacks, but be sure to add an empty item at the last, otherwise it may be wrong.
    Of course, the following line and no less:
# Pragma comment (linker, "/ INCLUDE: __tls_used")

/ / Project cpp file code:

/ / TLS_CallBack_test.cpp: Defines the entry point for the console application.
# Include <windows.h>
# Include <winnt.h>
/ / The following line tells the linker to create the file in the PE TLS directory
# Pragma comment (linker, "/ INCLUDE: __tls_used")
/ * This is PIMAGE_TLS_CALLBACK () function prototype, which the first and third arguments to retain the second parameter determines the function in that case * /
void NTAPI my_tls_callback1 (PVOID h, DWORD reason, PVOID pv)
(
/ * There are four options DLL_PROCESS_ATTACH, DLL_THREAD_ATTACH, DLL_THREAD_DETACH and DLL_PROCESS_DETACH. See Microsoft's release of "Microsoft Portable Executable and Common Object File Format Specification v8" * /
/ / Only create the main thread in the process initialization code to execute when
if (reason == DLL_PROCESS_ATTACH) (
   MessageBox (NULL, "hi, this is tls callback", "title", MB_OK);
)
return;
)
/ * The following section of this is to create a tls
". CRT $ XLB" means:
. CRT that is to use the C RunTime mechanism
$ XLB in the back
That the identity of random X
L TLS callback section that is
B to B can be replaced by any of a letter Y, but can not use ". CRT $ XLA" and ". CRT $ XLZ"
Because ". CRT $ XLA" and ". CRT $ XLZ" for tlssup.obj of
* /
# Pragma data_seg (". CRT $ XLB")
/ * If you want to define multiple TLS_CallBack function can be written in the following sentence:
PIMAGE_TLS_CALLBACK p_thread_callback [] = (tls_callback_A, tls_callback_B, tls_callback_C, 0);
One tls_callback_B and tls_callback_C should be your other TLS_callBack function defined
* /
extern "C" PIMAGE_TLS_CALLBACK my_tls_callbacktbl [] = (my_tls_callback1, 0);
# Pragma data_seg ()

int main (void)
(
MessageBox (NULL, "hi, this is main ()"," title", MB_OK);
return 0;
)

/ * CPP file end * /

'rexxxxx' 카테고리의 다른 글

PEcompact MUP  (0) 2015.09.19
어셈_조건분기문  (0) 2009.10.23
Posted by applicationlayer
: