Jump to content
  • Advertisement
Sign in to follow this  
Programmer101

Getting a LPCWSTR from OPENFILENAME

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

This question has probably been asked a few times before, but none of the previous posts on this topic seem to help me. I'm trying to get a file name through an open file dialog to pass into DirectShow to open a video. The DirectShow functions only seem to take a LPCWSTR for strings so I'm trying to convert and nothing that I do will work. I'm getting access violations and failed conversions left and right. I just can't get it. Can anybody tell me how I should do this, I would really appreciate it. Here is my code:
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,
				   LPSTR szCmdLine, int nCmdShow)
{
	//String
	LPWSTR file;
	
	OPENFILENAMEW ofn;
    ZeroMemory(&ofn, sizeof(ofn));

    // Fill in standard structure fields
    ofn.lStructSize       = sizeof(OPENFILENAMEW);
    ofn.lpstrCustomFilter = NULL;
    ofn.nFilterIndex      = 1;
    ofn.nMaxFile          = MAX_PATH;
    ofn.lpstrTitle        = L"Open Media File...\0";
    ofn.lpstrFileTitle    = NULL;
    ofn.lpstrDefExt       = L"*\0";
    ofn.Flags             = OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST;

    // Create the standard file open dialog and return its result
    if(!GetOpenFileNameW((LPOPENFILENAMEW)&ofn))
		return 0;

	file = ofn.lpstrFile;

	//Play the video
	PlayVideo(file, hInst);

	return 0;
}


The truth is, when it comes to these conversions, I have no idea what I'm doing and I couldn't find any samples that did this. Thanks. EDIT: I changed it a bit. Still doesn't work, no conversion error but it can't open the file. [Edited by - Programmer101 on February 5, 2008 8:27:47 PM]

Share this post


Link to post
Share on other sites
Advertisement
Your code isnt doing any conversions. What it also isnt doing is giving the Open Dialog any place to store the file name. You need to set lpstrFile to the address of an array large enough to hold the file name (an array with atleast nMaxFile elements).

Share this post


Link to post
Share on other sites
You're right and I fixed it. Here is the code for anybody who has the same issue:

TCHAR fileName[MAX_PATH];
WCHAR wFileName[MAX_PATH];

*fileName = 0;

OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));

ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 1;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFile = fileName;
ofn.lpstrTitle = TEXT("Open Media File...\0");
ofn.lpstrFileTitle = NULL;
ofn.lpstrDefExt = TEXT("*\0");
ofn.Flags = OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST;

if(!GetOpenFileName((LPOPENFILENAME)&ofn))
return 0;

MultiByteToWideChar(CP_ACP, 0, fileName, sizeof(fileName), wFileName, sizeof(WCHAR)*MAX_PATH);

LPWSTR fnString = wFileName;


Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!