Commit dd4254a3d9d26cd436f984515570ec8cd211ee33

Timothy Gu 2014-10-08T15:50:21

Add linked modules to a separate global list Fixes #2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 972ffb4..1dbec48 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,7 @@ if (BUILD_SHARED_LIBS)
 endif (BUILD_SHARED_LIBS)
 
 add_library(dl ${sources})
+target_link_libraries(dl psapi)
 
 install (TARGETS dl RUNTIME DESTINATION bin
                     LIBRARY DESTINATION lib${LIB_SUFFIX}
diff --git a/Makefile b/Makefile
index 0f32932..aec9ba8 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,8 @@
 # dlfcn-win32 Makefile
 #
 include config.mak
-CFLAGS=-Wall -O3 -fomit-frame-pointer
+CFLAGS = -Wall -O3 -fomit-frame-pointer
+LIBS  += -lpsapi
 
 ifeq ($(BUILD_SHARED),yes)
 	TARGETS += libdl.dll
@@ -33,7 +34,7 @@ libdl.a: $(LIB_OBJS)
 	$(RANLIB) libdl.a
 
 libdl.dll: $(LIB_OBJS)
-	$(CC) $(SHFLAGS) -shared -o $@ $^
+	$(CC) $(SHFLAGS) -shared -o $@ $^ $(LIBS)
 
 libdl.lib: libdl.dll
 	$(LIBCMD) /machine:i386 /def:libdl.def
@@ -60,7 +61,7 @@ lib-install: $(LIBS)
 install: $(INSTALL)
 
 test.exe: test.o $(TARGETS)
-	$(CC) -o $@ $< -L. -ldl
+	$(CC) -o $@ $< -L. -ldl $(LIBS)
 
 testdll.dll: testdll.c
 	$(CC) -shared -o $@ $^
diff --git a/dlfcn.c b/dlfcn.c
index add1f4f..324fafc 100644
--- a/dlfcn.c
+++ b/dlfcn.c
@@ -22,7 +22,9 @@
 #include <stdlib.h>
 #include <crtdbg.h>
 #endif
+#define PSAPI_VERSION 1
 #include <windows.h>
+#include <psapi.h>
 #include <stdio.h>
 
 #define DLFCN_WIN32_EXPORTS
@@ -40,23 +42,25 @@ typedef struct global_object {
 } global_object;
 
 static global_object first_object;
+static global_object first_automatic_object;
+static int auto_ref_count = 0;
 
 /* These functions implement a double linked list for the global objects. */
-static global_object *global_search( HMODULE hModule )
+static global_object *global_search( global_object *start, HMODULE hModule )
 {
     global_object *pobject;
 
     if( hModule == NULL )
         return NULL;
 
-    for( pobject = &first_object; pobject ; pobject = pobject->next )
+    for( pobject = start; pobject; pobject = pobject->next )
         if( pobject->hModule == hModule )
             return pobject;
 
     return NULL;
 }
 
-static void global_add( HMODULE hModule )
+static void global_add( global_object *start, HMODULE hModule )
 {
     global_object *pobject;
     global_object *nobject;
@@ -64,15 +68,22 @@ static void global_add( HMODULE hModule )
     if( hModule == NULL )
         return;
 
-    pobject = global_search( hModule );
+    pobject = global_search( start, hModule );
 
     /* Do not add object again if it's already on the list */
     if( pobject )
         return;
 
-    for( pobject = &first_object; pobject->next ; pobject = pobject->next );
+    if( start == &first_automatic_object )
+    {
+        pobject = global_search( &first_object, hModule );
+        if( pobject )
+            return;
+    }
+
+    for( pobject = start; pobject->next; pobject = pobject->next );
 
-    nobject = malloc( sizeof(global_object) );
+    nobject = malloc( sizeof( global_object ) );
 
     /* Should this be enough to fail global_add, and therefore also fail
      * dlopen?
@@ -86,14 +97,14 @@ static void global_add( HMODULE hModule )
     nobject->hModule = hModule;
 }
 
-static void global_rem( HMODULE hModule )
+static void global_rem( global_object *start, HMODULE hModule )
 {
     global_object *pobject;
 
     if( hModule == NULL )
         return;
 
-    pobject = global_search( hModule );
+    pobject = global_search( start, hModule );
 
     if( !pobject )
         return;
@@ -185,18 +196,40 @@ void *dlopen( const char *file, int mode )
 
     if( file == 0 )
     {
+        HMODULE hAddtnlMods[1024]; // Already loaded modules
+        HANDLE hCurrentProc = GetCurrentProcess( );
+        DWORD cbNeeded;
+
         /* POSIX says that if the value of file is 0, a handle on a global
          * symbol object must be provided. That object must be able to access
          * all symbols from the original program file, and any objects loaded
          * with the RTLD_GLOBAL flag.
          * The return value from GetModuleHandle( ) allows us to retrieve
          * symbols only from the original program file. For objects loaded with
-         * the RTLD_GLOBAL flag, we create our own list later on.
+         * the RTLD_GLOBAL flag, we create our own list later on. For objects
+         * outside of the program file but already loaded (e.g. linked DLLs)
+         * they are added below.
          */
         hModule = GetModuleHandle( NULL );
 
         if( !hModule )
             save_err_ptr_str( file );
+
+
+        /* GetModuleHandle( NULL ) only returns the current program file. So
+	 * if we want to get ALL loaded module including those in linked DLLs,
+	 * we have to use EnumProcessModules( ).
+         */
+        if( EnumProcessModules( hCurrentProc, hAddtnlMods,
+                                sizeof( hAddtnlMods ), &cbNeeded ) != 0 )
+        {
+            DWORD i;
+            for( i = 0; i < cbNeeded / sizeof( HMODULE ); i++ )
+            {
+                global_add( &first_automatic_object, hAddtnlMods[i] );
+            }
+        }
+        auto_ref_count++;
     }
     else
     {
@@ -233,7 +266,7 @@ void *dlopen( const char *file, int mode )
         if( !hModule )
             save_err_str( lpFileName );
         else if( (mode & RTLD_GLOBAL) )
-            global_add( hModule );
+            global_add( &first_object, hModule );
     }
 
     /* Return to previous state of the error-mode bit flags. */
@@ -242,6 +275,21 @@ void *dlopen( const char *file, int mode )
     return (void *) hModule;
 }
 
+static void free_auto( )
+{
+    global_object *pobject = first_automatic_object.next;
+    if( pobject )
+    {
+        global_object *next;
+        for ( ; pobject; pobject = next )
+        {
+            next = pobject->next;
+            free( pobject );
+        }
+        first_automatic_object.next = NULL;
+    }
+}
+
 int dlclose( void *handle )
 {
     HMODULE hModule = (HMODULE) handle;
@@ -255,7 +303,18 @@ int dlclose( void *handle )
      * objects.
      */
     if( ret )
-        global_rem( hModule );
+    {
+        HMODULE cur = GetModuleHandle( NULL );
+        global_rem( &first_object, hModule );
+        if( hModule == cur )
+        {
+            auto_ref_count--;
+            if( auto_ref_count < 0 )
+                auto_ref_count = 0;
+            if( !auto_ref_count )
+                free_auto( );
+        }
+    }
     else
         save_err_ptr_str( handle );
 
@@ -268,37 +327,47 @@ int dlclose( void *handle )
 void *dlsym( void *handle, const char *name )
 {
     FARPROC symbol;
+    HMODULE hModule;
 
     current_error = NULL;
 
     symbol = GetProcAddress( handle, name );
 
-    if( symbol == NULL )
-    {
-        HMODULE hModule;
+    if( symbol != NULL )
+        goto end;
 
-        /* If the handle for the original program file is passed, also search
-         * in all globally loaded objects.
-         */
+    /* If the handle for the original program file is passed, also search
+     * in all globally loaded objects.
+     */
 
-        hModule = GetModuleHandle( NULL );
+    hModule = GetModuleHandle( NULL );
 
-        if( hModule == handle )
+    if( hModule == handle )
+    {
+        global_object *pobject;
+
+        for( pobject = &first_object; pobject; pobject = pobject->next )
         {
-            global_object *pobject;
+            if( pobject->hModule )
+            {
+                symbol = GetProcAddress( pobject->hModule, name );
+                if( symbol != NULL )
+                    goto end;
+            }
+        }
 
-            for( pobject = &first_object; pobject ; pobject = pobject->next )
+        for( pobject = &first_automatic_object; pobject; pobject = pobject->next )
+        {
+            if( pobject->hModule )
             {
-                if( pobject->hModule )
-                {
-                    symbol = GetProcAddress( pobject->hModule, name );
-                    if( symbol != NULL )
-                        break;
-                }
+                symbol = GetProcAddress( pobject->hModule, name );
+                if( symbol != NULL )
+                    goto end;
             }
         }
     }
 
+end:
     if( symbol == NULL )
         save_err_str( name );
 
@@ -316,3 +385,22 @@ char *dlerror( void )
 
     return error_pointer;
 }
+
+#ifdef SHARED
+BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
+{
+    /*
+     * https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx 
+     *
+     *     When handling DLL_PROCESS_DETACH, a DLL should free resources such as heap
+     *     memory only if the DLL is being unloaded dynamically (the lpReserved
+     *     parameter is NULL).
+     */
+    if( fdwReason == DLL_PROCESS_DETACH && !lpvReserved )
+    {
+        auto_ref_count = 0;
+        free_auto( );
+    }
+    return TRUE;
+}
+#endif
diff --git a/test.c b/test.c
index d6280e4..878694e 100644
--- a/test.c
+++ b/test.c
@@ -31,7 +31,8 @@
 #define CLOSE_LIB    dlclose( library )
 #define CLOSE_GLOBAL dlclose( global  )
 
-#define RETURN_ERROR return 1
+#define RETURN_ERROR printf("From line %d\n", __LINE__); return 1
+
 #define RUNFUNC do { \
                     ret = function (); \
                     if( ret != 0) {    \
@@ -73,6 +74,7 @@ int main()
     void *library;
     char *error;
     int (*function)( void );
+    int (*printf_local)( const char * );
     int (*nonexistentfunction)( void );
     int ret;
 
@@ -106,6 +108,20 @@ int main()
     else
         printf( "SUCCESS\tGot global handle: %p\n", global );
 
+    printf_local = dlsym(global, "printf");
+    if (!printf_local)
+    {
+        error = dlerror();
+        printf("ERROR\tCould not get symbol from global handle: %s\n",
+            error ? error : "");
+        CLOSE_LIB;
+        CLOSE_GLOBAL;
+        RETURN_ERROR;
+    }
+    else
+        printf("SUCCESS\tGot symbol from global handle: %p\n", printf_local);
+    printf_local("Hello world from local printf!\n");
+
     function = dlsym( library, "function" );
     if( !function )
     {
@@ -288,6 +304,19 @@ int main()
                 error ? error : "" );
     }
 
+    function = dlsym(global, "printf");
+    if (!function)
+    {
+        error = dlerror();
+        printf("ERROR\tCould not get symbol from global handle: %s\n",
+            error ? error : "");
+        CLOSE_LIB;
+        CLOSE_GLOBAL;
+        RETURN_ERROR;
+    }
+    else
+        printf("SUCCESS\tGot symbol from global handle: %p\n", function);
+
     ret = dlclose( library );
     if( ret )
     {
diff --git a/visual-studio/12/dl/dl.vcxproj b/visual-studio/12/dl/dl.vcxproj
index cea69bd..45f87e7 100644
--- a/visual-studio/12/dl/dl.vcxproj
+++ b/visual-studio/12/dl/dl.vcxproj
@@ -129,6 +129,7 @@
     </ClCompile>
     <Link>
       <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|Win32'">
@@ -149,6 +150,7 @@
     </ClCompile>
     <Link>
       <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|x64'">
@@ -174,6 +176,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'">
@@ -203,6 +206,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'">
diff --git a/visual-studio/12/dlfcn-win32.sln b/visual-studio/12/dlfcn-win32.sln
index d64cbdb..952a69c 100644
--- a/visual-studio/12/dlfcn-win32.sln
+++ b/visual-studio/12/dlfcn-win32.sln
@@ -30,62 +30,74 @@ Global
 		ReleaseStatic|x64 = ReleaseStatic|x64
 	EndGlobalSection
 	GlobalSection(ProjectConfigurationPlatforms) = postSolution
-		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Debug|ARM.ActiveCfg = Debug|Win32
+		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Debug|ARM.ActiveCfg = ReleaseStatic|x64
+		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Debug|ARM.Build.0 = ReleaseStatic|x64
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Debug|Win32.ActiveCfg = Debug|Win32
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Debug|Win32.Build.0 = Debug|Win32
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Debug|x64.ActiveCfg = Debug|x64
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Debug|x64.Build.0 = Debug|x64
-		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.DebugStatic|ARM.ActiveCfg = DebugStatic|Win32
+		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.DebugStatic|ARM.ActiveCfg = ReleaseStatic|x64
+		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.DebugStatic|ARM.Build.0 = ReleaseStatic|x64
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.DebugStatic|Win32.ActiveCfg = DebugStatic|Win32
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.DebugStatic|Win32.Build.0 = DebugStatic|Win32
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.DebugStatic|x64.ActiveCfg = DebugStatic|x64
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.DebugStatic|x64.Build.0 = DebugStatic|x64
-		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Release|ARM.ActiveCfg = Release|Win32
+		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Release|ARM.ActiveCfg = ReleaseStatic|x64
+		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Release|ARM.Build.0 = ReleaseStatic|x64
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Release|Win32.ActiveCfg = Release|Win32
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Release|Win32.Build.0 = Release|Win32
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Release|x64.ActiveCfg = Release|x64
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.Release|x64.Build.0 = Release|x64
-		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.ReleaseStatic|ARM.ActiveCfg = ReleaseStatic|Win32
+		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.ReleaseStatic|ARM.ActiveCfg = ReleaseStatic|x64
+		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.ReleaseStatic|ARM.Build.0 = ReleaseStatic|x64
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.ReleaseStatic|Win32.ActiveCfg = ReleaseStatic|Win32
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.ReleaseStatic|Win32.Build.0 = ReleaseStatic|Win32
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.ReleaseStatic|x64.ActiveCfg = ReleaseStatic|x64
 		{36E45DAF-D2EC-4660-B84F-3BC72664643E}.ReleaseStatic|x64.Build.0 = ReleaseStatic|x64
-		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Debug|ARM.ActiveCfg = Debug|Win32
+		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Debug|ARM.ActiveCfg = ReleaseStatic|x64
+		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Debug|ARM.Build.0 = ReleaseStatic|x64
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Debug|Win32.ActiveCfg = Debug|Win32
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Debug|Win32.Build.0 = Debug|Win32
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Debug|x64.ActiveCfg = Debug|x64
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Debug|x64.Build.0 = Debug|x64
-		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.DebugStatic|ARM.ActiveCfg = DebugStatic|Win32
+		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.DebugStatic|ARM.ActiveCfg = ReleaseStatic|x64
+		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.DebugStatic|ARM.Build.0 = ReleaseStatic|x64
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.DebugStatic|Win32.ActiveCfg = DebugStatic|Win32
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.DebugStatic|Win32.Build.0 = DebugStatic|Win32
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.DebugStatic|x64.ActiveCfg = DebugStatic|x64
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.DebugStatic|x64.Build.0 = DebugStatic|x64
-		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Release|ARM.ActiveCfg = Release|Win32
+		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Release|ARM.ActiveCfg = ReleaseStatic|x64
+		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Release|ARM.Build.0 = ReleaseStatic|x64
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Release|Win32.ActiveCfg = Release|Win32
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Release|Win32.Build.0 = Release|Win32
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Release|x64.ActiveCfg = Release|x64
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.Release|x64.Build.0 = Release|x64
-		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.ReleaseStatic|ARM.ActiveCfg = ReleaseStatic|Win32
+		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.ReleaseStatic|ARM.ActiveCfg = ReleaseStatic|x64
+		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.ReleaseStatic|ARM.Build.0 = ReleaseStatic|x64
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.ReleaseStatic|Win32.ActiveCfg = ReleaseStatic|Win32
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.ReleaseStatic|Win32.Build.0 = ReleaseStatic|Win32
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.ReleaseStatic|x64.ActiveCfg = ReleaseStatic|x64
 		{583AA2B8-B981-4DE9-A374-47EC9F817C5E}.ReleaseStatic|x64.Build.0 = ReleaseStatic|x64
-		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Debug|ARM.ActiveCfg = Debug|Win32
+		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Debug|ARM.ActiveCfg = ReleaseStatic|x64
+		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Debug|ARM.Build.0 = ReleaseStatic|x64
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Debug|Win32.ActiveCfg = Debug|Win32
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Debug|Win32.Build.0 = Debug|Win32
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Debug|x64.ActiveCfg = Debug|x64
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Debug|x64.Build.0 = Debug|x64
-		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.DebugStatic|ARM.ActiveCfg = DebugStatic|Win32
+		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.DebugStatic|ARM.ActiveCfg = ReleaseStatic|x64
+		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.DebugStatic|ARM.Build.0 = ReleaseStatic|x64
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.DebugStatic|Win32.ActiveCfg = DebugStatic|Win32
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.DebugStatic|Win32.Build.0 = DebugStatic|Win32
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.DebugStatic|x64.ActiveCfg = DebugStatic|x64
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.DebugStatic|x64.Build.0 = DebugStatic|x64
-		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Release|ARM.ActiveCfg = Release|Win32
+		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Release|ARM.ActiveCfg = ReleaseStatic|x64
+		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Release|ARM.Build.0 = ReleaseStatic|x64
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Release|Win32.ActiveCfg = Release|Win32
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Release|Win32.Build.0 = Release|Win32
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Release|x64.ActiveCfg = Release|x64
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.Release|x64.Build.0 = Release|x64
-		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.ReleaseStatic|ARM.ActiveCfg = ReleaseStatic|Win32
+		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.ReleaseStatic|ARM.ActiveCfg = ReleaseStatic|x64
+		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.ReleaseStatic|ARM.Build.0 = ReleaseStatic|x64
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.ReleaseStatic|Win32.ActiveCfg = ReleaseStatic|Win32
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.ReleaseStatic|Win32.Build.0 = ReleaseStatic|Win32
 		{2AEB7143-35F8-488A-9414-CA4B0B45D16E}.ReleaseStatic|x64.ActiveCfg = ReleaseStatic|x64
diff --git a/visual-studio/12/test/test.vcxproj b/visual-studio/12/test/test.vcxproj
index 8773e07..6866897 100644
--- a/visual-studio/12/test/test.vcxproj
+++ b/visual-studio/12/test/test.vcxproj
@@ -128,6 +128,7 @@
     </ClCompile>
     <Link>
       <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|Win32'">
@@ -138,6 +139,7 @@
     </ClCompile>
     <Link>
       <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -148,6 +150,7 @@
     </ClCompile>
     <Link>
       <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|x64'">
@@ -158,6 +161,7 @@
     </ClCompile>
     <Link>
       <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -172,6 +176,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'">
@@ -186,6 +191,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -200,6 +206,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'">
@@ -214,6 +221,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>