c++ - 包括 DShow.h 会破坏 BDS2006 上的 VCL AnsiString::sprintf()

标签 c++ c++builder directshow ansistring

我终于有时间升级我的视频捕捉类(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 Form 应用程序,上面有一个 TTimerTTimer 递增计数器 i 并将其输出到表单的 Caption 中。 DirectX 库甚至没有链接,只包含 header !

链接器输出错误:

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

如果我交换 vcl.h 和 dshow.hincludes,编译器将在dstring.h` 中停止在这一行:

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

出现此错误消息:

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

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

有没有人有任何线索?

Q1。如何解决这个问题?

Q2。到底是什么原因/在哪里造成的?

我能想到并且应该可行的唯一解决方案(但我想尽可能避免它)是创建一个OBJ(或DLL)使用 DirectShow 东西,然后将其链接到标准的 VCL 项目中,而不包含 dshow.h,当然导出必须是也没有任何有趣的东西。

最佳答案

问题不在于 dshow.h本身,但实际上与 strsafe.h相反,哪个 dshow.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 有相似的#pragma#define许多其他已弃用的“不安全”C 函数的语句。

如果两者都是STRSAFE_NO_DEPRECATEDEPRECATE_SUPPORTED未定义(在这种情况下就是这种情况),使用 #define sprintf导致所有对sprintf任何类型 的后续引用符号 被视为sprintf_instead_use_StringCchPrintfA_or_StringCbPrintfA;在编译期间。

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

当你交换 vcl.h 时和 dshow.h包括 #define sprintf strsafe.h 中的声明在 dstring.h 之前得到处理包含在内,因此编译器会看到以下关于 AnsiString::sprintf() 的声明dstring.h 中的方法失败了:

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

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

  1. 添加 #define STRSAFE_NO_DEPRECATE#include <dshow.h> 之前的代码声明

  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
    

另一个支持的解决方案是定义 NO_DSHOW_STRSAFE之前#include <dshow.h>这样它就不会包括strsafe.h多亏了 dshow.h 中的这段代码:

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

关于c++ - 包括 DShow.h 会破坏 BDS2006 上的 VCL AnsiString::sprintf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40000171/

相关文章:

c++ - 自动生成谷歌模拟方法?

c++ - OpenCV:如何复制 NormalBayesClassifier?

c++ - 打印二叉树 - C++

c++ - Eclipse CDT : Unresolved inclusion of STL header

c# - 将 DirectShow 视频窗口附加到 WPF 控件

c# - DirectShowLib - 不能使用网络摄像头拍摄多于一张照片。出现 "Insufficient system resources exist to complete the requested service"错误

c++ - LLVM-如何通过函数的真实/原始名称获取函数

c++ - 使用 C++ builder 读取 gmail pop3

c++ - 如何在 borland C++ builder 6 中制作 .exe 文件

c++ - InfTee 过滤器不适用于 VMR