Thread: Set default directory with GetTempDir?

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Question Set default directory with GetTempDir?

    When my 'Options' dialog loads up the first time the control for the 'default save path' is empty.
    I've tried to use the GetTempDirectory API to fill it upon initialization but I must be doing something wrong.
    After selecting the 'browse' button and seleting the default path once everything works as I expected.

    Here is my code
    Code:
    BOOL CALLBACK OptionsProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	CoInitialize(0);
    
    	static char szDirectory[MAX_PATH];
    	HRESULT hr;
    	BROWSEINFO browseInfo;
    	LPITEMIDLIST lpItemList=0;
    
    	LPMALLOC lpM;
    	hr = SHGetMalloc (&lpM) ;
    	if (FAILED(hr) ) return 0;
    
    	ZeroMemory ( (PVOID) &browseInfo,sizeof (BROWSEINFO));
      
      	browseInfo.pidlRoot = 0;
    	browseInfo.hwndOwner = NULL;
    	browseInfo.pszDisplayName = szDirectory;
    	browseInfo.lpszTitle = "Open Folder ";
    	browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
    	browseInfo.lParam = 0;
    
    
    	switch (Message)
    	{
    	case WM_INITDIALOG:
    		//  Try to set text to default path... Not working!
    		if(pTempDirectoryPath[0] != NULL)// if already set just display it.
    		{
    			hdlg = GetDlgItem(hwnd,IDC_OUTPUT_FILES_LOCATIONS);
    			SetWindowText (hdlg, pTempDirectoryPath[0]);
    		}
    		else //(pTempDirectoryPath[0]) *** 
    /***************shouldn't this set it?*************/
    		{
    			char TempDirectoryPath[MAX_PATH+1];
    			GetTempPath(MAX_PATH+1, TempDirectoryPath); 
    			//pTempDirectoryPath[0] = &TempDirectoryPath[0];
    			//SetWindowText (hdlg, pTempDirectoryPath[0]);
    			SetWindowText (hdlg, TempDirectoryPath);
    		}
    
    		if(bSaveComposite)	// If radio button is set,
    		{
    			hdlg = GetDlgItem(hwnd,IDC_OPTIONS_SAVE_COMPOSITE );
    			SendMessage(hdlg, BM_SETCHECK, 1,0);
    		}
    		else	// set other button.
    		{
    			hdlg = GetDlgItem(hwnd,IDC_OPTIONS_SAVE_ALL );
    			SendMessage(hdlg, BM_SETCHECK, 1,0);
    			bSaveComposite = FALSE;
    		}
    		return TRUE;
    	case WM_COMMAND:
    switch (LOWORD(wParam))
    		{
    		case IDOK:
    			// checks to see if radio save_all is set, if so change BOOL.
    			if(SendMessage(GetDlgItem(hwnd,IDC_OPTIONS_SAVE_ALL ), BM_GETCHECK, 
    				(WPARAM)0 ,(LPARAM)0) == BST_CHECKED)
    				bSaveComposite = FALSE;
    			EndDialog(hwnd, IDCANCEL);
    			break;
    		case IDBROWSE:
    			if ((lpItemList = SHBrowseForFolder(&browseInfo)) == NULL)
    			{
    				return 0;
    			}
    			SHGetPathFromIDList(lpItemList,szDirectory); 
    			
    			hdlg = GetDlgItem(hwnd,IDC_OUTPUT_FILES_LOCATIONS);
    			pTempDirectoryPath[0] = szDirectory;	
    			SetWindowText (hdlg, pTempDirectoryPath[0]);
    			lpM->Free(lpItemList);
    			lpM->Release();
    			break;
    		case IDCANCEL:
    			MessageBeep (0);
    			EndDialog(hwnd, IDCANCEL);
    			break;
    		}
    		default:
                return FALSE;
    	}
    	return TRUE;
    }
    All the other cases in my code seems to work I just posted them for clarity.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Talking I finally fixed it!

    I just thought I'd post my code so when someone else searches for this they can see a solution too.

    I hate it when I do a search find the same question but no answer!

    Code:
    	case WM_INITDIALOG:
    		static char TempDirectoryPath[MAX_PATH+1];
    		// if first time thru initialize with temp dir.
    		if(pTempDirectoryPath[0] == NULL)
    		{
    			GetTempPath(MAX_PATH+1, TempDirectoryPath); 
    			pTempDirectoryPath[0] = &TempDirectoryPath[0];
    
    			// Convert to long path name.
    			GetLongPathName(
    				TempDirectoryPath,
    				pTempDirectoryPath[0],
    				MAX_PATH+1);
    		}
    		// if not first time thru keep users selection.
    		hdlg = GetDlgItem(hwnd,IDC_OUTPUT_FILES_LOCATIONS);
    		SetWindowText (hdlg, pTempDirectoryPath[0]);
    		if(bSaveComposite)	// If radio button is set,
    		{
    			hdlg = GetDlgItem(hwnd,IDC_OPTIONS_SAVE_COMPOSITE );
    			SendMessage(hdlg, BM_SETCHECK, 1,0);
    		}
    		else	// else set other button.
    		{
    			hdlg = GetDlgItem(hwnd,IDC_OPTIONS_SAVE_ALL );
    			SendMessage(hdlg, BM_SETCHECK, 1,0);
    			bSaveComposite = FALSE;
    		}
    		return TRUE;
    
    
    
    
    	case IDBROWSE:
    		if ((lpItemList = SHBrowseForFolder(&browseInfo)) == NULL)
    		{
    			return 0;
    		}
    		SHGetPathFromIDList(lpItemList,szDirectory); 
    		hdlg = GetDlgItem(hwnd,IDC_OUTPUT_FILES_LOCATIONS);
    		pTempDirectoryPath[0] = szDirectory;	
    		SetWindowText (hdlg, pTempDirectoryPath[0]);
    		lpM->Free(lpItemList);
    		lpM->Release();
    		break;
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    The major difference I see between the two code listings is that the TempDirectoryPath string is effectively allocated on the heap in the working sample (declared static), while in the failed code sample, the control is told about the text buffer although it is deallocated once the code steps out of OptionsProc(). Could this transient vs. more permanent text buffer be the cause?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. set system's default audio device
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 11-26-2007, 07:23 AM
  2. Find all possible subset of a given set
    By Downnin in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 02:03 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. Default Directory
    By ski6ski in forum Windows Programming
    Replies: 0
    Last Post: 09-28-2001, 08:07 AM
  5. Replies: 5
    Last Post: 09-03-2001, 09:45 PM