eaf73da198bf8ac1404663d3abb4e9a68f88062c
[scilab.git] / scilab / modules / dynamic_link / src / c / dllinfo.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) DIGITEO - 2011 - Allan CORNET
4
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at    
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12 /*---------------------------------------------------------------------------*/
13 #include <windows.h>
14 #include <Dbghelp.h>
15 #include "dllinfo.h"
16 /*---------------------------------------------------------------------------*/
17 #pragma comment(lib,"Dbghelp.lib")
18 /*---------------------------------------------------------------------------*/
19 static PIMAGE_NT_HEADERS getPEHeader(const wchar_t *dllfilename);
20 /*---------------------------------------------------------------------------*/
21 BOOL isDll(const char *dllfilename)
22 {
23     if (dllfilename)
24     {
25         wchar_t *wcfilename = to_wide_string(dllfilename);
26         if (wcfilename)
27         {
28             BOOL bRes = isDllW(wcfilename);
29             FREE(wcfilename);
30             return bRes;
31         }
32     }
33     return FALSE;
34 }
35 /*---------------------------------------------------------------------------*/
36 BOOL isX64Dll(const char *dllfilename)
37 {
38     if (dllfilename)
39     {
40         wchar_t *wcfilename = to_wide_string(dllfilename);
41         if (wcfilename)
42         {
43             BOOL bRes = isX64DllW(wcfilename);
44             FREE(wcfilename);
45             return bRes;
46         }
47     }
48     return FALSE;
49 }
50 /*---------------------------------------------------------------------------*/
51 BOOL isX86Dll(const char *dllfilename)
52 {
53     if (dllfilename)
54     {
55         wchar_t *wcfilename = to_wide_string(dllfilename);
56         if (wcfilename)
57         {
58             BOOL bRes = isX86DllW(wcfilename);
59             FREE(wcfilename);
60             return bRes;
61         }
62     }
63     return FALSE;
64 }
65 /*---------------------------------------------------------------------------*/
66 BOOL isDllW(const wchar_t *dllfilename)
67 {
68     PIMAGE_NT_HEADERS peHeader = getPEHeader(dllfilename);
69     if (peHeader)
70     {
71         return (peHeader->FileHeader.Characteristics & IMAGE_FILE_DLL);
72     }
73     return FALSE;
74 }
75 /*---------------------------------------------------------------------------*/
76 BOOL isX64DllW(const wchar_t *dllfilename)
77 {
78     PIMAGE_NT_HEADERS peHeader = getPEHeader(dllfilename);
79     if (peHeader)
80     {
81         return (peHeader->FileHeader.Machine & IMAGE_FILE_MACHINE_AMD64);
82     }
83     return FALSE;
84 }
85 /*---------------------------------------------------------------------------*/
86 BOOL isX86DllW(const wchar_t *dllfilename)
87 {
88     PIMAGE_NT_HEADERS peHeader = getPEHeader(dllfilename);
89     if (peHeader)
90     {
91         return (peHeader->FileHeader.Machine & IMAGE_FILE_MACHINE_I386);
92     }
93     return FALSE;
94 }
95 /*---------------------------------------------------------------------------*/
96 static PIMAGE_NT_HEADERS getPEHeader(const wchar_t *dllfilename)
97 {
98     HANDLE hMap = INVALID_HANDLE_VALUE;
99     HANDLE hFile = INVALID_HANDLE_VALUE;
100     void* mapAddr = NULL;
101     PIMAGE_NT_HEADERS peHdr = NULL;
102
103
104     hFile = CreateFileW(dllfilename, 
105         GENERIC_READ,
106         FILE_SHARE_READ,
107         NULL,
108         OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);
109
110     if (hFile == INVALID_HANDLE_VALUE) return NULL;
111
112     hMap = CreateFileMapping(hFile, 
113         NULL,           // security attrs
114         PAGE_READONLY,  // protection flags
115         0,              // max size - high DWORD
116         0,              // max size - low DWORD      
117         NULL );         // mapping name - not used
118
119     if (hMap == INVALID_HANDLE_VALUE) 
120     {
121         CloseHandle(hFile);
122         hFile = INVALID_HANDLE_VALUE;
123         return NULL;
124     }
125
126     // next, map the file to our address space
127     mapAddr = MapViewOfFileEx( 
128         hMap,             // mapping object
129         FILE_MAP_READ,  // desired access
130         0,              
131         0,              
132         0,              
133         NULL );         // suggested map addr
134
135     if (mapAddr == NULL)
136     {
137         CloseHandle(hMap);
138         CloseHandle(hFile);
139         hFile = INVALID_HANDLE_VALUE;
140         hMap = INVALID_HANDLE_VALUE;
141         return NULL;
142     }
143
144     peHdr = ImageNtHeader( mapAddr );
145     if (peHdr)
146     {
147         if (peHdr->FileHeader.SizeOfOptionalHeader == 0)
148         {
149             peHdr = NULL;
150         }
151     }
152
153     CloseHandle(hMap);
154     CloseHandle(hFile);
155     hFile = INVALID_HANDLE_VALUE;
156     hMap = INVALID_HANDLE_VALUE;
157
158     return peHdr;
159 }
160 /*---------------------------------------------------------------------------*/