Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

FindFirstFile error C2664

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

840 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