GetPackageInfo - C++ Builder

C++ Builder 参考手册 ➙ System::Sysutils ➙ GetPackageInfo


获取组件包 (.bpl) 的信息

头文件:#include
命名空间:System::Sysutils
函数原型:

void __fastcall GetPackageInfo(NativeUInt Module, void * Param, int &Flags, TPackageInfoProc InfoProc);

其中参数 InfoProc 为回调函数:

typedef void __fastcall (*TPackageInfoProc)(const System::UnicodeString Name, TNameType NameType, System::Byte Flags, void * Param);

参数:

  • Module:组件包 (.bpl) 的句柄,由函数 LoadPackage 返回的句柄;
  • Param:这个参数直接传递给参数 InfoProc 回调函数的 Param 参数;
  • Flags:组件包属性,本文后面介绍的 TPackageFlags 类型;
  • InfoProc:回调函数,枚举到组件包的每个组成部分都会调用这个回调函数;
    • Name:组件包组成部分的名称;
    • NameType:组件包组成部分的类型:
      ◦ ntContainsUnit:包含的单元;
      ◦ ntRequiresPackage:依赖的组件包 (.bpl);
      ◦ ntDcpBpiName:dcp/bpi 的名称;
    • Flags:
      ◦ 如果 NameType 是包含的单元 (ntContainsUnit 类型),为本文后面介绍的 TPackageUnitFlags 类型;
      ◦ 如果 NameType 是其他类型,Flags 无意义;
    • Param:GetPackageInfo 的 Param 直接传递到回调函数的 Param 参数;

返回值:

  • 通过参数 Flags 和回调函数 InfoProc 返回组件包的信息,函数没有返回值;

  • 组件包属性 TPackageFlags 类型
    • 这个类型没有包含在任何头文件里面,"帮助" 里面也没有说明,
    • 以下内容根据源码的注释编写,注释为源码里面的原注释

typedef union
{
    struct
    {
        unsigned long NeverBuild:1;       // 1: never-build, 0: always build
        unsigned long DesignTimeOnly:1;   // 1: design-time only, 0: not design-time only
        unsigned long RunTimeOnly:1;      // 1: run-time only, 0: not run-time only
        unsigned long NotCheckDupUnits:1; // 1: do not check for dup units, 0: perform normal dup unit check
        unsigned long Reserved0425:22;    // reserved
        unsigned long Producer:2;         // 0: pre-V4, 1: undefined, 2: C++, 3: Pascal
        unsigned long Reserved2829:2;     // reserved
        unsigned long FileType:2;         // 0: EXE, 1: Package DLL, 2: Library DLL, 3: undefined
    };
    unsigned long Value;
    int Flags;
} TPackageFlags;
组件包属性 说明
NeverBuild 1: 不编译,0: 总是编译
DesignTimeOnly 1: 只是设计期间组件包,0: 非只是设计期间组件包
RunTimeOnly 1: 只是运行期间组件包,0: 非只是运行期间组件包
NotCheckDupUnits 1: 不检查重复单元,0: 正常检查重复单元
Reserved0425 (预留)
Producer 生成组件包的语言:0: 4.0之前的版本, 1: 未定义, 2: C++, 3: Pascal
Reserved2829 (预留)
FileType 0: EXE, 1: 组件包 bpl, 2: 动态运行库 dll, 3: 未定义
  • 组件包单元属性 TPackageUnitFlags 类型
    • 这个类型没有包含在任何头文件里面,"帮助" 里面也没有说明,
    • 以下内容根据源码的注释编写,注释为源码里面的原注释
typedef union
{
    struct
    {
        unsigned char MainUnit:1;           // main unit
        unsigned char PackageUnit:1;        // package unit (dpk source)
        unsigned char WeakPackageUnit:1;    // $WEAKPACKAGEUNIT unit
        unsigned char OCWeakPkgUnit:1;      // original containment of $WEAKPACKAGEUNIT (package into which it was compiled)
        unsigned char ImplicitlyImported:1; // implicitly imported
        unsigned char Reserved:3;           // reserved
    };
    unsigned char Value;
    System::Byte Flags;
} TPackageUnitFlags;
组件包单元属性 说明 (1: 包含这个属性,0: 不包含这个属性)
MainUnit 主单元
PackageUnit 组件包单元,dpk 源码
WeakPackageUnit Delphi 的 $WEAKPACKAGEUNIT 单元 (实现部分在 .dcp 里面,不在 .bpl 里面)
OCWeakPkgUnit $WEAKPACKAGEUNIT 单元在这个单元里面编译
ImplicitlyImported 隐含的导入(implicitly imported)
Reserved (预留)

例子:获取《C++ Builder 创建和使用动态加载的包 (.bpl)》生成的 HsuanluTestPackage.bpl 的信息

typedef union
{
    struct
    {
        unsigned long NeverBuild:1;       // 1: never-build, 0: always build
        unsigned long DesignTimeOnly:1;   // 1: design-time only, 0: not design-time only
        unsigned long RunTimeOnly:1;      // 1: run-time only, 0: not run-time only
        unsigned long NotCheckDupUnits:1; // 1: do not check for dup units, 0: perform normal dup unit check
        unsigned long Reserved0425:22;    // reserved
        unsigned long Producer:2;         // 0: pre-V4, 1: undefined, 2: C++, 3: Pascal
        unsigned long Reserved2829:2;     // reserved
        unsigned long FileType:2;         // 0: EXE, 1: Package DLL, 2: Library DLL, 3: undefined
    };
    unsigned long Value;
    int Flags;
} TPackageFlags;

typedef union
{
    struct
    {
        unsigned char MainUnit:1;           // main unit
        unsigned char PackageUnit:1;        // package unit (dpk source)
        unsigned char WeakPackageUnit:1;    // $WEAKPACKAGEUNIT unit
        unsigned char OCWeakPkgUnit:1;      // original containment of $WEAKPACKAGEUNIT (package into which it was compiled)
        unsigned char ImplicitlyImported:1; // implicitly imported
        unsigned char Reserved:3;           // reserved
    };
    unsigned char Value;
    System::Byte Flags;
} TPackageUnitFlags;

template
UnicodeString EnumToString(T t)
{
    return Typinfo::GetEnumName(__delphirtti(T), (int)t);
}

UnicodeString PackageFlagsStr(TPackageFlags pf)
{
    UnicodeString s;

    s += pf.NeverBuild       ? L"never-build\r\n"                : L"always build\r\n";
    s += pf.DesignTimeOnly   ? L"design-time only\r\n"           : L"not design-time only\r\n";
    s += pf.RunTimeOnly      ? L"run-time only\r\n"              : L"not run-time only\r\n";
    s += pf.NotCheckDupUnits ? L"do not check for dup units\r\n" : L"perform normal dup unit check\r\n";

    s += L"Producer: ";
    switch(pf.Producer)
    {
        case 0: s += L"pre-V4\r\n"; break;
        case 1: s += L"undefined\r\n"; break;
        case 2: s += L"C++\r\n"; break;
        case 3: s += L"Pascal\r\n"; break;
    }

    s += L"FileType: ";
    switch(pf.FileType)
    {
        case 0: s += L"EXE\r\n"; break;
        case 1: s += L"Package DLL\r\n"; break;
        case 2: s += L"Library DLL\r\n"; break;
        case 3: s += L"undefined\r\n"; break;
    }
    return s;
}

UnicodeString UnitFlagsStr(TPackageUnitFlags uf)
{
    UnicodeString s;
    if(uf.MainUnit          ){ if(!s.IsEmpty())s+=L" | "; s+=L"MainUnit";           }
    if(uf.PackageUnit       ){ if(!s.IsEmpty())s+=L" | "; s+=L"PackageUnit";        }
    if(uf.WeakPackageUnit   ){ if(!s.IsEmpty())s+=L" | "; s+=L"WeakPackageUnit";    }
    if(uf.OCWeakPkgUnit     ){ if(!s.IsEmpty())s+=L" | "; s+=L"OCWeakPkgUnit";      }
    if(uf.ImplicitlyImported){ if(!s.IsEmpty())s+=L" | "; s+=L"ImplicitlyImported"; }
    return s;
}

void __fastcall PackageInfoProc(const System::UnicodeString Name, TNameType NameType, System::Byte Flags, void * Param)
{
    TForm1 *pForm = (TForm1*)Param;
    pForm->Memo1->Lines->Add(Name);
    pForm->Memo1->Lines->Add(L"    NameType: " + EnumToString(NameType));
    if(NameType == ntContainsUnit)
    {
        TPackageUnitFlags uf;
        uf.Flags = Flags;
        pForm->Memo1->Lines->Add(L"    Flags: " + UnitFlagsStr(uf));
    }
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    try
    {
        NativeUInt hPackage = Sysutils::LoadPackage(L"HsuanluTestPackage.bpl");

        TPackageFlags pf;
        GetPackageInfo(hPackage, this, pf.Flags, PackageInfoProc);
        Memo1->Lines->Add(L"\r\nPackage flags:");
        Memo1->Lines->Add(PackageFlagsStr(pf));

        Sysutils::UnloadPackage(hPackage);
    }
    catch(Exception &e)
    {
        ShowMessage(e.Message);
    }
}

运行结果:

运行结果

相关:

  • System::Sysutils::LoadPackage
  • System::Sysutils::UnloadPackage
  • System::Sysutils::InitializePackage
  • System::Sysutils::FinalizePackage
  • System::Sysutils::GetModuleName
  • System::Sysutils::GetPackageInfo
  • System::Sysutils::GetPackageDescription
  • System::Sysutils::GetPackageTargets
  • System::Sysutils::EPackageError
  • System::Sysutils
  • C++ Builder 创建和使用动态加载的包 (.bpl)

C++ Builder 参考手册 ➙ System::Sysutils ➙ GetPackageInfo

你可能感兴趣的:(GetPackageInfo - C++ Builder)