짧은 답변 :
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<>
를 사용하여 컴파일 얻을 수 있지만 두 가지 문제로 실행됩니다 :
WS_STRING
잘못된 length
회원이있을 것이다 인해 매크로의 RTL_NUMBER_OF
의 사용 대신에 널 (null) 종료를보고.
WS_STRING
은 d
을 참조하며 사본을 가져 오지 않습니다. 이것은 지역 변수 인 경우 분명히 문제가됩니다.
관련 코드 조각이 :
// 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)
당신은 아마'말썽 <>'자신에 const_cast 수 있습니다. 달려있어. –