如何在Visual C ++中正确调用CopyFile函数? [英] How do I properly call the CopyFile function in Visual C++?

查看:177
本文介绍了如何在Visual C ++中正确调用CopyFile函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它需要两个 CHAR * 作为输入。 int _stdcall在DLL项目中的FileTrans(char * InFile,char * OutFile)



m只是调用 CopyFile(InFile,OutFile,false); 经过一些进程后(与文件无关)。但它说它需要两个输入 LPCWSTR

解决方案

像接受字符串参数的所有Windows API函数一样,实际上是CopyFile函数的两个变体:




  • CopyFileA是ANSI版本,它采用 narrow 非Unicode)字符串在系统的默认字符集中。基本上,它接受类型 const char * 的参数,但是Windows头使用typedef LPCSTR p>


  • CopyFileW是宽版本,需要 Unicode 字符串。为了做到这一点,它接受类型 w_char * 的参数,但Windows头使用typedef LPCWSTR (注意typedef中的附加 W )。




然后,根据是否为您的项目定义了 UNICODE 预处理器宏(在包含Windows头文件之前的代码中,或者在Visual Studio中的项目属性中), Windows头将未修改的CopyFile定义为CopyFileA或CopyFileW。当然,如果定义 UNICODE ,CopyFile将被定义为Unicode版本CopyFileW。否则,它将被定义为CopyFileA。想法是调用一般的CopyFile函数在编译时自动解析为正确的变体。



当然,现在你明白了,大多忘记了。在现代Windows编程中,绝对没有理由调用旧的ANSI版本的函数或根本不处理窄字符串。忘记 char * 甚至可以用作字符串类型 - 这些字符串已经死了。你现在要使用的唯一字符串是Unicode字符串,由 wchar_t 字符组成。因此, UNICODE 符号应该总是为您的代码定义,你应该只使用 W UNICODE调用CopyFile时获得的函数相同)。



<
定义),我们看到:

  BOOL WINAPI CopyFile(LPCWSTR lpExistingFileName,
LPCWSTR lpNewFileName ,
BOOL bFailIfExists);

回想一下,你在上面得知, LPCWSTR 只是 const wchar_t * 的一个typedef同义词,一个由宽字符组成的C风格字符串。您已经知道为什么参数标记为 const :因为该函数不修改这些值。



你还学到上面这些是你应该使用的唯一类型的字符串,下一步是修改你的FileTrans函数接受宽字符串(并使他们 const 如果不修改它们):

  int _stdcall FileTrans(const wchar_t * InFile,const wchar_t * OutFile); 



现在,从FileTrans里面,你可以调用CopyFile没有任何问题,因为你有正确的类型字符串。






但有一点自由,额外的建议:从不使用C ++中的原始C风格字符串。始终使用由< string> 头定义在 std 命名空间中的C ++字符串类。



这个类有两个常见的变体, std :: string std :: wstring 。和以前一样, w 是指宽字符串,这是您要在Windows中使用的唯一类型。因此, std :: wstring 是您在代码库中替换 CHAR * 的新替代品。



更改FileTrans函数的声明如下:

  #include< string& 

// ...其他一些东西...

int __stdcall FileTrans(const std :: wstring& InFile,const std :: wstring& OutFile);

请注意,我已更改您的原始 CHAR * 参数指向 std :: wstring 对象的常量引用。常数引用在这里工作得很好,因为你不会在函数内部改变这些值。



如果你不清楚什么常数意味着什么使用引用,或类类型通常如何在C ++中工作,请咨询您的最喜欢的C ++书) - 这是所有C ++程序员所需的知识。记住C ++与C不是同一种语言,因此相同的成语不适用。在许多情况下,有更好的方法来做事情,这当然是这种情况的一个例子。


I have a function which takes two CHAR* as input viz. int _stdcall FileTrans(char* InFile, char* OutFile) in a DLL project.

In the function I'm just calling CopyFile(InFile, OutFile, false); after some process (not related to the files). But it says that it needs both inputs as LPCWSTR. I Googled it but couldn't find anything very interesting.

解决方案

Like all Windows API functions that accept a string parameter, there are actually two variants of the CopyFile function:

  • CopyFileA is the ANSI version, which takes narrow (non-Unicode) strings in the system's default character set. Basically, it accepts parameters of type const char*, but the Windows headers use the typedef LPCSTR for this.

  • CopyFileW is the wide version, which takes Unicode strings. In order to do this, it accepts parameters of type w_char*, but the Windows headers use the typedef LPCWSTR for this (note the additional W in the typedef).

Then, depending on whether the UNICODE preprocessor macro is defined for your project (either in your code before you include the Windows headers, or in your project's properties in Visual Studio), the Windows headers define the unadorned CopyFile as either CopyFileA or CopyFileW. Naturally, if UNICODE is defined, CopyFile will be defined as the Unicode version CopyFileW. Otherwise, it will be defined as CopyFileA. The idea is that the call to the general CopyFile function is automatically resolved at compile time to the correct variant.

Of course, now that you understand all of that, you can mostly forget about it. In modern Windows programming, there is absolutely no reason to call the old ANSI versions of functions or to deal with narrow strings at all. Forget that char* can even be used as a string type—those strings are dead to you. The only strings you're going to be using from now on are Unicode strings, composed of wchar_t characters. Thus the UNICODE symbol should always be defined for your code, and you should only use the W version of Windows API functions.

Looking again at the prototype for the CopyFileW function (the same one you get when you call CopyFile with UNICODE defined), we see:

BOOL WINAPI CopyFile(LPCWSTR lpExistingFileName,
                     LPCWSTR lpNewFileName,
                     BOOL bFailIfExists);

Recall that you learned above that LPCWSTR is just a typedef synonym for const wchar_t*, a C-style string that is composed of wide characters. You already know why the parameters are marked const: because the function doesn't modify those values.

And because you also learned above that these are the only types of strings that you should be using anymore, the next step is to modify your FileTrans function to accept wide strings (and make them const if it's not going to modify them):

int _stdcall FileTrans(const wchar_t* InFile, const wchar_t* OutFile);

Now, from inside of FileTrans, you can call CopyFile without any problems because you have the right type of strings.


But a bit of free, extra advice: never use raw C-style strings in C++. Always use the C++ string class, defined in the std namespace by the <string> header.

There are two common variants of this class, std::string and std::wstring. As before, the w refers to wide strings, which are the only type you want to use in Windows. So std::wstring is your new replacement for CHAR* throughout your code base.

Change your declaration of the FileTrans function to look like this:

#include <string>

// ...some other stuff...

int __stdcall FileTrans(const std::wstring& InFile, const std::wstring& OutFile);

Note that I've changed your original CHAR* parameters to constant references to std::wstring objects. Constant references work well here, since you're not going to be changing either of those values inside of the function.

If you're unclear on what constant means, how to use references, or how class types generally work in C++, please consult your favorite C++ book)—this is required knowledge for all C++ programmers. Remember that C++ is not the same language as C and therefore the same idioms do not apply. In many cases, there is a better way to do things, and this is certainly an example of such a case.

这篇关于如何在Visual C ++中正确调用CopyFile函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆