Solved

FindFirstFile error C2664

Posted on 2006-06-14
5
1,622 Views
Last Modified: 2008-01-09
I'm using FindFirstFile to get a list of directories.

In the past I've used it as shown below with VS2005.  In this new project I'm getting a C2664 error because it wants the first parameter to be type LPCWSTR and that a const char*.

I can just cast it to a LPCWSTR but does anybody know why this error is showing up?  

Thanks.


      WIN32_FIND_DATA FindFile;
      HANDLE hFind = INVALID_HANDLE_VALUE;

        string myDir = "C:\\Temp\\*";

         hFind = FindFirstFile(myDir.c_str(), &FindFile);  

This results in the following:
error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
0
Comment
Question by:JohnSantaFe
5 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 16908280
Hi JohnSantaFe,
> does anybody know why this error is showing up?

Because the code is being compiled in UNICODE instead of ANSI.

You can use FindFirstFileA instead.
 hFind = FindFirstFileA(myDir.c_str(), &FindFile);

Cheers!
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 16908666
Or - if you need to use UNICODE - try

     WIN32_FIND_DATA FindFile;
     HANDLE hFind = INVALID_HANDLE_VALUE;

        wstring myDir = L"C:\\Temp\\*";

         hFind = FindFirstFile(myDir.c_str(), &FindFile);  
0
 
LVL 12

Assisted Solution

by:rajeev_devin
rajeev_devin earned 50 total points
ID: 16908740
I don't want to say that jkr's suggestion is wrong.

But I always encourage explicit function names in case of UNICODE and ANSI.
So, I would have done something like Axter has done

wstring myDir = L"C:\\Temp\\*";
hFind = FindFirstFileW(myDir.c_str(), &FindFile);
0
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 50 total points
ID: 16908997
You can try this also,
#include <atlconv.h>

WIN32_FIND_DATA FindFile;
HANDLE hFind = INVALID_HANDLE_VALUE;

string myDir = "C:\\Temp\\*";

char *c_str1 = myDir.c_str();

USES_CONVERSION;
hFind = FindFirstFile(A2T(c_str1) , &FindFile); //or A2W

Best Regards,
DeepuAbrahamK
0
 

Author Comment

by:JohnSantaFe
ID: 16912016
Thanks everybody.

I know there are good aruguments for using unicode and wstrings etc. but the rest of this application is not written that way and I don't think it ever will.

I changed the project properties to use a 'multi-byte character set' intead of unicode and that works.
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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 291
What does std::atomic give me? 7 113
Least Squares Curve Fitting 4 61
Header of docx file 17 98
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

14 Experts available now in Live!

Get 1:1 Help Now