Hello everyone,
I'm writing a simple program that will display a file dialog box to the user and than open that file using directX. Before I can load the image onto a surface I need to know its size so that's why I'm calling the GetImageInfo function. Unfortunately after a week of trying and reading about character encoding for windows I'm still failing to get this to work.
If I hardcode the file path into the function call using _T("") it works but if I try to use the path returned from the file dialog I get an error D3DERR_INVALIDCALL. Here's my code, oh and I'm using the UNICODE character set for my project as well.
Code:
#ifdef _UNICODE
typedef wstring tstring;
#else
typedef string tstring;
#endif
OPENFILENAME ofn; // common dialog box structure
TCHAR szFile[_MAX_PATH]; // buffer for file name
HANDLE test;
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = mainLoop.hWnd;
ofn.lpstrFile = szFile;
//
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = _T("All\0*.*\0Text\0*.TXT\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
if (GetOpenFileName(&ofn)==TRUE)
test = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
tstring temp (ofn.lpstrFile);
HRESULT out;
out = D3DXGetImageInfoFromFile(temp.c_str(), &Info);
When I run this code out is equal to D3DERR_INVALIDCALL, if I run the following :
Code:
out = D3DXGetImageInfoFromFile(_T("F:\\Pictures\\ShannonTrip2006-PrintRun\\ShannonTrip2006 326.jpg"), &Info);
This works are returns S_OK. I really can't tell what I'm doing wrong, the file dialog returns a LPWSTR which is a wide string and the directX api call takes LPCWSTR which is a const wide string so when I use the LPWSTR as the constructor for the other string it should all work. This is starting to drive me mad as I really can't see what I'm doing wrong and I feel I've got a good understanding of what I'm attempting to do. Any help is really appreciated.