简体   繁体   English

MFC CString 和 LPTSTR 是否可以互换?

[英]Are MFC CString and LPTSTR interchangeable?

I am trying to use a DLL with an ANSI C compiler.我正在尝试将 DLL 与 ANSI C 编译器一起使用。 One of the DLL functions takes a void pointer. DLL 函数之一采用空指针。 In some sample Windows code that was provided with the DLL, the struct that gets passed to the function is defined as having three CString entities.在与 DLL 一起提供的一些示例 Windows 代码中,传递给函数的结构被定义为具有三个 CString 实体。 I have told the author of the DLL that they should not be passing MFC classes through their DLL functions.我已经告诉 DLL 的作者,他们不应该通过他们的 DLL 函数传递 MFC 类。 They have told me just to replace the CString declarations in the struct with char arrays and it should be fine.他们告诉我只是用 char 数组替换结构中的 CString 声明,应该没问题。 I'm 99% sure that's wrong, but since I don't have VC++ and don't have any experience with MFC, and since I've seen some posts saying LPTSTR can be used in place of CString ( What is `CString`? ), I'm starting to wonder if I'm wrong.我 99% 肯定那是错的,但是因为我没有 VC++ 并且没有任何 MFC 经验,而且因为我看到一些帖子说 LPTSTR 可以代替 CString (什么是CString ? ),我开始怀疑我是不是错了。

Can someone please confirm for me that CString and LPTSTR are not interchangeable as arguments to a function?有人可以帮我确认 CString 和 LPTSTR 作为函数的参数不能互换吗? If you can provide the source for the definition of the CString class, that would be helpful so I can send it to the DLL's author and explain that the memory footprint of a char array is not the same as a CString class, and that you can't pass a pointer to a struct that was defined with char arrays and then treat it as a bunch of CString objects.如果您可以提供 CString 类定义的源代码,那将很有帮助,因此我可以将其发送给 DLL 的作者并解释 char 数组的内存占用与 CString 类不同,并且您可以't 传递一个指向用 char 数组定义的结构的指针,然后将其视为一堆 CString 对象。

CString is an alias of the CStringT class template. CStringCStringT类模板的别名。 Objects of this class are really better not to pass to the DLL.此类的对象最好不要传递给 DLL。 The character type of the string class can be TCHAR (for both ANSI and Unicode character strings - see explanation below).字符串类的字符类型可以是TCHAR (对于 ANSI 和 Unicode 字符串 - 请参阅下面的说明)。 The definition of CString (and CStringT ) can most likely be found in the atlstr.h header file. CString (和CStringT )的定义很可能在atlstr.h头文件中找到。

LPTSTR is a regular pointer to a sequence of characters. LPTSTR是指向字符序列的常规指针。 The data type ( TCHAR* ) depends on the settings of the development environment: if the "Use Unicode Character set" option is selected, the TCHAR data type will be wchar_t (and LPTSTR will be wchar_t* , respectively).数据类型 ( TCHAR* ) 取决于开发环境的设置:如果选择了“使用 Unicode 字符集”选项,则TCHAR数据类型将为wchar_tLPTSTR将分别为wchar_t* )。 If the "Use Multi-byte character set" is selected, the TCHAR will be defined as char (and LPTSTR will be char* ).如果选择了“使用多字节字符集”,则TCHAR将被定义为char (而LPTSTR将是char* )。

So the question about interchangeability between CString and LPTSTR is not so simple.所以CStringLPTSTR之间的互换性问题并不是那么简单。 It also depends on how the DLL is written.它还取决于 DLL 的编写方式。 If the DLL was designed with the same environment settings as the main program, then CString and LPTSTR can really be interchangeable.如果 DLL 的设计环境设置与主程序相同,那么CStringLPTSTR真的可以互换。

Also, remember that CStirng is a class with many methods, while LPTSTR is just a pointer.另外,请记住CStirng是一个有很多方法的类,而LPTSTR只是一个指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM