Click here to Skip to main content
16,009,114 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
(The Question was updated)
Am trying to play the video in c#.
I have the dll which is containing the function to play the video. The only thing i need is that the video has to play in the picture box when i call the Renderfile function by giving the video path name in c#.

Here is the c++ code which was created the dll:
C++
// RenderVideoFile.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "RenderVideoFile.h"

void RenderFile(WCHAR *strFilename,HWND hwnd)
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;
    IVMRWindowlessControl *pWc=NULL;
    // Initialize the COM library.
    HRESULT hr=S_OK;// = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return;
    }

    // Create the filter graph manager and query for interfaces.
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                        IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        printf("ERROR - Could not create the Filter Graph Manager.");
        return;
    }

    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
    hr = InitWindowlessVMR(hwnd, pGraph, &pWc);
    // Build the graph. IMPORTANT: Change this string to a file on your system.
    hr = pGraph->RenderFile(strFilename, NULL);
	long lWidth, lHeight; 
	hr = pWc->GetNativeVideoSize(&lWidth, &lHeight, NULL, NULL); 
	if (SUCCEEDED(hr))
	{
		RECT rcSrc, rcDest; 
		// Set the source rectangle.
		SetRect(&rcSrc, 0, 0, lWidth, lHeight); 
	    
		// Get the window client area.
		GetClientRect(hwnd,&rcDest); 
		// Set the destination rectangle.
		SetRect(&rcDest, 0, 0, rcDest.right, rcDest.bottom); 
	    
		// Set the video position.
		hr = pWc->SetVideoPosition(&rcSrc, &rcDest); 
	}

    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.
        }
    }
    pWc->Release();
    pControl->Release();
    pEvent->Release();
    pGraph->Release();
    //CoUninitialize();
}


This is the C# code:
C++
namespace CSharpTest
{
   public partial class Form1 : Form
    {
        [DllImport("C:\\RenderVideoFile.dll",CharSet=CharSet.Unicode)]
        public static extern void RenderFile(string strFilename,IntPtr hwnd);
        RenderFile("C:\\123.avi",pictureBox1.Handle);
 
    }
} 

I imported the function of renderfile in c# from the dll. It was executed without any error but the video was not played. What i have to correct!?
Posted
Updated 27-Jan-13 19:40pm
v7
Comments
Azziet 24-Jan-13 6:52am    
there is an error
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

please check this..in RenderFile() function
J.Surjith Kumar 24-Jan-13 6:59am    
I executed this program it shows that "Error-could not initialize the com library". I dont know why the CoInitialize() was failed.
J.Surjith Kumar 24-Jan-13 8:29am    
Put the dll inside the c# file and give that location in the dllimport and execute it.
J.Surjith Kumar 24-Jan-13 8:19am    
Is that possible to play the video like this?
Azziet 24-Jan-13 8:26am    
no you cant play video like this ..you can use html5 video tag to play video


Rather than expoting and importing the functions from the dll using the marshal concept it works as i expected:(The video was played in the picture box using the below code)
This link will be useful to know how it works:
How to Marshal a C++ Class[^]
C++
namespace CSharpManaged
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CSUnmanagedTestClass testClass = new CSUnmanagedTestClass();
            testClass.RenderFile("C:\\123.avi", pictureBox1.Handle);
        }
    }
}
 
Share this answer
 
Comments
J.Surjith Kumar 28-Jan-13 3:30am    
Maxim Kartavenkov solution helped me to resolve from this. Very thankful to him :)
C#
[DllImport("C:\\Documents and Settings\\BTC\\CSharpTest\\RenderFileDll.dll", CharSet = CharSet.Ansi)]
public static extern void RenderFile(string strFilename);
 
Share this answer
 
Comments
J.Surjith Kumar 24-Jan-13 10:21am    
I tried this. But getting the same "Error-could not initialize the com library".
Maxim Kartavenkov 24-Jan-13 10:35am    
1. remove "HRESULT hr = CoInitialize(NULL);"
2. remove "CoUninitialize();"
3. trash - "hr = pGraph->RenderFile((LPCWSTR)strFilename, NULL);" - if you use ANSI then use Conversion, otherwise use UNICODE.
Maxim Kartavenkov 24-Jan-13 10:37am    
// C#
[DllImport("Dll.dll", CharSet=CharSet.Unicode)]
public static extern void RenderFile(string strFilename);
// C++
extern "C" __declspec(dllexport) void RenderFile(WCHAR *strFilename)
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
HRESULT hr = S_OK;

// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return;
}

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(strFilename, 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();
}
J.Surjith Kumar 25-Jan-13 0:51am    
Ya now it's playing.. Thank you Maxim..
J.Surjith Kumar 28-Jan-13 1:26am    
But the video was not played in the picture box i have passed the picture box handle as the parameter but it doesn't work. What i have to do
XML
no you cant play video like this ..you can use html5 video tag to play video

<video width="320" height="240" controls="">
  <source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
  <source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>
 
Share this answer
 
Comments
J.Surjith Kumar 24-Jan-13 8:35am    
I am asking to play the video using c# by accessing the function from dll.
J.Surjith Kumar 24-Jan-13 8:38am    
And am doing it in windows application not in web.
Jibesh 25-Jan-13 1:09am    
This is not the right solution. Please read OPs question clearly. my vote 1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900