Thread: Api call failing when passing a string from a windows File Dialog

  1. #1
    Registered User
    Join Date
    Oct 2006

    Api call failing when passing a string from a windows File Dialog

    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;
    typedef string tstring;
            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;
    	// Display the Open dialog box. 
    	if (GetOpenFileName(&ofn)==TRUE) 
    		test = CreateFile(ofn.lpstrFile, GENERIC_READ,
    			(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.
    Last edited by Ken Fitlike; 10-09-2006 at 05:08 AM. Reason: added code tags

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Windows specific.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Boston, Massachusetts
    As maxorator said, this should be in the windows board. Also, why would you want to display the dialog box in D3D?

  4. #4
    Registered User
    Join Date
    Oct 2001
    > tstring temp (ofn.lpstrFile);
    What happens if you try printing the contents of temp is at this point? You could use MessageBox to check its contents if you can't use cout. Or you could print it to a file.

  5. #5
    Registered User
    Join Date
    Oct 2006
    I didn't see the windows programming forum I've reposted there, admins feel free to delete the repost and move this or delete this, or simply ignore it.

    Manutd, I want to allow the user to select a file on their harddrive and than display it. Using windows file dialog seems like the logical approach here.

    Swoopy I can see in the debugger that temp has the correct contents, its also using two byte or wide character encoding so it should work with a UNICODE interface. That's what makes it so confusing.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    New York
    That is wierd. Is test a valid handle? You fill out D3DXIMAGE_INFO structure same both times?

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    	test = CreateFile(ofn.lpstrFile, GENERIC_READ,
    			(HANDLE) NULL);
    This code opens the file with exclusive access. Therefore, subsequent attempts to open the file, such as in D3DXGetImageInfoFromFile, will fail. Generally, when opening a file, it is a good idea to at least allow other access attempts read access by specifying FILE_SHARE_READ for the third parameter of CreateFile.
    Quote Originally Posted by MSDN CreateFile
    [in] Sharing mode of the object. You cannot request a sharing mode that conflicts with the access mode specified in a previous open request whose handle is still open.

    If this parameter is zero and CreateFile succeeds, the object cannot be shared and cannot be opened again until the handle is closed.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Ah anonytmouse you were absolutely right. Thanks man, I've been trying to figure out this bug for about two weeks now. New lesson learned never leave in a line of code you don't absolutely understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM