Click to See Complete Forum and Search --> : Winapi C++ retrieve variable


zaryk
September 27th, 2008, 07:54 AM
Im writing another function (save function) that involves retrieving the filename - variable "szFileName" from this function...but I dont exactly know how to do the retrieving part....I figured it would involve pointers but i am not too good with pointers


BOOL Load(HWND hwnd, BOOL bSave)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH];

ZeroMemory(&ofn, sizeof(ofn));
szFileName[0] = 0;

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0HTML Files (*.html)\0*.html\0HTM Files (*.htm)\0*.htm\0PHP Files (*.php)\0*.php\0UnkEdit Files (*.uke)\0*.uke\0All Files (*.*)\0*.*\0\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";

ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(GetOpenFileName(&ofn))
{
if(!LoadFile(GetDlgItem(hwnd, EDIT_TEXT), szFileName))
{
MessageBox(hwnd, "Load of file failed.", "Error",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
return TRUE;
}

egawtry
September 27th, 2008, 11:51 AM
If you just want to return a string pointer, use this template:


LPCTSTR MyFunc(int x, int y)
{
static TCHAR szReturn[MAX_PATH];

// Fill in szReturn here

return szReturn;
}


If you really need to return a BOOL:


BOOL MyFunc(int x, int y, LPTSTR lpString, UINT cbString)
{
// Do your thing here
_tcsncpy_s(lpString, cbString, szFilename, sizeof(szFilename)/sizeof(szFilename[0]));

return FALSE;
}


-Erik