Thread: Opening a video file through c++

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    Lightbulb Opening a video file through c++

    Ok so i am still obsessed with my noob video library program.I have got a nice database done where all my 300 movies are listed...i can search for them by using various parameters (search by actor,director etc)

    It suddenly struck me that it would be cool if i were to run the movies directly from my program.What I mean is this
    (After having searched for the movie "Ice Age")
    1)I now select an option given to me as a user "Do you want to run the movie ?"
    2)I select yes and windows media player pops up and starts playing the movie.

    Is this even possible by simple using c++ ?? Any pointers as to how i could get this done otherwise would be of great help.

    Disclaimer : This is no homework..just coding it as a hobby..on request i can display my code...its just a bit too long (937 lines) hence i am avoiding it now

  2. #2
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    #include <cstdlib>, using namespace std; and use "system("WindowsMediaExecutable MyVideoFilename");" Of course, you need to look up the executable name for windows media player, probably along with its path, plus you will need the full path to the video file. This will execute windows media player, with the video as an argument.
    Last edited by CrazyNorman; 09-07-2007 at 04:35 PM. Reason: In a moment of carelessness, mistakes were made...
    Programming Your Mom. http://www.dandongs.com/

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You mean <cstdlib>.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ... and std::system().

    Or use ShellExecute() (from windows.h) on the actual file, and it will open with your default video player. See the FAQ for more information.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    Unhappy

    Quote Originally Posted by dwks View Post
    ... and std::system().

    Or use ShellExecute() (from windows.h) on the actual file, and it will open with your default video player. See the FAQ for more information.
    Thanks to all for the help

    dwks..I went through the FAQs (must say they answer almost all questions..i wish i had been there before)

    an example of ShellExecute in the FAQs is as follows

    Code:
    #include <windows.h>  //You need shell32.lib for this one 
    
    int main(void)
    {
      char szPath[] = "C:\\WINDOWS\\system32\\Calc.exe";
    
      HINSTANCE hRet = ShellExecute(
            HWND_DESKTOP, //Parent window
            "open",       //Operation to perform
            szPath,       //Path to program
            NULL,         //Parameters
            NULL,         //Default directory
            SW_SHOW);     //How to open
    
      /*
      The function returns a HINSTANCE (not really useful in this case)
      So therefore, to test its result, we cast it to a LONG.
      Any value over 32 represents success!
      */
    
      if((LONG)hRet <= 32)
      {
        MessageBox(HWND_DESKTOP,"Unable to start program","",MB_OK);
        return 1;
      }
    
      return 0;
    }
    Now the code is actually very overwhelming to me...but i would love to understand what it is doing...can any one put a jest of it in a few words.

    Another issue is if i compile the above exact code i get following 2 errors (Visual Studio 2005 )
    Code:
    error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    and

    Code:
    error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [24]' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    this is slowly beginning to look beyond my knowledge...

    suppose my path is as simple as c:\\Movies\\Ice Age.wmv
    and char path[]="c:\\Movies\\Ice Age.wmv"

    then should my ShellExecute command look like this ??

    ShellExecute(HWND_Desktop,"open",path,NULL,NULL,SW _SHOW)

    Thanks all

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are using unicode text, so you need to use TEXT("Some text") to make the compiler happy.

    And yes, your example seems like it should work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    Unhappy

    Quote Originally Posted by matsp View Post
    You are using unicode text, so you need to use TEXT("Some text") to make the compiler happy.

    And yes, your example seems like it should work.

    --
    Mats
    Not sure what you mean...thanks to my noobish knowledge

    perhaps this is too much to ask for but if u have the time can you write down where i should be inserting TEXT in the ShellExecute command ?

    damn i thought this would be easy

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Anywhere you type a string out.

    So basically
    Code:
    TCHAR szPath[] = TEXT("C:\\WINDOWS\\system32\\Calc.exe")
    
    //...
    
    MessageBox(HWND_DESKTOP,TEXT("Unable to start program"),TEXT(""),MB_OK);
    Last edited by prog-bman; 09-07-2007 at 06:43 PM.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    To explain those, they basically convert to between char's and wchar_t depending on which mode you are compiling for.

  10. #10

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    Thumbs up It worked...thanks all

    OMG It worked....what a sense of achievement lol..well thanks guys..i need to make this forum my home page :P

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    Angry Short sighted me

    OK i hope this wont be considered as bumping an old topic.Since it was just made yesterday i thought it more appropriate to post my question in this rather than making a new thread.

    So the problem is this..when i am using TEXT("C:\\Movies\\Ice Age.avi") everything works fine and through ShellExecute i am successful in opening the movie
    However if i am to use a variable containing the path in the ShellExecute call then i fail

    Now this is however what I am doing..

    1)I am reading the path of a movie( stored by me in a .txt file previously) and storing it in string member Movie_Path,namely it is being stored in

    Code:
    q->Movie_instance.Movie_Path
    2)The copying of the path when read from .txt file into a char array is done with the help of a for loop ,namely
    Code:
    for(int i = 0;i <length;i++)
    {
         path[i]=q->Movie_instance.Movie_Path[i];
    }
    where length = q->Movie_instance.Movie_Path.lenght();


    The ShellExecute function is then called as follows
    Code:
    HINSTANCE hRet = ShellExecute(
    			HWND_DESKTOP, //Parent window
    			TEXT("open"),       //Operation to perform
    			TEXT("path"),       //Path to program
    			NULL,         //Parameters
    			NULL,         //Default directory
    			SW_SHOW);     //How to open
    
    			if((LONG)hRet <= 32)
    			{
    				MessageBox(HWND_DESKTOP,TEXT("Unable to start the Movie"),TEXT(""),MB_OK);
    				
    			}
    I get no compiler errors but on running the programm the MessageBox pops up saying "unable to start the movie"
    Clearly the ShellExecute function is failing.But why ??Can TEXT macros not be used for variables.But if i dont use TEXT macro then i get UNICODE errors

    I can paste the entire code on demand.If someone has the time do solve this for free beer

    Thanks again

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > path[i]
    If you have say
    TCHAR path[1000];

    Then the 3rd parameter to shellExecute() should just be
    Code:
    HINSTANCE hRet = ShellExecute(
    			HWND_DESKTOP, //Parent window
    			TEXT("open"),       //Operation to perform
    			path,             //Path to program
    			NULL,         //Parameters
    			NULL,         //Default directory
    			SW_SHOW);     //How to open
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If your entire program uses narrow strings, trying to convert them to wchar_t for the WinAPI's sake is nonsense. Just use ShellExecuteA instead of ShellExecute, and drop the Unicode string stuff.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    Unhappy

    Quote Originally Posted by CornedBee View Post
    If your entire program uses narrow strings, trying to convert them to wchar_t for the WinAPI's sake is nonsense. Just use ShellExecuteA instead of ShellExecute, and drop the Unicode string stuff.
    I am now using ShellExecuteA.
    Code:
    HINSTANCE hRet = ShellExecuteA(
    			HWND_DESKTOP, //Parent window
    			"open",       //Operation to perform
    			"path",       //Path to program
    			NULL,         //Parameters
    			NULL,         //Default directory
    			SW_SHOW);     //How to open
    
    			if((LONG)hRet <= 32)
    			{
    				MessageBox(HWND_DESKTOP,TEXT("Unable to start the Movie"),TEXT(""),MB_OK);
    				
    			}
    IN SHORT THE ABOVE DOESNT WORK BUT THE CODE BELOW WORKS

    Code:
    HINSTANCE hRet = ShellExecuteA(
    			HWND_DESKTOP, //Parent window
    			"open",       //Operation to perform
    			"C:\\Movies\\IceAge.avi",       //Path to program
    			NULL,         //Parameters
    			NULL,         //Default directory
    			SW_SHOW);     //How to open
    
    			if((LONG)hRet <= 32)
    			{
    				MessageBox(HWND_DESKTOP,TEXT("Unable to start the Movie"),TEXT(""),MB_OK);
    				
    			}
    I even printed the char array "path" through cout on the display to see if it really has got the right path in it.Then i even debugged the entire thing.All seems fine

    The problem is that ShellExecuteA is failing at opening anything through a variable.Giving it the direct path i.e "C:\\Movies\\Ice Age.avi" works fine but giving the path as a variable doesnt.I am sick and tired of getting the MessageBox "Unable to start the Movie"

    *weeps* *weeps* No compiler errors..no linker errors..but the movie wont open.*weeeeps*

    Any advice including "go commit suicide" are welcome..just anything
    Last edited by roalme00; 09-09-2007 at 03:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM