需要帮助将一些 LPCTSTR 传递给 C++ 中的函数
Need help passing some LPCTSTR's to a function in C++
我对C++很陌生,有一个问题可能很明显。我可以使用MSDN示例来安装服务(http://msdn.microsoft.com/en-us/library/ms682450%28v=VS.85%29.aspx)如果我把它放在一个单独的项目中。
我正试图将其添加为另一个项目中的函数,并且在传递名称、二进制路径等所需的LPCTSTR字符串时遇到了问题。
到目前为止,我已经尝试过:
int Install(LPCTSTR serviceName, LPCTSTR serviceDisplayName, LPCTSTR servicePath);
我知道这是错误的,但我很难弄清楚我到底应该用什么。即使是指向解释的链接也可以。谢谢
LPCTSTR是
long pointer to const text string
根据您是否针对UNICODE/MBCS/ANSI构建,您需要
const char*
(ANSI/MBCS(const wchar_t*
(UNICODE(
(来自内存(
这里有一个支持Unicode或非Unicode构建的示例。请注意,您希望定义的UNICODE
和_UNICODE
都能在Unicode构建中正常工作。换行_T
宏中的所有文本字符串。
#include <windows.h> /* defines LPCTSTR and needs UNICODE defined for wide build. */
#include <stdio.h>
#include <tchar.h> /* defines _T, _tprintf, _tmain, etc. and needs _UNICODE defined for wide build. */
int Install(LPCTSTR serviceName, LPCTSTR serviceDisplayName, LPCTSTR servicePath)
{
_tprintf(_T("%s %s %sn"),serviceName,serviceDisplayName,servicePath);
return 0;
}
int _tmain(int argc, LPTSTR argv[])
{
int i;
LPCTSTR serviceName = _T("serviceName");
LPCTSTR serviceDisplayName = _T("serviceDisplayName");
LPCTSTR servicePath = _T("servicePath");
for(i = 0; i < argc; i++)
_tprintf(_T("argv[%d] = %sn"),i,argv[i]);
Install(serviceName,serviceDisplayName,servicePath);
return 0;
}
如果您已经有了LPCTSTR
s,那么您只需将函数调用为:
int result = Install(serviceName, serviceDisplayName, servicePath);
LPCTSTR是一个指向常量TCHAR字符串的长指针,因此它通常是const char *
或const wchar_t *
,具体取决于您的unicode设置。因此,您可以使用许多常用的方法来处理C字符串,以及Microsoft提供的任何方法(我相信MFC有一些字符串类/函数(。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗