-
Notifications
You must be signed in to change notification settings - Fork 1
/
RtcWinUtils.cpp
135 lines (121 loc) · 3.74 KB
/
RtcWinUtils.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "pch.h"
#include "resource.h"
#include "RtcWinUtils.h"
CString GetModulePath()
{
CString strFilePath, tempStr;
TCHAR tszCurrentDirectory[MAX_PATH];
int nStartIndex = -1;
::GetModuleFileName(NULL, tszCurrentDirectory, MAX_PATH);
tempStr.Format(_T("%s"), tszCurrentDirectory);
nStartIndex = tempStr.ReverseFind(_T('\\'));
if (nStartIndex == -1) return _T("");
wsprintf(tszCurrentDirectory, _T("%s"), tempStr.Left(nStartIndex + 1));
strFilePath.Format(_T("%s"), tszCurrentDirectory);
return strFilePath;
}
CString GetAbsFilePath(CString strFilePath)
{
CString strAbsPath = strFilePath, tempStr;
UINT uLen = 0;
tempStr = GetModulePath();
if (tempStr.IsEmpty()) return strAbsPath;
uLen = tempStr.GetLength();
if (strAbsPath.Left(uLen).CompareNoCase(tempStr) == 0)
return strAbsPath;
strAbsPath = tempStr + strAbsPath;
return strAbsPath;
}
BOOL FileExist(CString strFilePath)
{
HANDLE hFind;
WIN32_FIND_DATA fdata;
hFind = ::FindFirstFile(strFilePath, &fdata);
if (hFind != INVALID_HANDLE_VALUE)
{
::FindClose(hFind); return TRUE;
}
return FALSE;
}
BOOL FolderExist(CString strFolderPath)
{
return FileExist(strFolderPath + _T("\\*.*"));
}
std::string CStringToStdString(CString strValue) {
std::wstring wbuffer;
size_t length;
#ifdef _UNICODE
wbuffer.assign(strValue.GetString(), strValue.GetLength());
#else
length = ::MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, (LPCTSTR)strValue, -1, NULL, 0);
wbuffer.resize(length);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strValue, -1, (LPWSTR)(wbuffer.data()), wbuffer.length());
#endif
length = WideCharToMultiByte(CP_UTF8, 0, wbuffer.data(), wbuffer.size(), NULL, 0, NULL, NULL);
std::string buffer;
buffer.resize(length);
WideCharToMultiByte(CP_UTF8, 0, strValue, -1, (LPSTR)(buffer.data()), length, NULL, NULL);
return (buffer);
}
CString StdStringToCString(std::string utf8str) {
int nLen = ::MultiByteToWideChar(CP_UTF8, NULL, utf8str.data(), utf8str.size(), NULL, 0);
std::wstring wbuffer;
wbuffer.resize(nLen);
::MultiByteToWideChar(CP_UTF8, NULL, utf8str.data(), utf8str.size(), (LPWSTR)(wbuffer.data()), wbuffer.length());
#ifdef UNICODE
return (CString(wbuffer.data(), wbuffer.length()));
#else
nLen = WideCharToMultiByte(CP_ACP, 0, wbuffer.data(), wbuffer.length(), NULL, 0, NULL, NULL);
std::string ansistr;
ansistr.resize(nLen);
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)(wbuffer.data()), wbuffer.length(), (LPSTR)(ansistr.data()), ansistr.size(), NULL, NULL);
return (CString(ansistr.data(), ansistr.length()));
#endif
}
BOOL CreateCACert(CString strFilePath) {
HGLOBAL hResourceLoaded;
HRSRC hRes;
void* lpResLock;
DWORD dwSize = 0;
BOOL bRst = FALSE;
if (FileExist(strFilePath)) return TRUE;
hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_BINARY1), _T("Binary"));
if (hRes) {
hResourceLoaded = LoadResource(NULL, hRes);
if (hResourceLoaded) {
lpResLock = LockResource(hResourceLoaded);
dwSize = SizeofResource(NULL, hRes);
if (dwSize > 0 && lpResLock) {
HANDLE hFile = CreateFile(strFilePath, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
bRst = WriteFile(hFile, lpResLock, dwSize, NULL, NULL);
CloseHandle(hFile);
}
}
FreeResource(hResourceLoaded);
}
}
return bRst;
}
void LogPrintf(const char* fmt, ...) {
char buffer[8192] = { 0 };
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
CStringA strLog = buffer;
OutputDebugStringA(strLog + "\n");
}
std::string GenerateUUID() {
std::string uuid;
GUID guid;
HRESULT hr = CoCreateGuid(&guid);
if (hr == S_OK) {
RPC_WSTR strGUID;
if (UuidToString(&guid, &strGUID) == RPC_S_OK) {
uuid = CStringToStdString((LPTSTR)strGUID);
RpcStringFree(&strGUID);
}
}
return uuid;
}