-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstance.cpp
85 lines (75 loc) · 2.16 KB
/
instance.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
//
// instance.cpp - instance implementation
//
// lecui user interface library, part of the liblec library
// Copyright (c) 2019 Alec Musasa (alecmus at live dot com)
//
// Released under the MIT license. For full details see the
// file LICENSE.txt
//
#include "instance.h"
#include "form_impl/form_impl.h"
#include "error/win_error.h"
namespace liblec {
namespace lecui {
class instance_manager::impl {
public:
impl(form& fm, const std::string& guid) :
_fm(fm) {
if (!guid.empty()) {
// capture form guid
if (fm._d._guid.empty())
fm._d._guid = guid;
// for limiting the number of instances of the app
if (!fm._d._p_instance)
fm._d._p_instance = new limit_single_instance(guid);
}
}
form& _fm;
};
instance_manager::instance_manager(form& fm, const std::string& guid) : _d(*(new impl(fm, guid))) {}
instance_manager::~instance_manager() { delete& _d; }
bool instance_manager::send_data(const std::string& guid, const std::string& data,
const long& timeout_milliseconds, std::string& error) {
// find the form's native handle
auto hWnd = _d._fm._d.find_native_handle(guid);
if (hWnd == nullptr) {
error = "Instance not found";
return false;
}
else {
// pass the commandline to the existing instance
LPSTR szData = (LPSTR)data.c_str();
COPYDATASTRUCT cds;
cds.cbData = static_cast<DWORD>(strlen(szData) + 1);
cds.lpData = szData;
DWORD_PTR result = 0;
if (SendMessageTimeout(hWnd,
WM_COPYDATA, // message
0, // WPARAM
(LPARAM)&cds, // LPARAM
SMTO_BLOCK |
SMTO_ABORTIFHUNG,
static_cast<UINT>(timeout_milliseconds),
&result) == 0) {
error = "Timeout while sending data";
return false;
}
else {
switch (result) {
case form::impl::instance_messages::busy:
error = "Previously sent data is still being processed by the target instance";
return false;
case form::impl::instance_messages::no_handler:
error = "Target instance has no handler for the sent data";
return false;
case form::impl::instance_messages::handled:
default:
break;
}
}
return true;
}
}
}
}