繁体   English   中英

需要帮助将一些 LPCTSTR 传递给 C++ 中的 function

[英]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) .

我正在尝试将其作为 function 添加到另一个项目中,并且无法传递名称、二进制路径等所需的 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 %s\n"),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] = %s\n"),i,argv[i]);

    Install(serviceName,serviceDisplayName,servicePath);

    return 0;
}

如果您已经拥有LPCTSTR ,那么您只需将 function 称为:

int result = Install(serviceName, serviceDisplayName, servicePath);

LPCTSTR 是一个指向 const TCHAR 字符串的长指针,因此它通常是const char *const wchar_t * ,具体取决于您的 unicode 设置。 因此,您可以使用许多常用方法来处理 C 字符串,以及 Microsoft 提供的任何字符串(我相信 MFC 具有一些字符串类/函数)。

暂无
暂无

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

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