WTL应用程序已经非常小了。但是,使用VS 2005,WTL 9.10的静态链接应用程序的重量为136 kB(139,264字节),用于Win32配置。
查看可执行文件时,我注意到oleaut32.dll
的静态导入。使用dumpbin
粗略显示通过序数进行一次导入。
OLEAUT32.dll
4181C0 Import Address Table
41C9B8 Import Name Table
0 time date stamp
0 Index of first forwarder reference
Ordinal 277
检查oleaut32.dll
发现导出名为VarUI4FromStr
。
使用IDA挖掘一下,我发现VarUI4FromStr
使用了ATL::CRegParser::AddValue
。在那里的家属之后,在ATL::CRegParser::RegisterSubkeys
显示了两个电话。
交叉引用我的Visual Studio安装的ATL代码和调查结果,我发现罪魁祸首是ATL::CAtlComModule
。它做了很多TypeLib注册的东西,对我的用例来说根本就不需要。
但是,链接器似乎将所有内容都留下了,因为它无法合理地决定将其丢弃。
如何摆脱这种看似多余的导入?
答案 0 :(得分:3)
唉,因为WTL::CAppModule
直接来自ATL::CComModule
,包括atlbase.h
标题,而_ATL_NO_COMMODULE
定义会导致错误:
Error 1 fatal error C1189: #error : WTL requires that _ATL_NO_COMMODULE is not defined $(ProjectDir)\wtl\Include\atlapp.h 33
最终引入ATL::CComModule
的实际罪魁祸首是ATL::CAtlComModule
。所以我们的目标是摆脱它们。
我们会尝试通过定义atlbase.h
来排除_ATL_NO_COMMODULE
排除所有TypeLib注册码,但在我们完成包含它之后立即取消它。这种方式atlapp.h
和其他WTL标题赢了"注意"。
#define _ATL_NO_COMMODULE
#include <atlbase.h>
#undef _ATL_NO_COMMODULE
#include <atlapp.h>
显然,这让我们遇到了麻烦:
1>atlapp.h(1515) : error C2039: 'CComModule' : is not a member of 'ATL'
1>atlapp.h(1515) : error C2504: 'CComModule' : base class undefined
1>atlapp.h(1524) : error C2653: 'CComModule' : is not a class or namespace name
1>atlapp.h(1543) : error C2653: 'CComModule' : is not a class or namespace name
1>atlapp.h(1625) : error C3861: 'GetModuleInstance': identifier not found
1>atlapp.h(1784) : error C2653: 'CComModule' : is not a class or namespace name
1>atlapp.h(1806) : error C2065: 'm_nLockCnt' : undeclared identifier
但是,看起来我们应该能够快速解决这些错误。
首先让我们检查ATL::CComModule
来自哪里:它在atlbase.h
中定义了和。我们当然可以声明我们自己的类,并且在 #include <atlbase.h>
和#include <atlapp.h>
之间进行挤压(见上文)。
让我们从基础开始,基于atlbase.h
中可以找到的内容:
namespace ATL
{
class CComModule : public CAtlModuleT<CComModule>
{
public:
CComModule() {}
};
};
现在我们得到一组不同的错误:
1>atlapp.h(1524) : error C2039: 'Init' : is not a member of 'ATL::CComModule'
1> stdafx.h(31) : see declaration of 'ATL::CComModule'
1>atlapp.h(1625) : error C3861: 'GetModuleInstance': identifier not found
很好,所以我们实际上只缺少两个类功能:GetModuleInstance
和Init
。让我们把它们拉进来。为了更好地衡量,我们还会添加GetResourceInstance
:
namespace ATL
{
class CComModule : public CAtlModuleT<CComModule>
{
public:
CComModule() {}
HINSTANCE GetModuleInstance() throw() { return _AtlBaseModule.m_hInst; }
HINSTANCE GetResourceInstance() throw() { return _AtlBaseModule.m_hInstResource; }
HRESULT Init(_ATL_OBJMAP_ENTRY*, HINSTANCE, const GUID*) throw() { return S_OK; }
};
};
简短的测试证明这是正常的。而且,oleaut32.dll
导入已从ole32.dll
继续导入三次。非常成功!
结果我们在可执行文件大小上保存了 12 kB 。
你知道缩小WTL应用程序大小的其他很酷的技巧吗?
请注意,根据您的代码使用的ATL::CComModule
的功能,您可能 以回退使用原始代码。唯一的另一种选择是扩展上面的ATL::CComModule
模拟版本来修复你可能遇到的任何编译错误。
我还没有对此进行过广泛的测试,如果遇到任何麻烦,我会报告(通过编辑此答案)。但是,通过在我的更改之前在ATL和WTL代码以及IDA数据库中查看它,我认为这是一个安全的更改。
您也可以使用6001.18002独立WDK(适用于Vista SP1),它支持目标Windows 2000(SP4)和更新版本。它包括ATL 7.0,因此适合构建ATL + WTL应用程序。
为此,您只需将以下两行添加到sources
文件中:
USE_STATIC_ATL=1
ATL_VER=70
由于WDK使用msvcrt.dll
系统DLL作为其默认(多线程动态链接)C运行时,您可以通过动态链接来进一步缩小可执行文件大小 C运行时。在这种情况下,由于特定的C运行时是一个系统DLL(从Windows 2000开始),您可以放心它会起作用。
考虑到所有这一切,您可以构建标准的Windows应用程序(GUI或控制台)和非常小的DLL。
以下是一个示例sources
文件,您可以将其用作模板:
# Name of the program
TARGETNAME=progname
# Prefix used for the intermediate/output paths (e.g. objfre_w2k_x86)
TARGETPATH=obj
# A program, not a driver or DLL or static lib
TARGETTYPE=PROGRAM
# windows == GUI, console == Console, native == no subsystem ...
UMTYPE=windows
# Use Unicode ("wide char") instead of ANSI
UNICODE=1
# By default the WDK build treats warnings as errors - this turns it off
BUILD_ALLOW_ALL_WARNINGS=1
# Link dynamically to msvcrt.dll
USE_MSVCRT=1
# Don't link against any of the ATL DLLs
USE_STATIC_ATL=1
# In the 7600.16385.1 WDK you can use 70 as well, which translates to 71
ATL_VER=70
USE_NATIVE_EH=1
# RTTI is required for some ATL/WTL features
USE_RTTI=1
# GUI programs require these entry points
!IF defined(UNICODE) && $(UNICODE)
UMENTRY=wwinmain
C_DEFINES=$(C_DEFINES) /DUNICODE /D_UNICODE
!ELSE
UMENTRY=winmain
C_DEFINES=$(C_DEFINES) /DMBCS /D_MBCS
!ENDIF
# Include folders
INCLUDES=$(DDK_INC_PATH);$(CRT_INC_PATH);$(SDK_INC_PATH);wtl\Include
# Libraries to link to, not just import libraries
TARGETLIBS=\
$(SDK_LIB_PATH)\kernel32.lib \
$(SDK_LIB_PATH)\user32.lib \
$(SDK_LIB_PATH)\Comctl32.lib \
# Give source files (also resources and .mc) in the current or parent directory
SOURCES=...
7600.16385.1独立的WDK也适用于此。从Windows 8 WDK开始的WDK现在需要一个可以集成的Visual C ++。这也导致二进制文件需要相应的C运行时版本,而不是之前WDK版本的msvcrt.dll
。