Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to select a file and place the file path in an edit box. When i do so i get a garbled string like 穨< when the actual path is something like c:\config\333.conf

I have two other edit boxes that i am using to select a folder path that work correctly.

I have a wchar_t* holding the path that i want to write to the editbox.

C++
INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	
  switch(uMsg)
  {
  case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case IDCANCEL:
      SendMessage(hDlg, WM_CLOSE, 0, 0);
      return TRUE;
	case IDOK:
       //SetDlgItemText(hDlg, IDC_IMAGE_A_EDIT, _T("OK was pressed"));
		DoTune();
      return TRUE;
	case IDC_IMAGE_A_FOLDER_SELECT_BUTTON:
		str = OpenFolder();
        SetDlgItemText(hDlg, IDC_IMAGE_A_EDIT, str);//this works
      return TRUE;
	case IDC_IMAGE_B_FOLDER_SELECT_BUTTON:
		str = OpenFolder();
        SetDlgItemText(hDlg, IDC_IMAGE_B_EDIT, str);//so does this
      return TRUE;
	case IDC_CONF_FILE_SELECT_BUTTON:
		wchar_t* filter = L"CONF Files (*.conf)\0*.conf\0";
		str = OpenFileName(filter, NULL);
		SetDlgItemTextW(hDlg, IDC_CONF_EDIT, str); //this doesn't work
      return TRUE;
    }
    break;

  case WM_CLOSE:
    if(MessageBox(hDlg, TEXT("Close the program?"), TEXT("Close"),
      MB_ICONQUESTION | MB_YESNO) == IDYES)
    {
      DestroyWindow(hDlg);
    }
    return TRUE;

  case WM_DESTROY:
    PostQuitMessage(0);
    return TRUE;
  }

  return FALSE;
}


C++
//main.h
#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
#include "resource.h"
#include <string>
#include <commdlg.h>
#include <shlobj.h>
#include <stdio.h>
#include <strsafe.h>
#include <comdef.h>
#include <string.h>

wchar_t* str;
//file path variable
char* CONFIG_PATH_ANALYSIS;
int numFiles;

void DoTune();
void DisplayErrorBox(LPTSTR lpszFunction);
wchar_t* OpenFolder();
void RunAnalysis(wchar_t* Folder);
wchar_t* ConvertCharArrayToLPCWSTR(const char* charArray);
wchar_t* OpenFileName(wchar_t *filter, HWND owner);

typedef int (__cdecl *ANALYZE_PROC)(char *, char *, char *,char *, bool, bool);
typedef int (__cdecl *GETAVERAGE_PROC)(char *, char *);
typedef int (__cdecl *VERIFY_PROC)(char *, char *);

HINSTANCE           hFoo;
HWND                hDlg;
GETAVERAGE_PROC     getAverageProcess;
ANALYZE_PROC        analyzeProcess;
VERIFY_PROC         verifyProcess;


C++
//OpenFileName Definition
wchar_t* OpenFileName(wchar_t *filter, HWND owner) {
    OPENFILENAME ofn;
    wchar_t fileName[MAX_PATH] = L"";
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = owner;
    ofn.lpstrFilter = filter;
    ofn.lpstrFile = fileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = NULL;
    wchar_t* fileNameStr;

    if ( GetOpenFileName(&ofn) )
        fileNameStr = fileName;

    return fileNameStr;
}


as you can see it's pretty straightforward and i am not new to C++ so i figure i am missing something simple. Also, i have verified that str has the correct path before it gets passed to the SetDlgItemText function.

As always, i appreciate any input.
Posted
Updated 21-Nov-13 8:16am
v2
Comments
Richard MacCutchan 21-Nov-13 13:19pm    
What are the declarations of str and OpenFileName?
Nelek 21-Nov-13 13:23pm    
Maybe problems with the font format or Unicode and similars?
fredrick72 21-Nov-13 13:46pm    
Nelek,

It could be but then it works for the previous two edit controls. I am pretty much stumped at this point.
enhzflep 21-Nov-13 14:06pm    
Hard to tell with what you've posted. (You should delete the newest 'solution', posting that content into your original 'question' by hitting "Improve question"

Anyhow, the fact that you've written SetDlgItemTextW suggests to me that you're possibly building an Ascii version of your project, and are resorting to explicitly calling the WideString, or Unicode version of SetDlgItemText.

That's fine, it has traps and pitfalls - I suspect one of which you're seeing here.
You see, the GetOpenFileName also comes (as do pretty much all win32 functions) with an A and a W version. Since you've not specified either, you get whichever version is the default for the type of project you're building - an Ascii version I suspect.

Problem is though, you're getting an ascii text from this function before then trying to display it with a unicode function.

How would I test?
I'd set the program to debug-mode and insert calls to printf and wprintf - one of them will give you rubbish. I suspect it will be the wprintf.

str = OpenFileName(filter, NULL);
printf("%s - ascii\n", (char*)str); // add this
wprintf(L"%s - unicode\n", str); // add this
SetDlgItemTextW(hDlg, IDC_CONF_EDIT, str); //this doesn't work

i think you forget convert char to wchar so...
you know that char is 8 bit and wchar is 16 bit...
so 2, 8 bit information is put in 1 wchar variable.

i don't know where you have garbage in dialog box or in edit box in main window?

but i think mistake is here
C++
if ( GetOpenFileName(&ofn) )
        fileNameStr = fileName;


sorry for poor info :-D
 
Share this answer
 
Comments
fredrick72 25-Nov-13 7:50am    
I believe you are correct. the confusing part is that fileName is 16 bit and ofn.lptstrFile should return 16 bit when multi-byte char set setting is used. So in that example this should have worked. I even verified that the _MBCS flag was being set in the command properly.
fredrick72 25-Nov-13 8:36am    
I may have had that backwards. For clarity i looked it up and this is the answer i found.

LPTSTR returns A LPWSTR if UNICODE is defined, an LPSTR otherwise. For more information, see Windows Data Types for Strings.
This type is declared in WinNT.h as follows:
C++

#ifdef UNICODE
typedef LPWSTR LPTSTR;
#else
typedef LPSTR LPTSTR;
#endif

So this makes a bit more sense; however, implicit conversion of char to w_char should have resulted in compile error. C++ certainly makes things difficult with all of these character types and definitions.
I have a solution but i am at a loss to explain why it works. I changed the OpenFileName function from its original form to this and it works perfectly.

C++
char* OpenFileName(char *filter = "Conf Files (*.conf)\0*.conf\0", HWND owner = NULL) {  
	OPENFILENAME ofn;
	char fileName[MAX_PATH] = "";
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = owner;
	ofn.lpstrFilter = filter;
	ofn.lpstrFile = fileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = "";
	char* fileNameStr;
	
	if ( GetOpenFileName(&ofn) )
		fileNameStr = fileName;
  
	return fileNameStr;
}


So basically just changing wchar_t to char worked; however, the recommended standard is to use wchar_t over char in a windows application so this is going a bit against that. I would still like to know why this works where the other failed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900