2014-12-08 3 views
0

wstring을 WS_STRING으로 변환하는 가장 좋은 방법은 무엇입니까?wstring을 WS_STRING으로 변환

매크로를 시도 :

wstring d=L"ddd"; 
WS_STRING url = WS_STRING_VALUE(d.c_str()) ; 

을 그리고있는 오류 :

cannot convert from 'const wchar_t *' to 'WCHAR *' 
+0

당신은 아마'말썽 <>'자신에 const_cast 수 있습니다. 달려있어. –

답변

1

짧은 답변 :

WS_STRING url = {}; 
url.length = d.length(); 
WsAlloc(heap, sizeof(WCHAR) * url.length, (void**)&url.chars, error); 
memcpy(url.chars, d.c_str(), sizeof(WCHAR) * url.length); // Don't want a null terminator 

긴 대답 :

WCHAR[] 이외의 문자에는 WS_STRING_VALUE을 사용하지 마십시오. 당신은 const_cast<>를 사용하여 컴파일 얻을 수 있지만 두 가지 문제로 실행됩니다 :

  1. WS_STRING 잘못된 length 회원이있을 것이다 인해 매크로의 RTL_NUMBER_OF의 사용 대신에 널 (null) 종료를보고.
  2. WS_STRINGd을 참조하며 사본을 가져 오지 않습니다. 이것은 지역 변수 인 경우 분명히 문제가됩니다.

관련 코드 조각이 :

// Utilities structure 
// 
// An array of unicode characters and a length. 
// 
struct _WS_STRING { 
    ULONG length; 
    _Field_size_(length) WCHAR* chars; 
}; 

// Utilities macro 
// 
// A macro to initialize a WS_STRING structure given a constant string. 
// 
#define WS_STRING_VALUE(S) { WsCountOf(S) - 1, S } 

// Utilities macro 
// 
// Returns the number of elements of an array. 
// 
#define WsCountOf(arrayValue) RTL_NUMBER_OF(arrayValue) 
관련 문제