1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Windows/DLL.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _UNICODE
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../Common/StringConvert.h"
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "DLL.h"
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _UNICODE
12baa3858d3f5d128a5c8466b700098109edcad5f2repo syncextern bool g_IsNT;
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
15baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NWindows {
16baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NDLL {
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
18baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CLibrary::Free()
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (_module == 0)
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return true;
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // MessageBox(0, TEXT(""), TEXT("Free"), 0);
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // Sleep(5000);
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!::FreeLibrary(_module))
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _module = 0;
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return true;
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
30baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CLibrary::LoadOperations(HMODULE newModule)
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (newModule == NULL)
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!Free())
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _module = newModule;
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return true;
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
40baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CLibrary::LoadEx(LPCTSTR fileName, DWORD flags)
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // MessageBox(0, fileName, TEXT("LoadEx"), 0);
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return LoadOperations(::LoadLibraryEx(fileName, NULL, flags));
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
46baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CLibrary::Load(LPCTSTR fileName)
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // MessageBox(0, fileName, TEXT("Load"), 0);
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // Sleep(5000);
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // OutputDebugString(fileName);
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // OutputDebugString(TEXT("\n"));
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return LoadOperations(::LoadLibrary(fileName));
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _UNICODE
56baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; }
57baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCSysString GetSysPath(LPCWSTR sysPath)
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  { return UnicodeStringToMultiByte(sysPath, GetCurrentCodePage()); }
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
60baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CLibrary::LoadEx(LPCWSTR fileName, DWORD flags)
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (g_IsNT)
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return LoadOperations(::LoadLibraryExW(fileName, NULL, flags));
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return LoadEx(GetSysPath(fileName), flags);
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
66baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CLibrary::Load(LPCWSTR fileName)
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (g_IsNT)
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return LoadOperations(::LoadLibraryW(fileName));
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return Load(GetSysPath(fileName));
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
74baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool MyGetModuleFileName(HMODULE hModule, CSysString &result)
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  result.Empty();
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  TCHAR fullPath[MAX_PATH + 2];
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  DWORD size = ::GetModuleFileName(hModule, fullPath, MAX_PATH + 1);
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (size <= MAX_PATH && size != 0)
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    result = fullPath;
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return true;
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return false;
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef _UNICODE
88baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool MyGetModuleFileName(HMODULE hModule, UString &result)
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  result.Empty();
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (g_IsNT)
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    wchar_t fullPath[MAX_PATH + 2];
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    DWORD size = ::GetModuleFileNameW(hModule, fullPath, MAX_PATH + 1);
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (size <= MAX_PATH && size != 0)
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      result = fullPath;
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return true;
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CSysString resultSys;
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!MyGetModuleFileName(hModule, resultSys))
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  result = MultiByteToUnicodeString(resultSys, GetCurrentCodePage());
106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return true;
107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}}
111