Solved

LPWSTR to CString Pointer

Posted on 2007-11-22
4
1,100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now