Solved

FindFirstFile error C2664

Posted on 2006-06-14
5
1,618 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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