阅读《COM本质论》Page 8 中关于软件分发和C++问题
有一个dll可以在o(1)时间阶内完成子串搜索
//    faststring.h文件
class __declspec(dllexport) FastString{
    char *m_psz;
public:
    FastString(const char *psz);
    ~FastString(void);
    int Length(void) const;
    int Find(const char *psz) const;
}//    faststring.cpp文件
#include "faststring.h"
#include <string.h>
FasrString::FastString(const char *psz):m_psz(new char[strlen(psz) + 1]){
    strcpy(m_psz, psz);
}FastString::~FastString(void){
    delete[] m_psz;
}int FastString::Length(void) const{
    return strlen(m_psz);
}int FastString::Find(const char *psz) const{
    //O(1) 查找代码,为简明起见从略
}
将上面的文件编译成dll,然后分发给客户。将上面的文件分发给客户以后,发现Length的操作不是很理想,FastString::Length使用C运行库的strlen过程,是一个O(n)算法,所以修改程序如下
//    faststring.h文件
class __declspec(dllexport) FastString{
    const int m_cch;
    char *m_psz;
public:
    FastString(const char *psz);
    ~FastString(void);
    int Length(void) const;
    int Find(const char *psz) const;
}//    faststring.cpp文件
#include "faststring.h"
#include <string.h>
FasrString::FastString(const char *psz):m_cch(strlen(psz)), m_psz(new char[strlen(psz) + 1]){
    strcpy(m_psz, psz);
}FastString::~FastString(void){
    delete[] m_psz;
}int FastString::Length(void) const{
    return m_cch;
}int FastString::Find(const char *psz) const{
    //O(1) 查找代码,为简明起见从略
}将新的dll编译后分发给客户,客户将新的dll拷贝覆盖旧的dll,客户的程序仍然可以继续运行。为什么在文章中说到,“启动以前安装的一个应用,碰巧它也要用到FastString DLL 。在最初几分钟,一切都很正常。然后,突然出现了一个对话框,提示发生了一个异常,并且用户的所有工作都丢失了。”我按照文章的内容写了一个dll和一个使用该dll的应用程序,然后修改dll的Find函数,在将新的dll拷贝到应用程序目录下,应用程序仍然可以使用,没有出现问题。谁能告诉我原因,帮我搞清楚作者想阐述的内容。

解决方案 »

  1.   

    就是两个软件本来是用同一个DLL结果,现在安装一个新版本的使用的DLL是2。0的,没有了以前的那个DLL了,对于没有重新安装的那个应用程序运行的时候  可能    会出现异常
      

  2.   

    我试过,原先的应用程序在新的dll2.0下也是可以用的,运行了几个应用程序都正常。
      

  3.   

    主要是修改dll输出类的成员属性,就是修改它的内存结构,由于使用dll 1.0的应用程序在的编译的时候决定了使用dll输出类的内存结构与使用dll 2.0的应用程序的不一致,光改函数是不会有作用的
      

  4.   

    哪里和哪里阿,接口不变还没讲呢
    ,接口不变就不能是这样定义class __declspec(dllexport) FastString{
        char *m_psz;
    ================================
    ==================================
    public:
        FastString(const char *psz);
        ~FastString(void);
        int Length(void) const;
        int Find(const char *psz) const;
    }//    faststring.cpp文件
    #include "faststring.h"
    #include <string.h>
    FasrString::FastString(const char *psz):m_psz(new char[strlen(psz) + 1]){
        strcpy(m_psz, psz);
    }FastString::~FastString(void){
        delete[] m_psz;
    }int FastString::Length(void) const{
        return strlen(m_psz);
    }int FastString::Find(const char *psz) const{
        //O(1) 查找代码,为简明起见从略
    }
    将上面的文件编译成dll,然后分发给客户。将上面的文件分发给客户以后,发现Length的操作不是很理想,FastString::Length使用C运行库的strlen过程,是一个O(n)算法,所以修改程序如下
    //    faststring.h文件
    class __declspec(dllexport) FastString{
        const int m_cch;
    ======================
        char *m_psz;
    ============================
    public:
        FastString(const char *psz);
        ~FastString(void);
        int Length(void) const;
        int Find(const char *psz) const;
    }
      

  5.   

    1生成一个dll 1.0以及两个应用
    2修改dll 成为2.0,并且修改其中一个应用
    3在没修改的应用中使用dll 2.0
    dll 要输出类,接口的设计也要有一定的要求
    异常肯定是有的,因为这是c++,编译器为我们做的很多的优化