Solved

LPWSTR to CString Pointer

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

708 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

18 Experts available now in Live!

Get 1:1 Help Now