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

This is a discussion on Api call failing when passing a string from a windows File Dialog within the Windows Programming forums, part of the Platform Specific Boards category; Hello everyone, I'm writing a simple program that will display a file dialog box to the user and than open ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    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.
    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.
    Last edited by Ken Fitlike; 10-09-2006 at 06:08 AM. Reason: added code tags

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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
    Location
    Boston, Massachusetts
    Posts
    870
    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
    Posts
    2,934
    > 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
    Posts
    4
    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
    Location
    New York
    Posts
    1,465
    That is wierd. Is test a valid handle? You fill out D3DXIMAGE_INFO structure same both times? http://msdn.microsoft.com/library/de...IMAGE_INFO.asp

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	test = CreateFile(ofn.lpstrFile, GENERIC_READ,
    			0, (LPSECURITY_ATTRIBUTES) NULL,
    			OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
    			(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
    Posts
    4
    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, 05:27 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02: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, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21