如何减小VB6编译后的可执行文件的大小?
我的 VB6 程序当前编译为 6MB 的可执行文件。我想这对于今天的机器来说并不是真正的问题,但我想将其减少到兆字节以下。
最好的方法是什么?
注意:我已经尝试过“优化小代码”。这实际上将 EXE 增加到了 16MB!
如果解决方案是将某些代码分离到单独的 DLL 中,那么最好的方法是什么?是否应该将所有表单保留在 EXE 中并将所有类和模块放入 DLL 中?我如何知道具体是什么原因导致 EXE 如此之大?
My VB6 program currently compiles to a 6MB executable. I guess this is not really a problem with today's machines, but I would like to reduce it to under a megabyte.
What would the best way to do this?
Note: I have already tried doing "Optimise for small code". This actually increased the EXE to 16MB!
If the solution is to separate some of the code into a separate DLL, what would be the best way to do this? Should you leave all the forms in the EXE and put all the classes and modules in a DLL? How do I know what in particular is causing the EXE to be so big?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以试试这个压缩器。
http://www.exetools.com/files/compressors/win/petite22.zip
保护并压缩您的 Vb6 exe 文件。
You can try this compressor.
http://www.exetools.com/files/compressors/win/petite22.zip
Secure and compress your Vb6 exe file.
试试这个压缩器:UPX。它免费且快速。
Try this compressor:UPX. It's free and fast.
很可能您的代码不是问题,问题是资源。例如,您可能在某种形式的图片框控件中放置了一个大图像。图像保存为未压缩的位图,这就是 EXE 大小的原因。如果这是您的问题,请将图像从应用程序中移出并移至外部 JPG 中。
另一个示例是,如果您在应用程序中有一堆报告,并且每个报告的页眉/页脚中都有公司徽标。将徽标集中在其他地方并通过辅助方法访问它。
Most likely your code is not the problem, the resources are. For instance, you probably have a large image plopped in a picturebox control on some form. The image is saved as an uncompressed bitmap and that is what's contributing to the size of the EXE. If that is your issue, move the image out of the app and into an external JPG.
Another example is if you have a bunch of reports in the app and each of the reports has the company logo in the header/footer. Centralize the logo elsewhere and access it via a helper method.
我必须说,编译为 P 代码,它应该位于 Make 选项卡高级选项中的某处。它将我的 EXE 从 700 KB 减少到 200 KB(差异很大)。然后,按照其他人的建议减少图像/声音/其他资源。
另外,检查你的图标!某些图标的每个图标最多可达 250KB - 如果您有多个带有多个不同图标的表单,那么这可能会加起来。典型的现代图标大约为 50 KB,但也可能更多,因为不同的图标分辨率会针对不同的颜色和显示设置进行存储,因此在一个图标 .ico 文件中,您将拥有 4 个用于高图形显示的图标图像,另外 4 个对于较低的图形显示(256 位颜色),另外 4 个用于 16 位颜色等,您最终可能会在一个 .ico 文件中为一种表单提供 16 - 20 个图标图像。您可以删除图标图像,例如一系列 gif 图像中的静态图片,可以逐帧删除它们(在本例中是逐个图标图片),也可以删除 16 位和 256 位颜色选项。
现在,我的下一个建议是,使用 #IF 指令删除应用程序中任何用户永远看不到的任何调试/报告文本(最终用户不需要的字符串)和/或任何附加功能您用于最终用户不需要的测试/调试或报告...您可以使用#IF指令删除所有这些。您可以通过在属性窗口中设置常量布尔值来一次性打开/关闭所有 #IF 指令(条件编译参数),有关这方面的更多信息可以在我的一篇有关 #IF 指令和文件大小的帖子中找到:<一href="https://stackoverflow.com/questions/13320285/when-i-use-conditional-compilation-arguments-to-exclude-code-why-doesnt-vb6-ex">当我使用条件编译参数进行排除时代码,为什么 VB6 EXE 文件大小没有改变?(另外,有很多很棒的 StackOverflow 帖子描述了如何使用 #IF THEN #ELSE 进行条件编译#END IF 指令,只需搜索)。
最后,根据我的最后建议,在仅使用 p 代码编译将文件大小减少 3 到 4 倍之后,您可以使用像样的 EXE 压缩器将文件大小再减少 3 到 4 倍!因此,仅使用 p 代码和 exe 压缩,我非常确定您可以将 6MB 文件压缩到 600KB 以下(甚至无需进行任何与 #IF 指令、图标文件和其他图像/声音资源相关的其他优化)。由于 p 代码编译与压缩无关,因此通过添加 exe 压缩,您可以在不丢失 .exe 扩展名的情况下大幅减小文件大小。通过使用好的压缩器,我并不是在谈论将其更改为 .zip 文件或 .rar 文件或任何其他文件。压缩后,它仍然会保留为.exe,同时也被压缩,人们可以正常运行它并使用它,不会注意到任何差异,因此,一旦您的exe被执行,它就会在内存中解压并加载到内存中飞,当你关闭应用程序时,没有任何变化(文件大小仍然相同,小且压缩)。 PECompact GUI 版本是一个不错的选择,甚至可能在某些 torrent 大小上可用,+ 在执行此操作的程序方面有几个免费选项。
总而言之,从 VB6 make tabs advance/compilation 部分进行 p 代码编译,看看有什么不同。然后进行每项优化,重新编译并记下文件大小差异,您将很好地了解每次更改会产生什么影响。重复此操作,直到尝试完所有选项。最大的差异来自于 p 代码编译和 exe 压缩。当然,您应该在 p-code & 之后尝试 resources/icons/#IF 指令。压缩选项,因为 p 代码和压缩是最简单的,并且很可能将文件大小减少到原始大小的 10%(10 x 差异总和)。
最后,让我知道你们相处得怎么样,我现在很好奇结果如何。另外,p 代码可以在所有具有 Visual Basic 运行时的位置/PC 上运行,因此实际上没有任何问题。他们说 p 代码的运行速度比正常编译的代码慢一点,但差异很小,根本不明显。鉴于现在的 PC 速度如此之快,运行速度的差异比 VB6 刚出现时更难注意到(更可以忽略不计),所以我真的认为它是终极免费午餐,尤其是在文件大小很重要的情况下给你。
如果您有任何其他问题,请告诉我。干杯。
I must say, compiles to P-Code, it should be in the Make tab advanced options somewhere. It reduces my EXE from 700 KB's to 200 KB's (big difference). Then, do the image/sound/other resources reductions as others have suggested.
Also, check your icons! Some icons can get up to 250KB's PER ICON - and if you have several forms with several different icons, then this can add up. A typical modern icon is about 50 KB's, but can be way more, as different icon resolutions are stored for different colour and display settings, so in one icon .ico file, you'll have 4 icon images for high graphics display, another 4 for a lower graphics display (256 bit colour) and another 4 for 16 bit colour etc. and you might end up having 16 - 20 icon images in one .ico file for just one form. You can remove the icon images like static pics in a series of gif images, they can be removed frame by frame (or icon pic by icon pic in this case) and 16 bit and 256 bit colour options can be removed as well.
Now, to my next recommendation, use #IF directives to remove any debugging/reporting texts that are in your application that will never be seen by any of the users (strings that are not needed by the end users) and/or any additional functions you use for testing/debugging or reporting that is not necessary to the end user... you can remove all of this using the #IF directive. You can turn all of your #IF directives (conditional compile arguments) on/off in one shot by setting a constant Boolean in your properties window, more info on this can be found in one of my posts re #IF directives and file size here: When I use Conditional Compilation Arguments to Exclude Code, why doesn't VB6 EXE file size change? (also, there are many great StackOverflow posts describing how to do conditional compilation using #IF THEN #ELSE #END IF directives, just search).
Finally, to my last recommendation, after you've reduced your file size by 3 to 4 times its size using p-code compilation alone, you can reduce it by another 3 to 4 times using a decent EXE compressor! So, just using p-code and exe compression I'm pretty sure you can get your 6MB file down under 600KB's (without even doing any of the other optimizations relating to #IF directives, icon files and other image/sound resources). Because p-code compilation is NOT compression related, by adding exe compression, you can get a huge file size reduction without losing the .exe extension. By using a good compressor, I'm not talking about changing it to a .zip file or a .rar file or anything. After compression, it will still remain as a .exe while also being compressed, and people can run it and use it normally without noticing any difference, so, as soon as your exe is executed, it decompresses in memory and loads into memory all on the fly, and when you close the application, nothing changes (file size still the same, small & compressed). PECompact GUI version is a good option, might even be available on some torrent sizes, + several free options in terms of programs that do this.
To sum up, do p-code compilation from your VB6 make tabs advanced / compilation section, look at the difference. Then do each optimization, recompile and note the file size difference and you'll get a good idea what the impact of each change would provide. Repeat until you've tried all options. Biggest differences will come from p-code compilation and exe compression. Of course, resources/icons/#IF directives you should try after the p-code & compression options, since p-code & compression are the easiest and will most likely reduce your file size down to 10% of its original size (10 x difference combined).
Finally, let me know how you get along, I am curious now how it turns out. Also, p-code runs on all locations/PC's that have visual basic runtime, so there is really no catch. They say p-code code runs a little bit slower than normal compiled code, but the difference is so small that it is not noticeable at all. Given that PC's are so fast now days, the running speed difference is even harder to notice (much more negligible) than it was when VB6 first came out, so I really would consider it to be the ultimate free lunch especially if file size is important to you.
Let me know how it goes or if you have any additional questions. Cheers.
如果您正在编写任何新代码,那么您确实应该为 VB.Net 编写。
VB.Net 相对于 VB6 的众多优势之一(除了显而易见的“不再支持 VB6”之外)是 .Net 运行时中内置的大量现成代码(这导致相应更小的。EXE文件)。
除此之外,可能导致 .exe 过大的一件事是使用大量位图。您可能想看看这是否是罪魁祸首。如果是这样,您只需在运行时加载图像即可大大减少文件大小。
If you're writing any new code, you should really be writing for VB.Net.
One of the many advantages of VB.Net over VB6 (outside of the obvious "VB6 isn't supported anymore") is the large set of ready-to-use code built in to the .Net runtime (which results in a correspondingly smaller .exe).
Outside of that, one thing that can cause large .exe's is using lots of bitmaps. You might want to see if that's the culprit. If so, you can get a HUGE reduction in filesize just by loading images at runtime instead.
对于 vb6 应用程序来说 6 MB 似乎很大。
应用程序中可能有一些声音或图像资源
对于图像...使用某些实用程序(例如 Photoshop)来减小大小。
还可以在运行时加载图像。这应该会给你一个更小的exe
6 mb seems large for a vb6 application.
It is possible you have some sound or image resources in the application
For images ... use some utility such as Photoshop to reduce the size.
Also load the images at run time. This should give you a smaller exe