From daa57f1f333de50fdbb8d0520680b8588db0965f Mon Sep 17 00:00:00 2001 From: anders_k Date: Fri, 6 Oct 2017 13:21:51 +0000 Subject: [PATCH] Optimize away more calls to GetModuleHandle(0) with __ImageBase under MSVC git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@6918 212acab6-be3b-0410-9dea-997c60f758d6 --- Contrib/Library/RegTool/RegTool.cpp | 11 +++++++++-- Source/exehead/Main.c | 12 ++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Contrib/Library/RegTool/RegTool.cpp b/Contrib/Library/RegTool/RegTool.cpp index 724ddcc3..7de36d4e 100644 --- a/Contrib/Library/RegTool/RegTool.cpp +++ b/Contrib/Library/RegTool/RegTool.cpp @@ -46,6 +46,13 @@ template HANDLE CreateFile(const T*p1,DWORD p2,DWORD p3,LPSECURITY_ATTR template BOOL CreateProcess(const T*p1,const T*p2,LPSECURITY_ATTRIBUTES p3,LPSECURITY_ATTRIBUTES p4,BOOL p5,DWORD p6,LPVOID p7,const T*p8,STARTUPINFO*p9,LPPROCESS_INFORMATION p10) { return sizeof(T) > 1 ? ::CreateProcessW(WP(p1),WP(p2),p3,p4,p5,p6,p7,WP(p8),(STARTUPINFOW*)p9,p10) : ::CreateProcessA(NP(p1),NP(p2),p3,p4,p5,p6,p7,NP(p8),(STARTUPINFOA*)p9,p10); } } +#if defined(_MSC_VER) && _MSC_VER >= 1200 +EXTERN_C IMAGE_DOS_HEADER __ImageBase; +#define HINST_THISCOMPONENT ( (HINSTANCE) &__ImageBase ) +#define HINST_APPLICATION HINST_THISCOMPONENT +#else +#define HINST_APPLICATION ( (HINSTANCE) GetModuleHandle(NULL) ) +#endif static bool IsWinNT() { @@ -239,7 +246,7 @@ template void RegFile(T cmd, const T *file, BOOL x64) } else if (!x64) { - if (CALL(GetModuleFileName)(GetModuleHandle(NULL), self, STR_SIZE)) + if (CALL(GetModuleFileName)(HINST_APPLICATION, self, STR_SIZE)) { CALL_wsprintf(cmdline, MKSTR("\"%s\" /%c%s"), self, cmd, file); ready++; @@ -385,7 +392,7 @@ template int RegTool() } { - if (CALL(GetModuleFileName)(GetModuleHandle(NULL), file, STR_SIZE)) + if (CALL(GetModuleFileName)(HINST_APPLICATION, file, STR_SIZE)) { DeleteFileOnReboot(file); } diff --git a/Source/exehead/Main.c b/Source/exehead/Main.c index 5bf955c0..3d40e283 100644 --- a/Source/exehead/Main.c +++ b/Source/exehead/Main.c @@ -49,6 +49,14 @@ #define SHUTDOWN_GRACE_OVERRIDE 0x00000020 #endif +#if defined(_MSC_VER) && _MSC_VER >= 1200 +EXTERN_C IMAGE_DOS_HEADER __ImageBase; +#define HINST_THISCOMPONENT ( (HINSTANCE) &__ImageBase ) +#define HINST_APPLICATION HINST_THISCOMPONENT +#else +#define HINST_APPLICATION ( (HINSTANCE) GetModuleHandle(NULL) ) +#endif + #if !defined(NSIS_CONFIG_VISIBLE_SUPPORT) && !defined(NSIS_CONFIG_SILENT_SUPPORT) #error One of NSIS_CONFIG_SILENT_SUPPORT or NSIS_CONFIG_VISIBLE_SUPPORT must be defined. #endif @@ -146,7 +154,7 @@ EXTERN_C void NSISWinMainNOCRT() #ifndef _WIN64 { - // KEY_WOW64_xxKEY causes registry functions to fail on WinNT4 & Win2000. + // KEY_WOW64_xxKEY flags causes registry functions to fail on WinNT4 & Win2000. // We don't filter them out because all registry instructions are supposed to fail when // accessing a unsupported view and RegKey* takes care of that by looking at the WOW64 flag. FARPROC fp = myGetProcAddress(MGA_IsOS); @@ -191,7 +199,7 @@ EXTERN_C void NSISWinMainNOCRT() mystrcpy(state_command_line, GetCommandLine()); #ifdef NSIS_CONFIG_VISIBLE_SUPPORT - g_hInstance = GetModuleHandle(NULL); + g_hInstance = HINST_APPLICATION; #endif//NSIS_CONFIG_VISIBLE_SUPPORT cmdline = state_command_line;