LPVOID in std::string umwandeln
-
Hi
std::string Socket::syserror(void) { LPVOID lpMsgBuf; DWORD dw = WSAGetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); //MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"Error", MB_OK | MB_ICONINFORMATION ); return lpMsgBuf; }
Wie wandle ich den lpMsgBuf in std::string um und wie gehe ich generell vor um ein soches Problem zu lösen?
Weshalb ist ein Cast auf &lpMsgBuf nötig (Instanzierung von lpMsgBuf als LPTSTR funktioniert nicht) ?
MfG Joe
-
du kannst statt LPVOID gleich LPTSTR verwenden und dann geht das ganz normal über den konstruktor. ansonsten mit nem cast.
-
und das LocalFree darfst du nicht vergessen.
LPTSTR lpMsgBuf; ... std::basic_string<TCHAR> systemErrorMessage(lpMsgBuf); LocalFree(lpMsgBuf); return systemErrorMessage;
-
Mit nem Cast auf was?
-
1>c:\c & c++ code\socketclass\socket.cpp(223) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it'
-
#include <windows.h> #include <string> std::basic_string<TCHAR> syserror(void) { LPTSTR lpMsgBuf; DWORD dw = WSAGetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); std::basic_string<TCHAR> systemErrorMessage(lpMsgBuf); LocalFree(lpMsgBuf); return systemErrorMessage; } int main() { SetLastError(ERROR_SUCCESS); MessageBox(NULL, syserror().c_str(), NULL, MB_OK); }