基本的に私がやりたいことは次のとおりです。
SerialPort::ReadBytes(int32& errcode, Message::string& msg, uint32 num)
{
DWORD numBytesRead = 0;
LPDWORD pNumBytesRead = &numBytesRead;
errcode = 0;
std::unique_ptr <char[]> buff (new char[num]);
// ^^^^ pass this char buffer to the ReadFile function below
if (!ReadFile(m_sp_pointer, // Handle to device
buff, // Receives data from device
num, // num bytes to read (in)
(LPDWORD)pNumBytesRead, // num bytes read (out)
NULL))
{
errcode = GetLastError();
}
if (numBytesRead > 0)
{
return true;
}
return false;
}
私はこれを正しく行っていないことを知っているので、私の質問は次のとおりです。これを正しく行うにはどうすればよいですか。これを悪い考えにするものはありますか。前もって感謝します。
編集:ローカルで宣言して渡すのではなく、実際にはパラメーターでunique_ptrを渡す必要がありますMessage::string& msg
。
私の最初の試みは、参照によってMessage::string
(std::string
)を渡すことでした。そのため、これもオプションです。つまり、unique_ptrをまったく使用する代わりに。その場合、ローカルで通常のを使用してからchar[]
、msg
コンテンツをに設定しchar[]
て返します。
どちらが良いかわかりませんが、vector<char>
代わりに多くの回答が推奨されているようです。(を使用するのと非常に似ていますstd::string
)。