-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComHelper.cpp
More file actions
79 lines (71 loc) · 2.01 KB
/
Copy pathComHelper.cpp
File metadata and controls
79 lines (71 loc) · 2.01 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
#include "pch.h"
#include "Systemic/ComHelper.h"
#include <strsafe.h>
#include <objbase.h>
#pragma comment(lib,"ole32.lib")
namespace Systemic::ComHelper
{
const char* copyToComBuffer(const char* str)
{
// Get string length
int strLen = str ? lstrlenA(str) : 0;
if (strLen == 0)
{
// We got an empty string, return a buffer with just a null terminator
char* utf8Str = (char*)::CoTaskMemAlloc(1);
if (utf8Str)
{
utf8Str[0] = '\0';
}
return utf8Str;
}
else
{
// Take the null terminator in account
++strLen;
// Allocate a COM buffer
char* utf8Str = (char*)::CoTaskMemAlloc(strLen);
if (utf8Str)
{
::memcpy(utf8Str, str, strLen);
}
return utf8Str;
}
}
const char* copyToComBuffer(const wchar_t* str)
{
// Get string length
int strLen = str ? lstrlenW(str) : 0;
if (strLen == 0)
{
// We got an empty string, return a buffer with just a null terminator
char* utf8Str = (char*)::CoTaskMemAlloc(1);
if (utf8Str)
{
utf8Str[0] = '\0';
}
return utf8Str;
}
else
{
// Take the null terminator in account
++strLen;
// Allocate a COM buffer
int utf8Len = WideCharToMultiByte(CP_UTF8, 0, str, strLen, 0, 0, NULL, NULL);
char* utf8Str = (char*)::CoTaskMemAlloc(utf8Len);
if (utf8Str)
{
// Copy and convert the string to UTF-8
if (!WideCharToMultiByte(CP_UTF8, 0, str, strLen, utf8Str, utf8Len, NULL, NULL))
{
utf8Str[0] = '\0';
}
}
return utf8Str;
}
}
void freeComBuffer(void* buffer)
{
::CoTaskMemFree(buffer);
}
}