Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FindFirstFile error C2664

Posted on 2006-06-14
5
Medium Priority
?
1,661 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 400 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 200 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 200 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:Deepu Abraham
Deepu Abraham earned 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.
Suggested Courses

722 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