-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathNativeMethods.cs
253 lines (140 loc) · 13.3 KB
/
NativeMethods.cs
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
// Copyright (c) libplctag.NET contributors
// https://github.com/libplctag/libplctag.NET
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
using System;
using System.Runtime.InteropServices;
using System.Text;
using static libplctag.NativeImport.plctag;
namespace libplctag.NativeImport
{
static class NativeMethods
{
const string DLL_NAME = "plctag";
static NativeMethods()
{
#if NET472_OR_GREATER
// Assume we're running on Windows because .NET Framework is not supported on other platforms
string executingDirectory = System.IO.Path.GetDirectoryName(typeof(NativeMethods).Assembly.Location);
string system = Environment.Is64BitProcess ? "X64" : "X86";
string libDirectory = System.IO.Path.Combine (executingDirectory, system);
if (System.IO.Directory.Exists(libDirectory))
{
SetDllDirectory(libDirectory);
}
else
{
SetDllDirectory(executingDirectory);
}
#endif
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetDllDirectory(string lpPathName);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_check_lib_version), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_create), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern Int32 plc_tag_create([MarshalAs(UnmanagedType.LPStr)] string lpString, int timeout);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_create_ex), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern Int32 plc_tag_create_ex([MarshalAs(UnmanagedType.LPStr)] string lpString, callback_func_ex func, IntPtr userdata, int timeout);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_destroy), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_destroy(Int32 tag);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_shutdown), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void plc_tag_shutdown();
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_callback), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_register_callback(Int32 tag_id, callback_func func);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_callback_ex), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_register_callback_ex(Int32 tag_id, callback_func_ex func, IntPtr userdata);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unregister_callback), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_unregister_callback(Int32 tag_id);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_register_logger), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_register_logger(log_callback_func func);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unregister_logger), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_unregister_logger(Int32 tag_id);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_lock), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_lock(Int32 tag);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_unlock), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_unlock(Int32 tag);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_status), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_status(Int32 tag);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_decode_error), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern IntPtr plc_tag_decode_error(int err);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_read), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_read(Int32 tag, int timeout);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_write), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_write(Int32 tag, int timeout);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_size), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_get_size(Int32 tag);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_size), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_size(Int32 tag, int new_size);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_abort), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_abort(Int32 tag);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern int plc_tag_get_int_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, int default_value);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern int plc_tag_set_int_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, int new_value);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_byte_array_attribute), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern int plc_tag_get_byte_array_attribute(Int32 tag, [MarshalAs(UnmanagedType.LPStr)] string attrib_name, [In] byte[] buffer, int buffer_length);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern UInt64 plc_tag_get_uint64(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Int64 plc_tag_get_int64(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_uint64(Int32 tag, int offset, UInt64 val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_int64(Int32 tag, int offset, Int64 val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_float64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern double plc_tag_get_float64(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_float64), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_float64(Int32 tag, int offset, double val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern UInt32 plc_tag_get_uint32(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Int32 plc_tag_get_int32(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_uint32(Int32 tag, int offset, UInt32 val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_int32(Int32 tag, int offset, Int32 val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_float32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern float plc_tag_get_float32(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_float32), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_float32(Int32 tag, int offset, float val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern UInt16 plc_tag_get_uint16(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Int16 plc_tag_get_int16(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_uint16(Int32 tag, int offset, UInt16 val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int16), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_int16(Int32 tag, int offset, Int16 val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_uint8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern byte plc_tag_get_uint8(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_int8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern sbyte plc_tag_get_int8(Int32 tag, int offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_uint8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_uint8(Int32 tag, int offset, byte val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_int8), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_int8(Int32 tag, int offset, sbyte val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_bit), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_get_bit(Int32 tag, int offset_bit);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_bit), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_bit(Int32 tag, int offset_bit, int val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_debug_level), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void plc_tag_set_debug_level(int debug_level);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern int plc_tag_get_string(Int32 tag_id, int string_start_offset, StringBuilder buffer, int buffer_length);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_string), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern int plc_tag_set_string(Int32 tag_id, int string_start_offset, [MarshalAs(UnmanagedType.LPStr)] string string_val);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_length), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_get_string_length(Int32 tag_id, int string_start_offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_capacity), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_get_string_capacity(Int32 tag_id, int string_start_offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_string_total_length), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_get_string_total_length(Int32 tag_id, int string_start_offset);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, [Out] byte[] buffer, int buffer_length);
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, [In] byte[] buffer, int buffer_length);
}
}