Thread: Start Program depending on selection

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Start Program depending on selection

    Hi,

    im kinda new to Win32 programming and im trying to figure out how to either run the default program for a specific file type or to run explorer that starts in the folder where the file is. but i cant seem to get it work...
    Code:
    hwndList = GetDlgItem(hWnd, LST_SELECT); 
    nItem = SendMessage(hwndList, LB_GETCURSEL, 0, 0); 
    i = SendMessage(hwndList, LB_GETITEMDATA, nItem, 0);
    
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    					
    std::string pathTo = (std::string)Learn[i].strPath;
    int Position = pathTo.find_last_of("\\");
    pathTo = pathTo.erase(Position + 1, pathTo.length());
    
    CreateProcess(NULL, lstrcat("explorer", (LPTSTR)pathTo.c_str()), NULL, NULL, true, 0, NULL, NULL, &si, &pi);
    the Learn[i].strPath is a string with a path like "C:\program files\ab".
    thats what i got so far, but it always returns an error: memory could not be read

    i would greatly appreciate it if you could help me to solve that or even show me a way how to open files using the default app for it.

    thx

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > lstrcat("explorer", (LPTSTR)pathTo.c_str())
    Both of these parameters are constant, so neither can be appended to the other.

    Do something like
    Code:
    string temp = "explorer" + pathTo;
    CreateProcess(NULL, temp.c_str(), NULL, NULL, true, 0, NULL, NULL, &si, &pi);

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    thx that works just fine

    btw is there an easy way to start the default program for a file, e.g. pdf? or do you have to look it up in the registry manually?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. start a program with space in the path
    By cindyding in forum C Programming
    Replies: 6
    Last Post: 12-28-2006, 11:50 AM
  2. Start up program
    By Breetai in forum Windows Programming
    Replies: 2
    Last Post: 01-11-2003, 01:12 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Start a program
    By FunkeeMunkee in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 07:18 PM