C++:将LPTSTR转换为char数组

Ole*_*syk 3 c c++ lptstr visual-studio

可能重复:
将lptstr转换为char*

我需要转换LPTSTR pCHAR ch[].我是C++的新手.

#include "stdafx.h"
#define _WIN32_IE 0x500
#include <shlobj.h>
#include <atlstr.h>
#include <iostream>
#include <Strsafe.h>

using namespace std;

int main(){
    int a;
    string res;
    CString path;
    char ch[MAX_PATH];
    LPTSTR p = path.GetBuffer(MAX_PATH);
    HRESULT hr = SHGetFolderPath(NULL,CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, p);

/* some operation with P and CH */

    if(SUCCEEDED(hr))
    { /* succeeded */
        cout << ch;
    } /* succeeded */
    else
    { /* failed */
        cout << "error";
    } /* failed */
    cin >> a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Ser*_* K. 6

LPTSTR是(非常量)TCHAR字符串.取决于它是否是Unicode,它出现.如果不是Unicode,LPTSTR是char*,如果不是,则为w_char*.

如果您使用的是非Unicode字符串,则LPTSTR只是一个char*,否则执行:

size_t size = wcstombs(NULL, p, 0);
char* CharStr = new char[size + 1];
wcstombs( CharStr, p, size + 1 );
Run Code Online (Sandbox Code Playgroud)

此外,此链接可以帮助:

将lptstr转换为char*