forked from wgwjifeng/op
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cpp
276 lines (240 loc) · 6.15 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// ConsoleTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
//#include "optest.hpp"
#include <time.h>
#include <chrono>
#include <filesystem>
#include "../include/op.h"
#include "../libop/imageProc/compute/ThreadPool.h"
#include "test.h"
#include "../libop/core/optype.h"
#pragma warning(disable : 4996)
using namespace std;
bool gRun = false;
mutex gMutex;
void proc_shared(libop *sharedOp) {
void *data = 0;
long size, ret;
while (gRun) {
this_thread::sleep_for(chrono::microseconds(100));
lock_guard<std::mutex> lock(gMutex);
cout << "thread:" << this_thread::get_id() << endl;
sharedOp->GetScreenDataBmp(0, 0, 50, 50, &data, &size, &ret);
}
}
void proc_unique(long hwnd) {
void *data = 0;
long size, ret;
libop *uniqueOp = new libop();
uniqueOp->BindWindow(hwnd, L"normal", L"normal", L"normal", 0, &ret);
while (gRun && ret) {
// this_thread::sleep_for(chrono::microseconds(100));
// lock_guard lock(gMutex);
cout << "thread:" << this_thread::get_id() << endl;
uniqueOp->GetScreenDataBmp(0, 0, 50, 50, &data, &size, &ret);
}
delete uniqueOp;
}
void test_shared() {
libop *sharedOp = new libop();
gRun = true;
thread t1(proc_shared, sharedOp);
thread t2(proc_shared, sharedOp);
cin.get();
gRun = false;
t1.join();
t2.join();
delete sharedOp;
}
void test_unique() {
gRun = true;
long hwnd[2] = {0x001407EE, 0x00010472};
thread t1(proc_unique, hwnd[0]);
thread t2(proc_unique, hwnd[1]);
cin.get();
gRun = false;
t1.join();
t2.join();
}
int test_com() {
#ifdef _WIN64
HMODULE hdll = LoadLibraryA("op_x64.dll");
#else
HMODULE hdll = LoadLibraryA("op_x86.dll");
#endif
if (!hdll) {
printf("LoadLibraryA false!\n");
return -1;
}
typedef HRESULT(__stdcall * DllGetClassObject_t)(
_In_ REFCLSID rclsid, _In_ REFIID riid, _Outptr_ LPVOID FAR * ppv);
DllGetClassObject_t pfun1 =
(DllGetClassObject_t)GetProcAddress(hdll, "DllGetClassObject");
if (!pfun1) {
printf("GetProcAddress false!\n");
return -2;
}
IClassFactory *fac = 0;
pfun1(CLSID_OpInterface, IID_IClassFactory, (void **)&fac);
if (!fac) {
printf("DllGetClassObject false!\n");
return -3;
}
IOpInterface *op;
fac->CreateInstance(NULL, IID_IOpInterface, (void **)&op);
if (!op) {
printf("CoCreateInstance false!\n");
return -4;
}
std::cout << "ok\n";
long hwnd = 0;
op->GetPointWindow(100, 100, &hwnd);
long ret = 0;
op->GetWindowState(hwnd, 2, &ret);
std::cout << "----ret----:" << ret << std::endl;
return 0;
}
void printHR(HRESULT hr) {
if (hr == S_OK) {
printf("\n");
} else if (hr == E_OUTOFMEMORY) {
printf("E_OUTOFMEMORY\n");
} else if (hr == DISP_E_UNKNOWNNAME) {
printf("DISP_E_UNKNOWNNAME\n");
} else if (hr == DISP_E_UNKNOWNLCID) {
printf("DISP_E_UNKNOWNLCID\n");
} else {
printf("other error code=%08X\n", hr);
}
}
// int test_invoke() {
// IOpInterface* op;
//
// //CoCreateInstance(&CLSID_OpInterface, NULL, CLSCTX_INPROC,
//&IID_IOpInterface, &op);
// /*if (op) {
// printf("use free com ok!\n");
// BSTR ver;
// op->lpVtbl->Ver(op, &ver);
// wprintf(L"op ver=%s", ver);
// }*/
// IDispatch* dispatch;
// HRESULT hr = 0;
// hr = CoCreateInstance(CLSID_OpInterface, NULL, CLSCTX_INPROC,
//IID_IDispatch, (void**)&dispatch); wchar_t* names = new wchar_t [256] ;
// memcpy(names, L"Ver", 8);
// if (dispatch == 0) {
// printf("error:CoCreateInstance\n");
// printHR(hr);
// return 0;
// }
//
// DISPID id;
// VARIANT ret;
// hr = dispatch->GetIDsOfNames(IID_NULL, &names, 1, LOCALE_SYSTEM_DEFAULT,
//&id); if (hr < 0) { printf("error:GetIDsOfNames\n"); printHR(hr); return 0;
// }
// DISPPARAMS parms = {
// NULL,
// 0,
// 0
// };
//
// hr = dispatch->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT,
//DISPATCH_METHOD, &parms, &ret, NULL, NULL); if (hr < 0) {
// printf("error:Invoke\n");
// printHR(hr);
// return 0;
// }
//
// if (ret.vt == VT_BSTR) {
// wprintf(ret.bstrVal);
// wprintf(L"\n");
// }
// else {
// printf("error:ret.vt != VT_BSTR\n");
// }
// return 0;
//}
int add(int a, int b) { return a + b; }
struct SimpleTest {
char a, b, c, d;
};
union Var {
SimpleTest sp;
int val;
};
void test_fs();
void test_threadPool();
void test_rect();
int main(int argc, char *argv[]) {
int a[2] = {0, 1};
int b = *a + 1;
int c = *(a + 1);
std::cout << "b:" << b << ", c:" << c << std::endl;
Var v = {};
v.sp.a = 1;
std::cout << "v:" << v.val << std::endl;
std::cout << add(1, 2);
// test_shared();
CoInitialize(NULL);
// test_unique();
// test_invoke();
test_com();
test *ptest = new test;
ptest->do_test();
delete ptest;
ptest = nullptr;
test_fs();
test_threadPool();
test_rect();
return 0;
}
void test_fs() {
namespace fs = std::filesystem;
std::cout << "current path:" << fs::current_path() << std::endl;
;
std::error_code e;
std::cout << "absolute: " << fs::absolute("../", e) << std::endl;
std::cout << "err code:" << e << std::endl;
std::cout << "absolute: " << fs::absolute("../e", e) << std::endl;
std::cout << "err code:" << e << std::endl;
}
void test_threadPool(){
size_t maxThread = std::thread::hardware_concurrency();
std::cout<<"max support thread num : "<<maxThread<<std::endl;
ThreadPool pool(4);
std::vector<std::future<int>> results;
for(int i = 0; i < 8; ++i) {
results.emplace_back(
pool.enqueue([i] {
std::cout << "hello " << i << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "world " << i << std::endl;
return i*i;
})
);
}
for(auto && result: results)
std::cout << result.get() << ' ';
std::cout << std::endl;
}
void test_rect(){
rect_t rc(0,0,100,100);
std::vector<rect_t> blocks;
rc.divideBlock(3,false, blocks);
for(auto& r : blocks){
std::cout<<r.x1<<","<<r.y1<<","<<r.x2<<","<<r.y2<<std::endl;
}
rc.divideBlock(3,true, blocks);
for(auto& r : blocks){
std::cout<<r.x1<<","<<r.y1<<","<<r.x2<<","<<r.y2<<std::endl;
}
}