gpt4 book ai didi

c++ - 如何缩小 WTL 应用程序的大小?

转载 作者:可可西里 更新时间:2023-11-01 11:26:01 26 4
gpt4 key购买 nike

WTL 应用程序已经非常小了。但是,对于 Win32 配置,使用 VS 2005 和 WTL 9.10 的静态链接应用程序的重量为 136 kB(139,264 字节)。

查看可执行文件时,我注意到 oleaut32.dll 的静态导入。使用 dumpbin 粗略地看一下,显示了一个通过 ordinal 的导入。

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,我发现 VarUI4FromStrATL::CRegParser::AddValue 使用。从那里跟随家属,在 ATL::CRegParser::RegisterSubkeys 中显示了两个调用。

将我的 Visual Studio 安装的 ATL 代码与调查结果进行交叉引用,我发现罪魁祸首是 ATL::CAtlComModule。它做了很多我的用例不需要的 TypeLib 注册工作。

但是,链接器似乎将所有这些都留在了里面,因为它无法合理地决定将其丢弃。

我怎样才能摆脱这个看似多余的导入?

最佳答案

唉,因为WTL::CAppModule直接来自 ATL::CComModule ,包括 atlbase.h header 同时具有 _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 header 不会“注意到”。

#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

非常好,所以我们实际上只缺少两个类函数:GetModuleInstanceInit .让我们把它们也拉进来。为了更好地衡量,我们还将添加 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 import 与来自 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

Thanks to the WDK, which uses msvcrt.dll , a system DLL, as its default (multithreaded dynamically linked) C runtime ,然后您可以通过动态 链接到 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 运行时版本而不是 msvcrt.dll来自以前的 WDK 版本。

关于c++ - 如何缩小 WTL 应用程序的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36161640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com