forked from ohio813/injector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.c
More file actions
139 lines (116 loc) · 2.69 KB
/
Copy pathmisc.c
File metadata and controls
139 lines (116 loc) · 2.69 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
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
////////////////////////////////////////////////////////////////////////////////////////////
// loader: command-line interface dll injector
// Copyright (C) 2009-2011 Wadim E. <wdmegrv@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////////////////
#include "misc.h"
void PrintErrorMsgA(char *format, ...)
{
va_list ap;
DWORD dwLastError = 0;
va_start(ap, format);
//printf("Error: ");
vprintf(format, ap);
va_end(ap);
dwLastError = GetLastError();
if(dwLastError)
{
printf(" [LastError: %d]", GetLastError());
}
SetLastError(0);
printf("\n");
}
void PrintErrorMsgW(wchar_t *format, ...)
{
va_list ap;
DWORD dwLastError = 0;
va_start(ap, format);
//printf("Error: ");
vwprintf(format, ap);
va_end(ap);
dwLastError = GetLastError();
if(dwLastError)
{
printf(" [LastError: %d]", GetLastError());
}
SetLastError(0);
printf("\n");
}
// This function will convert a char string to a wchar_t string.
wchar_t *__stdcall char_to_wchar_t(const char *src)
{
wchar_t *out_str = 0;
size_t len = 0;
int i = 0;
if(src == 0)
{
return 0;
}
len = (strlen(src) + 1) * sizeof(wchar_t);
out_str = (wchar_t*)malloc(len);
// memory allocation failed
if(!out_str)
{
printf("Error: Memory allocation failed.\n");
return 0;
}
memset(out_str, 0, len);
while(src[i] != '\0')
{
out_str[i] = (wchar_t)src[i];
++i;
}
return out_str;
}
int parse_int(const char *str)
{
int int_ret = 0;
if(str == 0)
{
return 0;
}
// try to get dec format
sscanf_s(str, "%d", &int_ret);
// try to get hex format
if(int_ret == 0 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
{
sscanf_s(str, "%x", &int_ret);
}
return int_ret;
}
char *alloc_stra(char *in_str)
{
size_t len = 0;
char *out_str = 0;
if(in_str == 0)
{
return 0;
}
len = strlen(in_str) + 1;
out_str = (char*)malloc(len);
// memory allocation failed
if(!out_str)
{
printf("Error: Memory allocation failed.\n");
return 0;
}
// strcpy_s failed
if(strcpy_s(out_str, len, in_str) != 0)
{
free(out_str);
return 0;
}
return out_str;
}