如何使MFC程序在低版本系统上运行 [英] How to make a MFC program run on a low version system

查看:75
本文介绍了如何使MFC程序在低版本系统上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含class和.h(dwmapi.h)文件的程序,仅在Win7系统中可用.我也想使程序在win xp上运行.我找到了一些替代功能来保留原始程序中的功能,但效果却不如原始程序.

I have a program including class and .h(dwmapi.h) file only usable in Win7 system. I want to make the program run on win xp too. I found some alternate functions to carry the features in the original program but it didn’t perform as well as the original one.

Is there a way to keep the class so that when running on win7 it perform as before and when running on xp it execute the alternate functions?

推荐答案

是否链接dwmapi通过使用dwmapi.h中的dwmapi函数并将程序与dwmapi.lib进行链接的静态链接来将程序函数链接到您的程序,Windows XP将无法加载您的exe,因为它无法在winxp上找到dwmapi.dll.为了避免这种情况,您不应该将程序与dwmapi.lib链接,而应该访问dwmapi函数,如下所示:
If you link dwmapi functions to your program with static linking by using dwmapi functions from dwmapi.h and linking your program against dwmapi.lib windows XP will be unable to load your exe because it won''t find dwmapi.dll on winxp. To avoid this you shouldn''t link your program against dwmapi.lib and you should access the dwmapi functions like this:
if (win7)
{
    // load the dwmapi.dll with LoadLibrary() dynamically and get the address of needed functions with GetProcAddress()
}
else
{
    // do xp dependent stuff
}


如果要优雅地执行此操作,则可以定义一个通用接口(带有仅抽象方法的类),该通用接口将在程序中的任何地方使用,并在单独的类中为每个Windows版本实现该接口,其中一个是dwmapi/LoadLibrary/GetProcAddress,然后一种具有XP功能.在您的程序中,您可以在任何地方使用该接口,但是您可以检测Win版本并实例化适当的实现.如果您必须根据Windows版本使用来自不同dll的许多函数,那么在任何地方都将LoadLibrary()和GetProcAddress()与函数指针一起使用是非常不舒服的,因此执行以下操作更加方便:
定义一个公共接口.在2个单独的DLL中创建此公共接口的2个实现.在执行win7实现的DLL中,您可以静态链接到dwmapi.lib,因此您无需弄乱Loadlibrary/getprocaddress/function指针.在您的程序中,您将检测Win版本并加载适当的DLL,并调用其导出的函数之一,该函数返回接口指针.在这里,您可以找到在DLL中实现接口的示例:在C/C ++项目中设计DLL的接口 [


If you want to do this elegantly then you define a a common interface (class with abstract-only methods) that you will use everywhere in your program and you implement it for each windows version in separate classes, one with dwmapi/LoadLibrary/GetProcAddress, and one with XP functions. In your program you use the interface everywhere but you can detect the win version and instantiate the appropriate implementation. If you have to use a lot of functions from different dlls depending on the windows version then its very uncomfortable to use LoadLibrary() and GetProcAddress() with function pointers everywhere so its much more comfortable to do the following:
Define a common interface. Create 2 implementations of this common interface in 2 separate DLLs. In the DLL where you make the win7 implementation you can link against dwmapi.lib statically so you don''t need to mess with Loadlibrary/getprocaddress/function pointers. In your program you detect the win version and load the appropriate DLL and call one of its exported functions that returns the interface pointer. Here you find an example of implementing an interface in a DLL: Designing the interface of DLLs in C/C++ projects[^]. This way you have to mess with DLL loading and GetProcAddress() only once in your program after detecting the windows version.


请回答.
它也很难为多操作系统创建dll,因此我使用第一个解决方案.幸运的是,在我的程序中不需要更改的地方太多了.
Just as the answer say.
It is a diffcult too create dll for multi-os, so I use the first solution. Luckily, the place which have to be changed is not too much in my program.


这篇关于如何使MFC程序在低版本系统上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆