The messagebox and windowtext of the handle both come up blank, i am assuming that they are displaying NULL. I don't understand what i did wrong as MSDN states:
"If the user specifies a file name and clicks the OK button, the return value is nonzero. The buffer pointed to by the lpstrFile member of the OPENFILENAME structure contains the full path and file name specified by the user."
If i replaced the sitesfilename.lpstrFile with L"anytext" the anytext is displayed. UNICODE is the character set.
SetWindowText(hEditOpenSiteFile,GetOpenFileNameW(&sitesfilesname.lpstrFile));
and
SetWindowText(hEditOpenSiteFile,GetOpenFileNameW(sitesfilesname.lpstrFile));
wont compile, incompatible error. Any other ideas?
THankl you gfor looking but i dont understand. I read the link but the only thing i see is .lpstrFile[0] = '\0' and as soon as i add the array brackets, [], to sitesfilesname.lpstrFile = '\0'; i get: Access violation writing location 0x00000000.
AM i getting this error becouse i am trying to write to a null locations, what should i do to fix this?
You need to actually read that article. 'lpstrFile' is a pointer, you need to allocate a buffer for it to fill. In the link I posted this is done on Line 11. 'szFile' is an array allocated at Line 3.
// This will return a string with the selected file path
// or will return an empty string if the user hit cancel.
//
// 'owner' should be the HWND of the window you want to own
// this dialog, or NULL if you don't care/don't want an owner.
std::wstring getOpenFileName(HWND owner)
{
wchar_t buffer[MAX_PATH] = "";
OPENFILENAMEW ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = owner;
ofn.lpstrFilter = L"Text Files\0*.txt\0All Files\0*\0\0";
ofn.nFilterInex = 1;
ofn.lpstrFile = buffer;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
if( !GetOpenFileNameW( &ofn ) )
return L"";
return buffer;
}
I have read it and add:
CHAR szFile[260]; // buffer for file name
and
sitesfilesname.lpstrFile = szFile;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
sitesfilesname.lpstrFile[0] = '\0';
and now i get cvalue of CHAR* can not be assigned to an entry of type LPWSTR.
I tried changing szfile to a lpwstr, did not work
tried seting sitesfielname.lpstrFile to both *szFile and &szFile and those did not work either.
I think i understand about setting the pointer to a value but why wont the MSDN code work?
I got it to work, i had to change the char to a WCHAR, these UNICODE semi compatible types is annoying.
The problem is you are using TCHARs. And TCHARs are confusing and stupid.
Note: WCHAR is still wrong, since you're probably using the TCHAR version and not the WCHAR version.
If you are using char, use:
OPENFILENAMEA, GetOpenFileNameA, SetWindowTextA
If you are using wchar_t (or WCHAR), use:
OPENFILENAMEW, GetOpenFileNameW, SetWindowTextW
If you are using TCHARs, use:
OPENFILENAME, GetOpenFileName, SetWindowText
Note again that TCHARs are stupid and confusing, and using them properly is extremely difficult. Save yourself a headache and just use with the A/W version of any/all WinAPI functions. Messing with TCHARs is a complete waste of time.