Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C C# MFC VC++
(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:
// 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:
 

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 23-Jan-13 23:17pm
Edited 27-Jan-13 19:40pm
v7
Comments
Azziet at 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 at 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 at 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 at 24-Jan-13 8:19am
   
Is that possible to play the video like this?
Azziet at 24-Jan-13 8:26am
   
no you cant play video like this ..you can use html5 video tag to play video
 
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

[DllImport("C:\\Documents and Settings\\BTC\\CSharpTest\\RenderFileDll.dll", CharSet = CharSet.Ansi)]
public static extern void RenderFile(string strFilename);
  Permalink  
Comments
J.Surjith Kumar at 24-Jan-13 10:21am
   
I tried this. But getting the same "Error-could not initialize the com library".
Maxim Kartavenkov at 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 at 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 at 25-Jan-13 0:51am
   
Ya now it's playing.. Thank you Maxim..
J.Surjith Kumar at 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
J.Surjith Kumar at 28-Jan-13 1:28am
   
That dll having this c++ function:
#include "RenderVideoFile.h"
void RenderFile(WCHAR *strFilename,HWND hwnd)
{
.
.
.
.
}

Calling that function from c#:
[DllImport("C:\\RenderVideoFile.dll",CharSet=CharSet.Unicode)]
public static extern void RenderFile(string strFilename,IntPtr hwnd);
.
.
.
RenderFile("C:\\123.avi",pictureBox1.Handle);
Maxim Kartavenkov at 28-Jan-13 1:52am
   
You modify the code without understanding it - you should research your self before pasting here code which you just cut from somethere.
J.Surjith Kumar at 28-Jan-13 2:05am
   
Before the video was played in the separate window which is the windowed mode now am trying to play the video in the window less mode. So that i added the IVMRWindowlessControl interface to write the video in the picture box. hr = InitWindowlessVMR(hwnd, pGraph, &pWc); is used to initialize the vmr. GetClientRect(hwnd,&rcDest); rcDest is consists of the picture box rectangle coordinates. I am clear with the code but am asking whether this idea will work or not!? If not what i have to do!?
Maxim Kartavenkov at 28-Jan-13 2:21am
   
I know what your code do, and it were never plays the video before - this bcs you just copy it from somethere without understanding - and due that some code parts are at wrong location.
J.Surjith Kumar at 28-Jan-13 2:36am
   
It works when i tested in new mfc application but not in c#.
J.Surjith Kumar at 28-Jan-13 2:38am
   
Definition of InitwindowLessVMR:
 
HRESULT InitWindowlessVMR(
HWND hwndApp, // Window to hold the video.
IGraphBuilder* pGraph, // Pointer to the Filter Graph Manager.
IVMRWindowlessControl** ppWc // Receives a pointer to the VMR.
)
{
if (!pGraph || !ppWc)
{
return E_POINTER;
}
IBaseFilter* pVmr = NULL;
IVMRWindowlessControl* pWc = NULL;
// Create the VMR.
HRESULT hr = CoCreateInstance(CLSID_VideoMixingRenderer, NULL,
CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVmr);
if (FAILED(hr))
{
return hr;
}

// Add the VMR to the filter graph.
hr = pGraph->AddFilter(pVmr, L"Video Mixing Renderer");
if (FAILED(hr))
{
pVmr->Release();
return hr;
}
// Set the rendering mode.
IVMRFilterConfig* pConfig;
hr = pVmr->QueryInterface(IID_IVMRFilterConfig, (void**)&pConfig);
if (SUCCEEDED(hr))
{
hr = pConfig->SetRenderingMode(VMRMode_Windowless);
pConfig->Release();
}
if (SUCCEEDED(hr))
{
// Set the window.
hr = pVmr->QueryInterface(IID_IVMRWindowlessControl, (void**)&pWc);
if( SUCCEEDED(hr))
{
hr = pWc->SetVideoClippingWindow(hwndApp);
if (SUCCEEDED(hr))
{
*ppWc = pWc; // Return this as an AddRef'd pointer.
}
else
{
// An error occurred, so release the interface.
pWc->Release();
}
}
}
pVmr->Release();
return hr;
}
J.Surjith Kumar at 28-Jan-13 2:38am
   
May i know where it went wrong??
Maxim Kartavenkov at 28-Jan-13 3:44am
   
You are waiting till playback complete.
J.Surjith Kumar at 28-Jan-13 4:10am
   
ya i know that, i just checked whether the video was played in the picture box or not.
J.Surjith Kumar at 28-Jan-13 1:56am
   
I pasted the code from my program.
J.Surjith Kumar at 28-Jan-13 3:31am
   
Thanks for your solution it helps me to get the result.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
  Permalink  
Comments
J.Surjith Kumar at 24-Jan-13 8:35am
   
I am asking to play the video using c# by accessing the function from dll.
J.Surjith Kumar at 24-Jan-13 8:38am
   
And am doing it in windows application not in web.
jibesh at 25-Jan-13 1:09am
   
This is not the right solution. Please read OPs question clearly. my vote 1
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^]
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);
        }
    }
}
  Permalink  
Comments
J.Surjith Kumar at 28-Jan-13 3:30am
   
Maxim Kartavenkov solution helped me to resolve from this. Very thankful to him :)

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

  Print Answers RSS
0 mhegazy94 460
1 Sergey Alexandrovich Kryukov 450
2 Kornfeld Eliyahu Peter 305
3 Ravi Bhavnani 200
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 6,890
1 OriginalGriff 6,761
2 CPallini 5,350
3 George Jonsson 3,609
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100