This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
dump.h
72 lines (63 loc) · 1.75 KB
/
dump.h
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
#ifndef VERSION_DUMP_H
#define VERSION_DUMP_H
#include <Windows.h>
#include <Shlobj.h>
#include <string>
#include <mutex>
#include "parser.h"
using namespace std;
mutex dumpMutex;
bool fileExists(const wstring &name) {
if (FILE *file = _wfopen(name.c_str(), L"r")) {
fclose(file);
return true;
} else {
return false;
}
}
bool getNextFreeFilePath(wstring &path, const char *name) {
path.resize(MAX_PATH);
if (!SHGetSpecialFolderPathW(HWND_DESKTOP, path.data(), CSIDL_DESKTOP, FALSE)) {
return false;
}
path.resize(wcslen(path.data()));
wstring fixedName(MAX_PATH, '\x00');
mbstowcs(fixedName.data(), name, strlen(name));
PathCleanupSpec(nullptr, fixedName.data());
fixedName.resize(wcslen(fixedName.data()));
int initialLen = path.length();
int i = 0;
do {
path.resize(initialLen);
path += L"\\JVMDUMP\\";
path += fixedName;
if (i != 0) {
path += to_wstring(i);
}
path += L".class";
i++;
} while (fileExists(path));
return true;
}
void DoDump(const char *buf, int len) {
if (!buf || len < 1) {
return;
}
lock_guard<mutex> lock(dumpMutex);
FILE *fp;
wstring path;
string className = GetJavaClassName(buf);
if (getNextFreeFilePath(path, className.c_str())) {
fp = _wfopen(path.c_str(), L"wb");
int written = fwrite(buf, sizeof(char), len, fp);
if (written == 0) {
MessageBox(nullptr, "Error 2", "Error", MB_OK);
} else if (written != sizeof(char) * len) {
MessageBox(nullptr, "Error 3", "Error", MB_OK);
}
fclose(fp);
} else {
MessageBox(nullptr, "Error 1", "Error", MB_OK);
}
}
#endif //VERSION_DUMP_H