我有一个包装 Win32 API 函数的类方法WriteFile()
。此 API 函数接受LPCVOID
要写入的数据的参数。但相反,我想将 C++ 迭代器传递给它。我该怎么做?
WriteFile()
功能:
BOOL WINAPI WriteFile(
_In_ HANDLE hFile,
_In_ LPCVOID lpBuffer,
_In_ DWORD nNumberOfBytesToWrite,
_Out_opt_ LPDWORD lpNumberOfBytesWritten,
_Inout_opt_ LPOVERLAPPED lpOverlapped
);
我的实现:
template <class Iter>
BinaryFile::RETURN_VALUES BinaryFile::Write(
Iter Begin,
Iter End,
DWORD & dwNumberOfBytesWritten /*= DUMMY_DWORD_REFERENCE*/)
{
if (m_nFileState != STATE_OPEN) return WRITE_FILE_NOT_OPEN;
BOOL bResult = WriteFile((HANDLE) m_hFile,
(LPCVOID) Begin,
(DWORD) sizeof(*Begin) * (End - Begin),
(LPDWORD) &dwNumberOfBytesWritten,
(LPOVERLAPPED) NULL);
m_dwLastError = ::GetLastError();
m_FilePointer += dwNumberOfBytesWritten;
if (bResult)
return WRITE_SUCCESSFUL;
else
return WRITE_FAILURE;
}
尝试调用它:
TextFile::RETURN_VALUES TextFile::Write(std::vector<uint8_t> & String)
{
DWORD dwNumberOfBytesWritten;
BinaryFile::RETURN_VALUES WriteReturn = m_File.Write(
String.begin(),
String.end(),
dwNumberOfBytesWritten);
if (WriteReturn == BinaryFile::RETURN_VALUES::WRITE_SUCCESSFUL)
return WRITE_SUCCESSFUL;
else if (dwNumberOfBytesWritten < String.end() - String.begin())
return WRITE_NOT_ALL_WRITTEN;
else
return WRITE_FAILURE;
}
我得到的编译器(VS2012)错误:
BinaryFile.h
错误 C2440
'type cast':无法从 'std::_Vector_iterator<_Myvec>' 转换为 'LPCVOID'