Thread: Getting File param name

  1. #1
    CodeMonkey
    Guest

    Getting File param name

    You know how in windows you can drag a file into an exe's icon and the exe will handle the file as it can? How do you do that? I'm using a Windows console application.

  2. #2
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125
    declare main int main(int argc,char ** argv){
    argc is one if there is a filename and argv[1] is the filename argv[0] is your owns programs file name Plus post this in the c board not the windows board.
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    That's how its best done in a console app, but there are a few other options if you want to use a win32 app.....

    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance,
    				   HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine,
    				   int nCmdShow){
    
    	MessageBox(HWND_DESKTOP,lpCmdLine,"From WinMain",MB_OK);
    
    	MessageBox(HWND_DESKTOP,GetCommandLine(),"with GetCommandLine",MB_OK);
    
    
    	return 0;
    }
    First you can call on the third param passed to winmain....if you drag and drop a file as you said earlier, you will get the full pathname of the file.....fine so far...

    Another more generic method is to use GetCommandLine()....this works for consoles and win 32 apps and gives the name of the calling app as well as the command line, so to get the dropped file name, you need to parse the string (of course take a copy of the string and alter that as opposed to trying to alter the return of this function )

    The GetCommandLine version is better as it allows you to work in unicode as well as ANSI (getting the params of main or WinMain costricts you to ANSI).......

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Thank you. By the way, Canine, the reason I posted it on the Windows board is because I wanted to know how to do it in Windows.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by CodeMonkey
    Thank you. By the way, Canine, the reason I posted it on the Windows board is because I wanted to know how to do it in Windows.
    You did say you wanted to do it in a console app. So he was correct.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Not all operating systems contain the file parameter option. But let's not fight about such an insignificant suject.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by CodeMonkey
    Not all operating systems contain the file parameter option. But let's not fight about such an insignificant suject.
    im not sure what you mean:
    Code:
    int main (int foo, char **bar)
    is an ANSI specification, and except for the formatting of bar[0], is guaranteed. if your compiler doesnt support command line args, um well, get one made some time after the 1940s
    hello, internet!

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Why doesn't getting the cmd line by WinMain param 3 not seem to work on my friend's XP system? Is there a different standard for Windows programming now?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: Getting File param name

    Originally posted by CodeMonkey
    blah blah blah ...Windows console application.
    this is the reference point for everyone's statements. console apps use main and not WinMain

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CodeMonkey: Why doesn't getting the cmd line by WinMain param 3 not seem to work on my friend's XP system?
    Fordy: The GetCommandLine version is better as it allows you to work in unicode as well as ANSI (getting the params of main or WinMain costricts you to ANSI).......
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Thank you all. You may now commence to **** yourselves.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM