簡體   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