-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsHelpers.cpp
More file actions
85 lines (67 loc) · 2.27 KB
/
Copy pathWindowsHelpers.cpp
File metadata and controls
85 lines (67 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
///////////////////////////////////////////////////////////////////////
// WindowsHelper.cpp - small helper functions for using Windows API //
// ver 1.0 //
//-------------------------------------------------------------------//
// Jim Fawcett (c) copyright 2015 //
// All rights granted provided this copyright notice is retained //
//-------------------------------------------------------------------//
// Language: C++, Visual Studio 2015 //
// Application: Several Projects, CSE687 - Object Oriented Design //
// Author: Jim Fawcett, Syracuse University, CST 4-187 //
// jfawcett@twcny.rr.com //
///////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <string>
#include <winsock2.h>
#include "WindowsHelpers.h"
using namespace WindowsHelpers;
#pragma comment(lib, "Ws2_32.lib")
std::string WindowsHelpers::wstringToString(const std::wstring& wstr)
{
std::string rtn;
for (auto ch : wstr)
rtn += static_cast<char>(ch);
rtn += '\0';
return rtn;
}
std::wstring WindowsHelpers::stringToWideString(const std::string& str)
{
std::wstring rtn;
for (auto ch : str)
rtn += ch;
rtn += static_cast<wchar_t>('\0');
return rtn;
}
//----< get socket error message string >----------------------------
std::string WindowsHelpers::GetLastMsg(bool WantSocketMsg) {
// ask system what type of error occurred
DWORD errorCode;
if (WantSocketMsg)
errorCode = WSAGetLastError();
else
errorCode = GetLastError();
if (errorCode == 0)
return "no error";
// map errorCode into a system defined error string
DWORD dwFlags =
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER;
LPCVOID lpSource = NULL;
DWORD dwMessageID = errorCode;
DWORD dwLanguageId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
LPSTR lpBuffer;
DWORD nSize = 0;
va_list *Arguments = NULL;
FormatMessage(
dwFlags, lpSource, dwMessageID, dwLanguageId,
(LPTSTR)&lpBuffer, nSize, Arguments
);
std::string _msg(lpBuffer);
LocalFree(lpBuffer);
return _msg;
}
#ifdef TEST_WINDOWSHELPERS
int main()
{
}
#endif