Link to home
Start Free TrialLog in
Avatar of Ocrana
Ocrana

asked on

LPWSTR to CString Pointer

Hi,

I have a function that will called :
BOOL BrowseForFile(CString *selName, CString defFilename)

I want to fill the selName with a OPENFILENAME value ofn.lpstrFile. How I can do this?

Ingo
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW, if you are *not* compiling as UNICODE, you'd have to convert to ANSI, i.e.

BOOL BrowseForFile(CString *selName, CString defFilename)
{
OPENFILENAME ofn;

//...

GetOpenFileName(&ofn);

char acAnsi[MAX_PATH];

wcstombs(acAnsi,ofn.lpstrFile,MAX_PATH);

*selName = acAnsi;

return bRC;
}

OPENFILENAME ofn;
GetOpenFileName(&ofn);
..

selName->Format("%s", ofn.lpstrFile);
Should also work.
Best Regards,
DeepuAbrahamK
Look at the BrowseForFile implementation at this page:
http://stuff.mit.edu/afs/sipb/project/ocaml/src/current/win32caml/menu.c

int BrowseForFile(char *fname,char *path)
{
        OPENFILENAME ofn;
        char *p,tmp[512],browsefor[512];
        int r;

        memset(tmp,0,sizeof(tmp));
        strncpy(tmp,fname,sizeof(tmp)-1);
        p = tmp;
        while (*p) {
                if (*p == '|')
                        *p = 0;
                p++;
        }
        memset(&ofn,0,sizeof(OPENFILENAME));
        ofn.lpstrFilter = tmp;
        ofn.nFilterIndex = 1;
        ofn.lStructSize = sizeof(OPENFILENAME);
        ofn.hwndOwner = hwndMain;
        ofn.hInstance = hInst;
        ofn.lpstrFilter = tmp;
        ofn.lpstrFile = path;
        wsprintf(browsefor,"Open %s",fname);
        ofn.lpstrTitle = browsefor;
        ofn.lpstrInitialDir = "c:\\";
        ofn.nMaxFile = MAX_PATH;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_LONGNAMES |
                OFN_HIDEREADONLY |OFN_EXPLORER;
        r = GetOpenFileName(&ofn);
        if (r == 0)
                return 0;
        else return 1;
}