# Problem with Char encoding when calling D3DXGetImageInfoFromFile()

This topic is 4121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

#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)
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 :
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.

##### Share on other sites
Have you tried running with the debug runtimes? They may well elaborate further on why it doesn't like that parameter; ISTR if it fails on a filename then the debug runtimes will output the received filename to the output - thus you can check that what D3DX receives is what you think you're sending it. At least it should allow you to determine which part is broken...

hth
Jack

##### Share on other sites
Well, what is the value of temp before passing it to the function?

##### Share on other sites
The value of temp is:
"F:\Pictures\ShannonTrip2006-PrintRun\ShannonTrip2006 326.jpg"

and the type is:
std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >

the Hex value for each char looks as follows:
[0] 0x0046 L'F' wchar_t

I'm going to download the directX run times and see if I can find out anything more.

##### Share on other sites
Okay I am running with the debug version of the directX runtimes but its not outputting anything to console when it goes through that function, I'm just getting this:
First-chance exception at 0x4fe13f3d in DirectX1.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x4fe13f3d in DirectX1.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x4fe13f3d in DirectX1.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x4fe13f3d in DirectX1.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x4fe13f3d in DirectX1.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x4fe13f3d in DirectX1.exe: 0xC0000005: Access violation reading location 0x00000000.

When I try to update the backbuffer with the new surface that contains the image.

##### Share on other sites
You are killing memory someplace. 0x0000000... is protected memory. You're overwritting something. Start debugging your new/delete code, you likely have a major bug unrelated to the issue you think you have.

##### Share on other sites
I don't think I am "killing memory" somewhere. I only get that error when I try to pass in the string from file dialog, if I hardcode it using _T("") it works fine which makes me think there's nothing else wrong with my code. I'm probably wrong though.

##### Share on other sites
I wonder if it is due to the fact that WCHAR is not wchar_t, it is unsigned short. They should act the same. Try replacing

typedef wstring tstring;

with

typedef basic_string<WCHAR> tstring;

or just defining

typedef basic_string<TCHAR> tstring;

##### Share on other sites
Thanks for the reply Colin, I've updated the section of code with your suggestion and unfortunately its still failing.

	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);	//fileName = _T("F:\\Pictures\\ShannonTrip2006-PrintRun\\ShannonTrip2006 326.jpg");		<b>basic_string<WCHAR></b> tryout(ofn.lpstrFile);	<b>basic_string<WCHAR></b> testy (_T("F:\\Pictures\\ShannonTrip2006-PrintRun\\ShannonTrip2006 326.jpg"));	if (FAILED (D3DXGetImageInfoFromFile(tryout.c_str(), &Info))){		OutputDebugString(_T("failed to load image info from dialog\n"));	} else if(FAILED (D3DXGetImageInfoFromFile(tryout.c_str(), &Info))){		OutputDebugString(_T("failed to load image info from literal\n"));	}	if(testy.compare(tryout)){		OutputDebugString(_T("they're not the same\n"));	}else{		OutputDebugString(_T("they're the same\n"));

I've updated the code to try the string from the file dialog and a literal defined one both in the same function. It fails in the file dialog version but passes the literal and when compared returns that they're the same. (I select the same file in the dialog as the one hardcoded).

Here's the console output when I run the above code.

failed to load image info from dialog
they're the same

Both strings are the same type and they have the same contents and the data looks the same in the debugger:

[0] 0x0046 L'F' wchar_t
[0] 0x0046 L'F' wchar_t

This is starting to feel like a personal cursade why isn't this code working!

##### Share on other sites
I think you are going to have to break the problem down further to find out where the bug is. Firstly where and how is Info defined? Next what does the test result of CreateFile return? None of these are likely to be the problem just probing :) You could also try with a filename that is not as long and has no spaces in it. Keep searching - you will get it in the end :)

##### Share on other sites
Got a solution at last! Unfortunately I didn't spot it myself but someone on another board did. It was the line where I was attempting to open the file to see if I had got a valid file was causing the problems.

 test = CreateFile(ofn.lpstrFile, GENERIC_READ,			0, (LPSECURITY_ATTRIBUTES) NULL,			OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,			(HANDLE) NULL);

The 3rd parameter 0 means exclusive access so it wasn't allowing me to open it. Why it was working with a literal I still don't know but its sorted now so I can finally move on. :)