Solved

LPWSTR to CString Pointer

Posted on 2007-11-22
4
1,112 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 250 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:DeepuAbrahamK
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

828 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