在 BDS2006 上包含 DShow.h 会破坏 VCL AnsiString::sprintf()

Including DShow.h breaks VCL AnsiString::sprintf() on BDS2006

我终于有时间升级我的视频捕获 class。我想比较 VFW(到目前为止我一直使用的)和 DirectShow。正如预期的那样,DirectShow 更快,但是当我添加信息文本时,AnsiString::sprint() 突然不再是 AnsiString 的成员。

经过一番努力,我找到了一个解决方法,因为 AnsiString::printf() 仍然有效,但我很好奇如何解决这个问题。也许 dshow.hdstring.h 中的一些定义有冲突?

我删掉了所有不必要的代码来说明这个问题:

//$$---- Form CPP ----
//---------------------------------------------------------------------------
#include <vcl.h>
#include <dshow.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    static int i=0;
    Caption=AnsiString().sprintf("%i",i);               // this does not work
    AnsiString s; s.printf("%i",i); Caption=s;  // this does work
    i++;
}
//---------------------------------------------------------------------------

它只是一个简单的 VCL 表单应用程序,上面只有一个 TTimerTTimer 递增计数器 i 并将其输出到表单的 Caption 中。 DirectX 库甚至没有 linked,只包含 headers!

链接器输出错误:

[C++ Error] Unit1.cpp(20): E2316 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' is not a member of 'AnsiString'

如果我在这一行交换 vcl.h 和 dshow.hincludes, the compiler stops indstring.h`:

AnsiString& __cdecl         sprintf(const char* format, ...); // Returns *this

出现此错误消息:

[C++ Error] dstring.h(59): E2040 Declaration terminated incorrectly

所以,显然存在一些冲突(AnsiString 关键字是问题所在)。将 dshow.h 放入 namespace 也无济于事。

有人有线索吗?

Q1。如何解决这个问题?

Q2。 What/where 到底是什么原因造成的?

我能想到并且应该可行的唯一解决方案(但我想尽可能避免它)是创建一个 OBJ(或 DLL) 与 DirectShow 的东西,然后 link 到一个标准的 VCL 项目而不包括 dshow.h在里面,当然出口也必须没有任何有趣的东西。

我没有 dshow.h 和 dstring.h 的这个版本,所以我无法自己检查,但从你引用的错误消息来看,似乎在 dshow.h 的某个地方或其依赖项,他们声明了一个 "sprintf" 宏。能不能找找看。

为了防止这种行为,您需要删除此宏。使用

#undef sprintf

在包含 dshow.h 的行之后。

问题不在于 dshow.h 本身,而实际上在于 strsafe.hdshow.h 默认包含 strsafe.h

strsafe.h包含以下代码1:

#ifndef STRSAFE_NO_DEPRECATE
// Deprecate all of the unsafe functions to generate compiletime errors. If you do not want
// this then you can #define STRSAFE_NO_DEPRECATE before including this file
#ifdef DEPRECATE_SUPPORTED

...
#pragma deprecated(sprintf)
...

#else // DEPRECATE_SUPPORTED

...
#undef sprintf
#define sprintf     sprintf_instead_use_StringCchPrintfA_or_StringCbPrintfA;
...

#endif  // DEPRECATE_SUPPORTED
#endif  // !STRSAFE_NO_DEPRECATE

1 许多其他已弃用的 "unsafe" C 函数也有类似的 #pragma#define 语句。

如果 STRSAFE_NO_DEPRECATEDEPRECATE_SUPPORTED 都没有定义(在这种情况下就是这种情况),使用 #define sprintf 会导致 所有对 sprintf 符号 的任意种类 在编译期间被视为 sprintf_instead_use_StringCchPrintfA_or_StringCbPrintfA;

这就是您遇到编译器错误的原因。当 vcl.hstrsafe.h 之前包含时,dstring.h 首先包含,因此编译器会看到 正确的 声明 AnsiString::sprintf() 方法,然后 strsafe.h 在编译器看到您的 Timer1Timer() 代码之前被包含(大概是 Unit1.h),所以您对 AnsiString().sprint("%i",i) 的调用实际上是在尝试调用 AnsiString().sprintf_instead_use_StringCchPrintfA_or_StringCbPrintfA;("%i",i),失败了。

当您交换 vcl.hdshow.h 包含时,strsafe.h 中的 #define sprintf 语句在包含 dstring.h 之前得到处理,因此编译器会看到dstring.hAnsiString::sprintf() 方法的以下声明失败:

AnsiString& __cdecl         sprintf_instead_use_StringCchPrintfA_or_StringCbPrintfA;(const char* format, ...); // Returns *this

为了防止这种行为,您 可以 #include <dshow.h> 之后使用 #undef sprintf 语句,就像 JeffRSon 所建议的那样。然而 正确的 解决方案是在 #include <strsafe.h> 之前定义 STRSAFE_NO_DEPRECATE。您可以通过以下任一方式做到这一点:

  1. #include <dshow.h> 语句之前将 #define STRSAFE_NO_DEPRECATE 添加到代码中

  2. STRSAFE_NO_DEPRECATE 添加到项目选项中的条件列表。

MSDN 上描述了此解决方案:

About Strsafe.h

  • When you include Strsafe.h in your file, the older functions replaced by the Strsafe.h functions will be deprecated. Attempts to use these older functions will result in a compiler error telling you to use the newer functions. If you want to override this behavior, include the following statement before including Strsafe.h.

    #define STRSAFE_NO_DEPRECATE
    
  • To allow only character count functions, include the following statement before including Strsafe.h.

    #define STRSAFE_NO_CB_FUNCTIONS
    
  • To allow only byte count functions, include the following statement before including Strsafe.h.

    #define STRSAFE_NO_CCH_FUNCTIONS
    

另一种支持的解决方案是在 #include <dshow.h> 之前定义 NO_DSHOW_STRSAFE,这样它将不再包含 strsafe.h,这要归功于 dshow.h 中的代码:

#ifndef NO_DSHOW_STRSAFE
#define NO_SHLWAPI_STRFCNS
#include <strsafe.h>  
#endif