views:

71

answers:

2

Hi,

I have implemented a file selector with a combobox. I want to write the selected filename to a log. The problem is that when I select a file from the original directory it goes well but when I choose a file from another directory it won't work. Can anybody help with this? Here is the code for the file selector, it is inside a dialog.

BOOL CALLBACK BateriaFaxDlg(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam){

char descripcion[100]="";

char archivo[100]="";   
char cad[100];

int i,l;

switch (msg)               

    {
        case WM_INITDIALOG:
      InitCombo(hDlg, "*.*");   
      return TRUE;
      break;
        case WM_COMMAND:
      switch(LOWORD(wParam)) {
       case IDOK: 

        i = SendDlgItemMessage(hDlg, IDC_ARCH2, CB_GETCURSEL, 0, 0);
        if(i >= 0) {
         SendDlgItemMessage(hDlg, IDC_ARCH2, CB_GETLBTEXT, (WPARAM)i, (LPARAM)archivo);
        }

        if (!GetDlgItemText(hDlg, IDC_DESCBATER, descripcion , 100)) {
         MessageBox(hDlg, "Ambos campos son obligatorios", "ERROR", MB_ICONEXCLAMATION | MB_OK); 
         break;
        }
        actualizarBaterias(GetParent(hDlg), "FAX", archivo, descripcion);
        EndDialog(hDlg, FALSE);
       break;
       case IDCANCEL:
        EndDialog(hDlg, FALSE);
        break;
       case IDC_ARCH2:
        switch(HIWORD(wParam)) {
         case CBN_CLOSEUP:
         case CBN_KILLFOCUS:
          if(DlgDirSelectComboBoxEx(hDlg, cad, 100, IDC_ARCH2)) {
           strcat(cad, "*.*");
           InitCombo(hDlg, cad);
          }      
         break;
        }
       break;   
       default:
       break;           
           return TRUE;
        }
    }


    return FALSE;
}

This is InitCombo:

void IniciarCombo(HWND hwnd, char* p) {

   char path[100];

   strcpy(path, p);

   DlgDirListComboBox(
    hwnd,           
    path,          
    IDC_ARCH2,      
    ID_TITULO,      
    DDL_DIRECTORY | DDL_DRIVES
   );
   SendDlgItemMessage(hwnd, IDC_ARCH2, CB_SETCURSEL, 0, 1);
}

and finally this is where i write the filename to a file.

void actualizarBaterias(HWND hWnd, char *tipo, char *archivo, char *descripcion) {
    FILE *fp;
    HWND hctrl;
    int i;
    HFONT hfont;

    fp = fopen("conf\\Baterias.conf", "a" );
    if (fp) {
     MessageBox(hWnd, "Actuali","error", MB_ICONEXCLAMATION | MB_OK);                                     
     fprintf(fp, "\n%s %s %s", tipo, archivo, descripcion);
     fclose(fp);
    }
}

Thanks in advance.

A: 

Is the problem that the file isn't opening? If so, I'd suspect that the current directory is being changed by the file selection dialog box, and so the new directory doesn't contain one called "conf".

You could try specifying an absolute path for conf\Baterias.conf to confirm that this is the problem.

Damyan
A: 

From the documentation for DlgDirListComboBox:

If lpPathSpec specifies a directory, DlgDirListComboBox changes the current directory to the specified directory before filling the combo box. The text of the static control identified by the nIDStaticPath parameter is set to the name of the new current directory.

You probably want to cache the current directory (GetCurrentDirectory) before calling DlgDirSelectComboBoxEx, then set it back after it returns. Or, don't call fopen with a relative directory.

MSN