Windows API - 使用WriteFile编写std :: string的正确方法是什么?

时间:2013-12-22 04:43:09

标签: c++ winapi

BOOL WINAPI WriteFile(
  _In_         HANDLE hFile,
  _In_         LPCVOID lpBuffer,
  _In_         DWORD nNumberOfBytesToWrite,
  _Out_opt_    LPDWORD lpNumberOfBytesWritten,
  _Inout_opt_  LPOVERLAPPED lpOverlapped
);

WriteFile采用const void *。如何将std :: string转换为const void *,以便将字符串的内容写入磁盘?

2 个答案:

答案 0 :(得分:5)

您可以使用c_str()char *获取std::string,然后您可以直接将其作为第二个参数传递给WriteFile。如果没有,那么只需将指针转换为LPCVOID

有关c_str()的详细信息,请参阅: std::string to char*

答案 1 :(得分:0)

如您所知,LPCVOID只是CONST void*的typedef。因此,您可以将const char*传递给该函数。 std::stringc_str()提供了一个函数。不需要强制转换,因为它们都是const限定的。