1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2004 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #include "CMSWindowsUtil.h"
|
---|
16 | #include "CStringUtil.h"
|
---|
17 | #include <stdio.h>
|
---|
18 |
|
---|
19 | //
|
---|
20 | // CMSWindowsUtil
|
---|
21 | //
|
---|
22 |
|
---|
23 | CString
|
---|
24 | CMSWindowsUtil::getString(HINSTANCE instance, DWORD id)
|
---|
25 | {
|
---|
26 | char buffer[1024];
|
---|
27 | int size = static_cast<int>(sizeof(buffer) / sizeof(buffer[0]));
|
---|
28 | char* msg = buffer;
|
---|
29 |
|
---|
30 | // load string
|
---|
31 | int n = LoadString(instance, id, msg, size);
|
---|
32 | msg[n] = '\0';
|
---|
33 | if (n < size) {
|
---|
34 | return msg;
|
---|
35 | }
|
---|
36 |
|
---|
37 | // not enough buffer space. keep trying larger buffers until
|
---|
38 | // we get the whole string.
|
---|
39 | msg = NULL;
|
---|
40 | do {
|
---|
41 | size <<= 1;
|
---|
42 | delete[] msg;
|
---|
43 | char* msg = new char[size];
|
---|
44 | n = LoadString(instance, id, msg, size);
|
---|
45 | } while (n == size);
|
---|
46 | msg[n] = '\0';
|
---|
47 |
|
---|
48 | CString result(msg);
|
---|
49 | delete[] msg;
|
---|
50 | return result;
|
---|
51 | }
|
---|
52 |
|
---|
53 | CString
|
---|
54 | CMSWindowsUtil::getErrorString(HINSTANCE hinstance, DWORD error, DWORD id)
|
---|
55 | {
|
---|
56 | char* buffer;
|
---|
57 | if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
58 | FORMAT_MESSAGE_IGNORE_INSERTS |
|
---|
59 | FORMAT_MESSAGE_FROM_SYSTEM,
|
---|
60 | 0,
|
---|
61 | error,
|
---|
62 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
63 | (LPTSTR)&buffer,
|
---|
64 | 0,
|
---|
65 | NULL) == 0) {
|
---|
66 | CString errorString = CStringUtil::print("%d", error);
|
---|
67 | return CStringUtil::format(getString(hinstance, id).c_str(),
|
---|
68 | errorString.c_str());
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | CString result(buffer);
|
---|
72 | LocalFree(buffer);
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 | }
|
---|