Open with my program

This is a discussion on Open with my program within the Windows Programming forums, part of the Platform Specific Boards category; I know how to get the command line info if i run my program with an argument from the command ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    43

    Open with my program

    I know how to get the command line info if i run my program with an argument from the command line but I want to be able to double click on the file and open with my program, how do I get he filename information that way?

    Help greatly appreciated

    Vista +MSVC2005

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    argv[0] should contain some variation of a string that will contain your program's name no matter how you run it.

    There are other ways of obtaining the exe name, if this is what you're after. GetModuleName(NULL) sounds familiar, but that's a random guess from memory.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    Isnt argv[0] a dos thing? I dont have argv[0] in winmain.

    I do not want the exe name. I want to right click on an mp3 file and open it with my program

    It seems every time i post on these boards I get morons replying with unthoughtout useless responses

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    No, it's not a DOS thing. It can be used in Windows console programs, you ignorant jackass. For Winmain(), GetModuleName() or whatever it is would work. So what I thought your unintelligible question was, I answered for both ways.

    Now let's see if your pathetic feeble brain can handle this....

    If you'd like to setup an extra selection for the right-click context menu for MP3 files, you can do the following:

    1. Open My Computer.
    2. Click on Tools.
    3. Click on Folder Options.
    4. Click the File Types tab.
    5. Find the MP3 file type in the list.
    6. To alter a menu item, click on Advanced. [Note: To change a file association, click Change instead, and select the program to change it to. You can stop here if that's all you wanted. And if that is all you wanted, you're an idiot since that option is all over the place, and it's called a file association.]
    7. If you wish to add a new menu item, click Add. Select the menu command and the program to perform the command.


    Now the next time someone tries to help and doesn't understand your question, try to come off less like a loser in calling them names because you lack the ability to ask your question properly.

    http://catb.org/~esr/faqs/smart-questions.html

    Or you could just STW like I did and not waste my time in the first place.
    Last edited by MacGyver; 08-14-2007 at 12:13 AM.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    There is no argv[0] in my application and lpCmdLine only contains the string when i run it from the console with an argument.

    I do not want to change file associations and I do not need the path to the application. I want the path to the mp3 file i double clicked on after I did the file associations. Are you retarded? Is it fun being retarded?

    Someone please help I have been googling this for a while with no luck.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by elmutt View Post
    Is it fun being retarded?
    You'd have to tell me. I don't know your experiences first hand.

    Edit:

    Code:
    #include <stdio.h>
    #include <Windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	printf("lpCmdLine = &#37;s\n", lpCmdLine);
    	getchar();
    	return 0;
    }
    I opened a file via My Computer with it, and it displayed the full path and filename in question.

    So, how about you post some code and explain exactly what you're doing to break your program so we can see how wise you really are?
    Last edited by MacGyver; 08-14-2007 at 12:39 AM.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    Code:
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    	MessageBoxA(NULL, lpCmdLine, "cows", MB_OK);
    
    return 0;
    }
    doesnt work for me, crashes

    Maybe your way only works if its a windows console application type?

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    This is really weird, actually that code doesn't crash and works perfectly but for some reason the exact same message box crashes in my big program with all my other code nearby.


    Thank you for the help and sorry about the comments, can probably figure it out from here.

    Another one of those bizarre errors that happen so much.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    MacGyver point was only that in a console application, it's in argv[0], but in a Win32 subsystem application it's in the lpCmdLine parameter.

    And if the lpCmdLine isn't good for you then you can use the GetCommandLine() function, which actually returns exactly the same thing.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    Cool, thanks again I got it working.

    I had some stuff later on in my code like:

    whatever.loadsomething("whatever.bmp");

    Its weird like the default path changes.

    When I right click open with It will only work if I use the full path like:


    whatever.loadsomething("c:\\testprogram\\whatever. bmp");

    but if i do it form the console it doesnt seem to matter.

    Whatever i dont wanna mess with it anymore.

    thanks for the help my Ultra slick media player is coming along nicely

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Why not dump the cwd (ie. with MessageBox() ) at the beginning of your program and then again before you call whatever.loadsomething() and see if it's changing throughout your program?

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    good idea, tomorrow. sleep time

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    When it is opened with a double-click on an associated file, then the current directory is the directory that file was in. I made something like this once:
    Code:
    char* GetPath(char* buffer){
        for(int i=strlen(buffer)-1;i>=0;i--){
            if(buffer[i]=='\\'){
                buffer[i+1]=0;
                break;
            }
        }
        return buffer;
    }
    
    char* GetCorrectPath(char* buffer,int size){
        buffer[0]=0;
        if(GetModuleFileName(GetModuleHandle(0),buffer,size)){
            GetPath(buffer);
        }
        return buffer;
    }
    Or something easier:
    Code:
    void SwitchToHomeDir(){
        char* homedir=new char[2048];
        if(GetModuleFileName(GetModuleHandle(0),homedir,2048)){
            for(int i=strlen(homedir)-1;i>=0;i--){
                if(homedir[i]=='\\'){
                    homedir[i+1]='\0';
                    break;
                }
            }
            SetCurrentDirectory(homedir);
        }
        delete[] homedir;
    }
    Last edited by maxorator; 08-14-2007 at 03:30 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to open IE
    By x2012 in forum C# Programming
    Replies: 2
    Last Post: 11-03-2006, 09:07 PM
  2. File Types Automatically Open With My Program
    By ElWhapo in forum Windows Programming
    Replies: 3
    Last Post: 12-29-2004, 05:39 PM
  3. how to make certain file types open IN your program
    By DarkViper in forum Windows Programming
    Replies: 4
    Last Post: 02-06-2003, 11:37 PM
  4. drawing over the open program.
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-23-2002, 03:37 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21