2011-06-12 14 views
1

使用DirectShow我可以運行在C++ Win32控制檯應用程序的How To Play a File代碼示例,但是當我嘗試通過的WinForms實現它,我得到這些鏈接錯誤:中的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(); 
} 
}; 
} 

如何在winform中設置編譯環境來執行DirectShow編程? 即時通訊使用Windows SDK v7.1和vC++ 2010

+0

你可以設置「顯示所有進程消息(/ VERBOSE)」鏈接器選項並檢查Strmiids.lib是否真的包含在內? – wimh 2011-06-12 10:10:04

回答

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。