How to Use the COM interface in YC ++

Source: Internet
Author: User

/*************************************** **************************************** *******************************
Here is an example of how to use the COM interface. The code is very short and the function is not simple.
In this example, you can not only use DirectShow to program video files, but also use the COM interface
I have some knowledge about the principle. This example is compiled and executed in YC ++. To compile in other environments, you need to add some declarations.
If you have any questions, I will discuss them with you in the following comments.
**************************************** **************************************** ******************************/

Void main ()
{
Void play_mpeg (wchar_t * m_name );
Play_mpeg (l "d: // MPEG // mpg. mpg"); // play the video file: D: // MPEG // mpg. mpg
}

Const guid iid_iunknown = {0 x unknown, 0 x unknown, 0x0000, {0xc0, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0x46 }};
Struct iunknown
{
Virtual int stdcall QueryInterface (const guid * riid, void ** ppvobject) = 0;
Virtual unsigned int stdcall addref () = 0;
Virtual unsigned int stdcall release () = 0;
};

// Auto ("...") {...} is the YC ++ extension syntax. This program uses three functions in the dynamic library ole32.dll,
// This syntax can replace the library file ole32.lib
Auto ("ole32.dll ")
{
Int stdcall coinitialize (void * pvreserved );
Void stdcall couninitialize ();
Int stdcall cocreateinstance (const guid * rclsid, iunknown * punkouter, unsigned int dwclscontext, const guid * riid, void ** bp );
}

Const guid clsid_filtergraph = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53,0x00,0x20, 0xaf, 0x0b, 0xa7, 0x70 }};
Const guid iid_imediacontrol = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00,0x20, 0xaf, 0x0b, 0xa7, 0x70 }};
Const guid iid_ivideowindow = {0x56a868b4, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00,0x20, 0xaf, 0x0b, 0xa7, 0x70 }};
Const guid iid_ibasicvideo = {0x56a868b5, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00,0x20, 0xaf, 0x0b, 0xa7, 0x70 }};
Const guid iid_imediaseeking = {0x36b73880, 0xc2c8, 0x11cf, {0x8b, 0x46,0x00,0x80, 0x5f, 0x6c, 0xef, 0x60 }};
Const guid time_format_frame = {0x7b785570, 0x8c82, 0x11cf, {0xbc, 0xc, 0x0, 0xaa, 0x0, 0xac, 0x74, 0xf6 }};

Struct idispatch: Public iunknown
{
Virtual int stdcall gettypeinfocount (int * pctinfo) = 0;
Virtual int stdcall gettypeinfo (INT itinfo, int lcid, void ** pptinfo) = 0;
Virtual int stdcall getidsofnames (guid * riid, wchar_t ** rgsznames, int cnames, int lcid, int * rgdispid) = 0;
Virtual int stdcall invoke (INT dispidmember, guid * riid, int lcid, short wflags, void * pdispparams, void * pvarresult, void * p1_info, int * puargerr) = 0;
};

Struct imediaseeking: Public iunknown
{
Virtual int stdcall getcapabilities (unsigned int * pcapabilities) = 0;
Virtual int stdcall checkcapabilities (unsigned int * pcapabilities) = 0;
Virtual int stdcall isformatsupported (guid * pformat) = 0;
Virtual int stdcall querypreferredformat (guid * pformat) = 0;
Virtual int stdcall gettimeformata (guid * pformat) = 0;
Virtual int stdcall isusingtimeformat (guid * pformat) = 0;
Virtual int stdcall settimeformat (const guid * pformat) = 0;
Virtual int stdcall getduration (int64 * pduration) = 0;
Virtual int stdcall getstopposition (int64 * pstop) = 0;
Virtual int stdcall getcurrentposition (int64 * pcurrent) = 0;
Virtual int stdcall converttimeformat (int64 * pTARGET, guid * ptargetformat, int64 source, guid * psourceformat) = 0;
Virtual int stdcall setpositions (int64 * pcurrent, unsigned int dwcurrentflags, int64 * pstop, unsigned int dwstopflags) = 0;
Virtual int stdcall getpositions (int64 * pcurrent, int64 * pstop) = 0;
Virtual int stdcall getavailable (int64 * pearliest, int64 * platest) = 0;
Virtual int stdcall setrate (double drate) = 0;
Virtual int stdcall getrate (double * pdrate) = 0;
Virtual int stdcall getpreroll (int64 * pllpreroll) = 0;
};

Struct imediacontrol: Public idispatch
{
Virtual int stdcall run () = 0;
Virtual int stdcall pause () = 0;
Virtual int stdcall stop () = 0;
Virtual int stdcall getstate (INT mstimeout, int * PFS) = 0;
Virtual int stdcall renderfile (wchar_t * strfilename) = 0;
Virtual int stdcall addsourcefilter (wchar_t * strfilename, struct idispatch ** ppunk) = 0;
Virtual int stdcall get_filtercollection (struct idispatch ** ppunk) = 0;
Virtual int stdcall get_regfiltercollection (struct idispatch ** ppunk) = 0;
Virtual int stdcall stopwhenready () = 0;
};

Struct ivideowindow: Public idispatch
{
Virtual int stdcall put_caption (wchar_t * strcaption) = 0;
Virtual int stdcall get_caption (wchar_t ** strcaption) = 0;
Virtual int stdcall put_windowstyle (INT windowstyle) = 0;
Virtual int stdcall get_windowstyle (int * windowstyle) = 0;
Virtual int stdcall put_windowstyleex (INT windowstyleex) = 0;
Virtual int stdcall get_windowstyleex (int * windowstyleex) = 0;
Virtual int stdcall put_autoshow (INT autoshow) = 0;
Virtual int stdcall get_autoshow (int * autoshow) = 0;
Virtual int stdcall put_windowstate (INT windowstate) = 0;
Virtual int stdcall get_windowstate (int * windowstate) = 0;
Virtual int stdcall put_backgroundpalette (INT backgroundpalette) = 0;
Virtual int stdcall get_backgroundpalette (int * pbackgroundpalette) = 0;
Virtual int stdcall put_visible (INT visible) = 0;
Virtual int stdcall get_visible (int * pvisible) = 0;
Virtual int stdcall put_left (INT left) = 0;
Virtual int stdcall get_left (int * pleft) = 0;
Virtual int stdcall put_width (INT width) = 0;
Virtual int stdcall get_width (int * pwidth) = 0;
Virtual int stdcall put_top (INT top) = 0;
Virtual int stdcall get_top (int * pTop) = 0;
Virtual int stdcall put_height (INT height) = 0;
Virtual int stdcall get_height (int * pheight) = 0;
Virtual int stdcall put_owner (INT owner) = 0;
Virtual int stdcall get_owner (int * owner) = 0;
Virtual int stdcall put_messagedrain (INT drain) = 0;
Virtual int stdcall get_messagedrain (int * drain) = 0;
Virtual int stdcall get_bordercolor (int * color) = 0;
Virtual int stdcall put_bordercolor (INT color) = 0;
Virtual int stdcall get_fullscreenmode (int * fullscreenmode) = 0;
Virtual int stdcall put_fullscreenmode (INT fullscreenmode) = 0;
Virtual int stdcall setwindowforeground (INT focus) = 0;
Virtual int stdcall receive yownermessage (INT hwnd, int umsg, int wparam, int lparam) = 0;
Virtual int stdcall setwindowposition (INT left, int top, int width, int height) = 0;
Virtual int stdcall getwindowposition (int * pleft, int * pTop, int * pwidth, int * pheight) = 0;
Virtual int stdcall getminidealimagesize (int * pwidth, int * pheight) = 0;
Virtual int stdcall getmaxidealimagesize (int * pwidth, int * pheight) = 0;
Virtual int stdcall getrestoreposition (int * pleft, int * pTop, int * pwidth, int * pheight) = 0;
Virtual int stdcall hidecursor (INT hidecursor) = 0;
Virtual int stdcall iscursorhidden (int * cursorhidden) = 0;
};

Struct ibasicvideo: Public idispatch
{
Virtual int stdcall get_avgtimeperframe (double * pavgtimeperframe) = 0;
Virtual int stdcall get_bitrate (int * pbitrate) = 0;
Virtual int stdcall get_biterrorrate (int * pbiterrorrate) = 0;
Virtual int stdcall get_videowidth (int * pvideowidth) = 0;
Virtual int stdcall get_videoheight (int * pvideoheight) = 0;
Virtual int stdcall put_sourceleft (INT sourceleft) = 0;
Virtual int stdcall get_sourceleft (int * psourceleft) = 0;
Virtual int stdcall put_sourcewidth (INT sourcewidth) = 0;
Virtual int stdcall get_sourcewidth (int * psourcewidth) = 0;
Virtual int stdcall put_sourcetop (INT sourcetop) = 0;
Virtual int stdcall get_sourcetop (int * psourcetop) = 0;
Virtual int stdcall put_sourceheight (INT sourceheight) = 0;
Virtual int stdcall get_sourceheight (int * psourceheight) = 0;
Virtual int stdcall put_destinationleft (INT destinationleft) = 0;
Virtual int stdcall get_destinationleft (int * pdestinationleft) = 0;
Virtual int stdcall put_destinationwidth (INT destinationwidth) = 0;
Virtual int stdcall get_destinationwidth (int * pdestinationwidth) = 0;
Virtual int stdcall put_destinationtop (INT destinationtop) = 0;
Virtual int stdcall get_destinationtop (int * pdestinationtop) = 0;
Virtual int stdcall put_destinationheight (INT destinationheight) = 0;
Virtual int stdcall get_destinationheight (int * pdestinationheight) = 0;
Virtual int stdcall setsourceposition (INT left, int top, int width, int height) = 0;
Virtual int stdcall getsourceposition (int * pleft, int * pTop, int * pwidth, int * pheight) = 0;
Virtual int stdcall setdefasoursourceposition () = 0;
Virtual int stdcall setdestinationposition (INT left, int top, int width, int height) = 0;
Virtual int stdcall getdestinationposition (int * pleft, int * pTop, int * pwidth, int * pheight) = 0;
Virtual int stdcall setdefadestindestinationposition () = 0;
Virtual int stdcall getvideosize (int * pwidth, int * pheight) = 0;
Virtual int stdcall getvideopaletteentries (INT startindex, int entries, int * pretrieved, int * ppalette) = 0;
Virtual int stdcall getcurrentimage (int * pbuffersize, int * pdibimage) = 0;
Virtual int stdcall isusingdefaultsource () = 0;
Virtual int stdcall isusingdefadestindestination () = 0;
};

Void play_mpeg (wchar_t * m_name)
{
Iunknown * Puk = 0;
Imediacontrol * PMC = 0;
Ivideowindow * pvw = 0;
Ibasicvideo * pbv = 0;
Imediaseeking * PMS = 0;

Coinitialize (0 );
# Define clsctx_inproc_server 1
Int hR = cocreateinstance (& clsid_filtergraph, 0, clsctx_inproc_server, & iid_iunknown, (void **) & PUK );
If (HR <0) goto p_end;

Puk-> QueryInterface (& iid_imediacontrol, (void **) & PMC );
HR = PMC-> renderfile (m_name );
If (HR <0) goto p_end;

Int vid_width, vid_height;
Puk-> QueryInterface (& iid_ibasicvideo, (void **) & pbv );
Pbv-> getvideosize (& vid_width, & vid_height );

Puk-> QueryInterface (& iid_ivideowindow, (void **) & pvw );
Pvw-> put_owner (0 );
Pvw-> put_windowstyle (ws_child );
Pvw-> setwindowposition (100,100, vid_width, vid_height );

Int64 vid_len;
Puk-> QueryInterface (& iid_imediaseeking, (void **) & PMS );
PMS-> settimeformat (& time_format_frame );
PMS-> getduration (& vid_len );
Y_ppoo ("video length (seconds) =", vid_len/25, vid_len, 0, vid_len/25., 0 );

PMC-> Run ();
Sleep (5000); // play for 5 seconds

P_end:
If (PMS) PMS-> release ();
If (pvw) pvw-> release ();
If (pbv) pbv-> release ();
If (PMC) PMC-> release ();
If (PUK) Puk-> release ();
Couninitialize ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.