|
1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // e32test\dll\t_nestdll.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include <e32std.h> |
|
19 #include <e32debug.h> |
|
20 #include <e32svr.h> |
|
21 |
|
22 // Utility macros for stringification, long-stringification, and token pasting |
|
23 // These are indirected so that if you call them on things which are also macros, |
|
24 // the macros are expanded first. |
|
25 #define STRINGIFY_(x) #x |
|
26 #define STRINGIFY(x) STRINGIFY_(x) |
|
27 #define LSTRINGIFY_(x) L ## #x |
|
28 #define LSTRINGIFY(x) LSTRINGIFY_(x) |
|
29 #define PASTE_(x,y) x ## y |
|
30 #define PASTE(x,y) PASTE_(x,y) |
|
31 |
|
32 // Declare the imported functions for the DLLs we statically link to |
|
33 #ifdef LINK_TO |
|
34 IMPORT_C TInt PASTE(DataForDLL,LINK_TO)(); |
|
35 #endif |
|
36 |
|
37 // Dummy class just to get a constructor into the binary |
|
38 class ConstructorTest |
|
39 { |
|
40 public: |
|
41 ConstructorTest(); |
|
42 }; |
|
43 |
|
44 // Constructor, which should be called exactly once per DLL |
|
45 ConstructorTest::ConstructorTest() |
|
46 { |
|
47 RDebug::Printf("In constructor for DLL " STRINGIFY(DLL_NUMBER)); |
|
48 |
|
49 // Call the imported functions for the DLL we statically link to. |
|
50 // This is just to get them into the import table. |
|
51 #ifdef LINK_TO |
|
52 PASTE(DataForDLL,LINK_TO)(); |
|
53 #endif |
|
54 |
|
55 // Use DLL TLS handle zero to fake a global data object |
|
56 TInt* loadOrder = (TInt*)UserSvr::DllTls(0); |
|
57 if (!loadOrder) |
|
58 { |
|
59 // If we're the first constructor, allocate an array and zero it |
|
60 loadOrder = new TInt[100]; |
|
61 for (TInt i = 0; i < 100; ++i) |
|
62 loadOrder[i] = 0; |
|
63 UserSvr::DllSetTls(0, loadOrder); |
|
64 } |
|
65 |
|
66 // Find the first zero entry in the load order array and add our index |
|
67 TInt next = 0; |
|
68 while (loadOrder[next] != 0) |
|
69 ++next; |
|
70 loadOrder[next] = DLL_NUMBER; |
|
71 |
|
72 // If we're supposed to be doing a nested DLL load, do it here. |
|
73 // You aren't supposed to leave in a constructor but it causes an |
|
74 // identifiable panic, to distinguish it from any other possible crash. |
|
75 #ifdef LOAD_DLL |
|
76 RDebug::Printf("Doing nested DLL load"); |
|
77 RLibrary lib; |
|
78 TInt r = lib.Load(TPtrC16((const TText*)(L"t_nestdll" LSTRINGIFY(LOAD_DLL) L".dll"))); |
|
79 RDebug::Printf("Nested DLL load returned %d", r); |
|
80 User::LeaveIfError(r); |
|
81 #endif |
|
82 } |
|
83 |
|
84 // Global instance of the dummy object |
|
85 ConstructorTest constructor_test; |
|
86 |
|
87 // The exported function for this DLL, which just tells you how many times the constructor was called |
|
88 EXPORT_C TInt PASTE(DataForDLL,DLL_NUMBER)() |
|
89 { |
|
90 return 0; |
|
91 } |