Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help.....recieve path

Status
Not open for further replies.

lasteem

Programmer
Dec 20, 2004
2
US
I'm trying to get a program to open a file when you doubleclick on the file. Like when you doubleclick a .pdf file, Adobe Reader starts and then opens the file. Thanks in advance for any help.
 
Use this:

HINSTANCE hInstance = ::ShellExecute(
hwnd,
NULL,
"C:\\MYDIR\MYFILE.PDF",
NULL,
NULL,
SW_SHOW
);

if (hInstance <= (HINSTANCE) 32)
{
LPVOID lpMsgBuf;
::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

// Display the string.
::AfxMessageBox((LPCTSTR)lpMsgBuf, MB_OK | MB_ICONINFORMATION);

// Free the buffer.
::LocalFree( lpMsgBuf );
}

William GS.
 
William,

First, thanks for taking the time to reply. In your code it appears that it would open 1 specific file "MYFILE.pdf". I want to take a random file and double click on it and have my program open. Then begin to manipulate this file. Associating the extention(.whatever) with the application in Windows should open the file but I'm not sure how to transfer that path(file name) from the explorer shell to my program. That may be what your code is doing and I'm just not educated enough to follow it(highly likely). If so, could you break it down for someone that's a little SLOOOOW. ;) Thanks!!!
 
Well you can create an association between a file type and a program using one of these two methods.

1. 2. From within explorer, hold down the shift key and right-click on the file in question. Then choose the "Open with..." option and use the dialog to locate the program you want to open that file type.

No doubt there is a win32 API interface to this as well, but that's more reading.

As for getting the filename inside your program, you have two choices.

If your program begins
Code:
int main ( int argc, char *argv[] ) {
    if ( argc > 1 ) {
        // argv[1] is the name of the file you double-clicked
    }
    return 0;
}

If your program begins with WinMain, then you get the command line using the GetCommandLine function.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top