Solved

FindFirstFile error C2664

Posted on 2006-06-14
5
1,644 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
[X]
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
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 157
Problem with SqlConnection 4 194
object oriented programming comparison 5 94
visual C++ 1 43
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 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.

752 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