Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LPWSTR to CString Pointer

Posted on 2007-11-22
4
Medium Priority
?
1,153 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:Ocrana
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 20336364
Usually, you'd use


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

//...

GetOpenFileName(&ofn);

*selName = ofn.lpstrFile;

return bRC;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 20336374
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;
}
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 20336386

OPENFILENAME ofn;
GetOpenFileName(&ofn);
..

selName->Format("%s", ofn.lpstrFile);
Should also work.
Best Regards,
DeepuAbrahamK
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20336397
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;
}
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question