简体   繁体   English

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

[英]Need help passing some LPCTSTR's to a function in C++

I'm very new to C++ and have a question that is probably obvious.我对 C++ 非常陌生,并且有一个可能很明显的问题。 I am able to use the MSDN example to install a service (http://msdn.microsoft.com/en-us/library/ms682450%28v=VS.85%29.aspx) if I have it in a stand alone program.如果我将它放在独立程序中,我可以使用 MSDN 示例安装服务 (http://msdn.microsoft.com/en-us/library/ms682450%28v=VS.85%29.aspx) .

I'm trying to add this as a function inside another project and am having trouble passing the LPCTSTR strings it needs for the name, binary path etc.我正在尝试将其作为 function 添加到另一个项目中,并且无法传递名称、二进制路径等所需的 LPCTSTR 字符串。

So far I have tried:到目前为止,我已经尝试过:

int Install(LPCTSTR serviceName, LPCTSTR serviceDisplayName, LPCTSTR servicePath);

I know this is wrong, but am having a hard time finding out what I should use exactly.我知道这是错误的,但很难找出我应该使用什么。 Even a link pointing to an explanation is fine.即使是指向解释的链接也可以。 Thanks!谢谢!

LPCTSTR is LPCTSTR 是

long pointer to const text string

Depending on whether you are targeting a UNICODE/MBCS/ANSI build you'd need取决于您是否针对您需要的 UNICODE/MBCS/ANSI 构建

  • const char* (ANSI/MBCS) const char* (ANSI/MBCS)
  • const wchar_t* (UNICODE) const wchar_t* (UNICODE)

(from memory) (从记忆里)

Here's an example that support Unicode or non-Unicode builds.这是一个支持 Unicode 或非 Unicode 版本的示例。 Note you want both UNICODE and _UNICODE defined to work properly in a Unicode build.请注意,您希望UNICODE_UNICODE定义在 Unicode 构建中正常工作。 Wrap all text strings in the _T macro.所有文本字符串包装在_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;
}

If you already have the LPCTSTR s, then you simply call the function as:如果您已经拥有LPCTSTR ,那么您只需将 function 称为:

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

LPCTSTR is a long pointer to a const TCHAR string, so it's usually const char * or const wchar_t * , depending on your unicode settings. LPCTSTR 是一个指向 const TCHAR 字符串的长指针,因此它通常是const char *const wchar_t * ,具体取决于您的 unicode 设置。 Due to that, you can use a lot of the usual methods for working with C strings, as well as any Microsoft provides (I believe the MFC has some string classes/functions).因此,您可以使用许多常用方法来处理 C 字符串,以及 Microsoft 提供的任何字符串(我相信 MFC 具有一些字符串类/函数)。

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

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