在Winforms中使用DirectShow

时间:2011-06-12 08:36:03

标签: winforms visual-c++ directshow

我可以在c ++ win32控制台应用程序中运行How To Play a File代码示例,但是当我尝试通过winforms实现它时,我会收到这些链接错误:

错误2错误LNK2020:未解析的令牌(0A000016)IID_IMediaEvent

错误3错误LNK2020:未解析的令牌(0A000017)IID_IMediaControl

还有一些链接错误......

这是表格的代码:

#include <dshow.h>
#pragma comment(lib, "Strmiids.lib")

namespace Form1 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->SuspendLayout();
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 262);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);

        }
#pragma endregion
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
{
 IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;

    // Initialize the COM library.
    HRESULT hr = CoInitialize(NULL);

    // Create the filter graph manager and query for interfaces.
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                        IID_IGraphBuilder, (void **)&pGraph);

    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

    // Build the graph. IMPORTANT: Change this string to a file on your system.
    hr = pGraph->RenderFile(L"C:\\Example.avi", NULL);
    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);

            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    pControl->Release();
    pEvent->Release();
    pGraph->Release();
    CoUninitialize();
}
};
}

如何在winforms中设置构建环境以进行DirectShow编程? 即时通讯使用Windows SDK v7.1和vc ++ 2010

1 个答案:

答案 0 :(得分:4)

你没有得到很好的诊断。问题是DirectShow是本机代码。但是你让编译器认为可以在托管模式下编译它。哪个效果出奇的好,直到链接器急转直下。你需要看起来像这样:

#pragma once
#pragma managed(push, off)
#include <dshow.h>
#pragma managed(pop)
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "ole32.lib")
// etc..

这可能会产生一连串的错误。在“解决方案资源管理器”窗口中,右键单击“项目”,“属性”,“配置属性”将/ clr:pure中的公共语言运行时支持更改为/ clr。当我尝试它时,它正确播放了一个示例.avi文件。在DirectShow窗口中,而不是表单。示例代码仅用于在控制台应用程序中工作。您还应该删除对CoInitialize和CoUninitialize的调用,.NET已初始化COM。建议改进错误处理。请考虑嵌入Windows Media Player。