Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
213 | f9daq | 1 | using RGiesecke.DllExport; |
2 | using System; |
||
3 | using System.Text; |
||
4 | using System.Runtime.InteropServices; |
||
5 | |||
6 | using AitMduManager; |
||
7 | |||
8 | /* |
||
9 | #region Assembly AitMduManager, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null |
||
10 | // AitMduManager.dll |
||
11 | #endregion |
||
12 | */ |
||
13 | |||
14 | // https://blogs.msdn.microsoft.com/jaredpar/2008/11/05/dereference-a-double-intptr/ |
||
15 | |||
16 | |||
17 | namespace NativeExports |
||
18 | { |
||
19 | class Program |
||
20 | { |
||
21 | |||
22 | static MduManager SD4 = new MduManager(); |
||
23 | |||
24 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
25 | static int SD4_BlockError() |
||
26 | { |
||
27 | return SD4.BlockError; |
||
28 | } |
||
29 | |||
30 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
31 | static void SD4_SetBlockTimeout(uint a) |
||
32 | { |
||
33 | SD4.BlockTimeout = a; |
||
34 | } |
||
35 | |||
36 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
37 | static uint SD4_GetBlockTimeout() |
||
38 | { |
||
39 | return SD4.BlockTimeout; |
||
40 | } |
||
41 | |||
42 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
43 | static long SD4_BlockTransferTime() |
||
44 | { |
||
45 | return SD4.BlockTransferTime; |
||
46 | } |
||
47 | |||
48 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
49 | static uint SD4_GetDriverTimeout() |
||
50 | { |
||
51 | return SD4.DriverTimeout; |
||
52 | } |
||
53 | |||
54 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
55 | static void SD4_SetDriverTimeout(uint tout) |
||
56 | { |
||
57 | SD4.DriverTimeout = tout; |
||
58 | } |
||
59 | |||
60 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
61 | static IntPtr SD4_Version() |
||
62 | { |
||
63 | return Marshal.StringToHGlobalAnsi(SD4.Version); |
||
64 | } |
||
65 | |||
66 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
67 | static int SD4_WordsTransferred() |
||
68 | { |
||
69 | return SD4.WordsTransferred; |
||
70 | } |
||
71 | |||
72 | /* |
||
73 | |||
74 | public event EventHandler DeviceAttached; |
||
75 | public event EventHandler DeviceRemoved; |
||
76 | public event EventHandler FirmwareStatusChanged; |
||
77 | */ |
||
78 | |||
79 | static IntPtr SD4_ArrayToIntPtr(int[] retval) |
||
80 | { |
||
81 | int len = retval.Length * Marshal.SizeOf(typeof(int)); |
||
82 | SD4_AllocateMemory(len); |
||
83 | Marshal.Copy(retval, 0, _unmanagedMemory, retval.Length); |
||
84 | return _unmanagedMemory; |
||
85 | } |
||
86 | |||
87 | static IntPtr SD4_ArrayToIntPtr(ushort[] retval) |
||
88 | { |
||
89 | int len = retval.Length * Marshal.SizeOf(typeof(ushort)); |
||
90 | SD4_AllocateMemory(len); |
||
91 | Marshal.Copy((short[])(object) retval, 0, _unmanagedMemory, retval.Length); |
||
92 | return _unmanagedMemory; |
||
93 | } |
||
94 | |||
95 | static IntPtr SD4_ArrayToIntPtr(uint[] retval) |
||
96 | { |
||
97 | int len = retval.Length * Marshal.SizeOf(typeof(uint)); |
||
98 | SD4_AllocateMemory(len); |
||
99 | Marshal.Copy((int[])(object) retval, 0, _unmanagedMemory, retval.Length); |
||
100 | return _unmanagedMemory; |
||
101 | } |
||
102 | |||
103 | static IntPtr SD4_ArrayToIntPtr(byte[] retval) |
||
104 | { |
||
105 | int len = retval.Length * Marshal.SizeOf(typeof(byte)); |
||
106 | SD4_AllocateMemory(len); |
||
107 | Marshal.Copy(retval, 0, _unmanagedMemory, retval.Length); |
||
108 | return _unmanagedMemory; |
||
109 | } |
||
110 | |||
111 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
112 | static IntPtr SD4_FindDevices( ref int len) |
||
113 | { |
||
114 | uint[] devices = SD4.FindDevices(); |
||
115 | if (devices == null) |
||
116 | { |
||
117 | len = 0; |
||
118 | return IntPtr.Zero; |
||
119 | } |
||
120 | else |
||
121 | { |
||
122 | len = devices.Length; |
||
123 | return SD4_ArrayToIntPtr(devices); |
||
124 | } |
||
125 | |||
126 | } |
||
127 | |||
128 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
129 | static int SD4_GetDeviceCount() |
||
130 | { |
||
131 | return SD4.GetDeviceCount(); |
||
132 | } |
||
133 | |||
134 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
135 | static IntPtr SD4_GetDeviceList( ref int len) |
||
136 | { |
||
137 | uint[] devices = SD4.GetDeviceList(); |
||
138 | if (devices == null) |
||
139 | { |
||
140 | len = 0; |
||
141 | return IntPtr.Zero; |
||
142 | } |
||
143 | else |
||
144 | { |
||
145 | len = devices.Length; |
||
146 | return SD4_ArrayToIntPtr(devices); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | |||
151 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
152 | static IntPtr SD4_GetUsbFirmwareVersionByDevice(uint deviceId) |
||
153 | { |
||
154 | return Marshal.StringToHGlobalAnsi(SD4.GetUsbFirmwareVersion(deviceId)); |
||
155 | } |
||
156 | |||
157 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
158 | static IntPtr SD4_GetUsbFirmwareVersion(int index) |
||
159 | { |
||
160 | return Marshal.StringToHGlobalAnsi(SD4.GetUsbFirmwareVersion(index)); |
||
161 | } |
||
162 | |||
163 | |||
164 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
165 | static bool SD4_IsDeviceAttached(uint deviceId) |
||
166 | { |
||
167 | return SD4.IsDeviceAttached(deviceId); |
||
168 | } |
||
169 | |||
170 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
171 | static bool SD4_ProgramFpgaFirmwareByDevice(uint deviceId, IntPtr data, int len) |
||
172 | { |
||
173 | byte[] fpgaFirmware = new byte[len]; |
||
174 | Marshal.Copy(data, fpgaFirmware, 0, len); |
||
175 | return SD4.ProgramFpgaFirmware(deviceId, fpgaFirmware); |
||
176 | } |
||
177 | |||
178 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
179 | static bool SD4_ProgramFpgaFirmware(int index, IntPtr data, int len) |
||
180 | { |
||
181 | byte[] fpgaFirmware = new byte[len]; |
||
182 | Marshal.Copy(data, fpgaFirmware, 0, len); |
||
183 | return SD4.ProgramFpgaFirmware(index, fpgaFirmware); |
||
184 | } |
||
185 | |||
186 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
187 | static bool SD4_ProgramUsbFirmware(int index, IntPtr data, int len) |
||
188 | { |
||
189 | byte[] usbFirmware = new byte[len]; |
||
190 | Marshal.Copy(data, usbFirmware, 0, len); |
||
191 | return SD4.ProgramUsbFirmware(index, usbFirmware); |
||
192 | } |
||
193 | |||
194 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
195 | static ushort SD4_Read(uint deviceId, int address) |
||
196 | { |
||
197 | return SD4.Read(deviceId, address); |
||
198 | } |
||
199 | |||
200 | |||
201 | |||
202 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
203 | static IntPtr SD4_ReadByteArray(uint deviceId, int address, int requested_elements , ref int len) |
||
204 | { |
||
205 | SD4_AllocateByteBuffer(requested_elements); |
||
206 | SD4.Read(deviceId, address, SD4_ByteBuffer); |
||
207 | _unmanagedMemory = SD4_ArrayToIntPtr(SD4_ByteBuffer); |
||
208 | len = _unmanagedMemoryLength; |
||
209 | return _unmanagedMemory; |
||
210 | } |
||
211 | |||
212 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
213 | static IntPtr SD4_ReadUShortArray(uint deviceId, int address, int requested_elements, ref int len) |
||
214 | { |
||
215 | /* |
||
216 | System.Console.WriteLine("SD4_ReadUShortArray"); |
||
217 | System.Console.WriteLine(deviceId); |
||
218 | System.Console.WriteLine(address); |
||
219 | System.Console.WriteLine(requested_elements); |
||
220 | */ |
||
221 | |||
222 | SD4_AllocateUshortBuffer(requested_elements); |
||
223 | SD4.Read(deviceId, address, SD4_UShortBuffer); |
||
224 | _unmanagedMemory = SD4_ArrayToIntPtr(SD4_UShortBuffer); |
||
225 | len = _unmanagedMemoryLength; |
||
226 | return _unmanagedMemory; |
||
227 | } |
||
228 | |||
229 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
230 | static void SD4_ReconfigureByDevice(uint deviceId) |
||
231 | { |
||
232 | SD4.Reconfigure(deviceId); |
||
233 | } |
||
234 | |||
235 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
236 | static void SD4_Reconfigure(int index) |
||
237 | { |
||
238 | SD4.Reconfigure(index); |
||
239 | } |
||
240 | |||
241 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
242 | static void SD4_ReconnectByDevice(uint deviceId) |
||
243 | { |
||
244 | SD4.Reconnect(deviceId); |
||
245 | } |
||
246 | |||
247 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
248 | static void SD4_Reconnect(int index) |
||
249 | { |
||
250 | SD4.Reconnect(index); |
||
251 | } |
||
252 | |||
253 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
254 | static void SD4_ResetFpga(int index) |
||
255 | { |
||
256 | SD4.ResetFpga(index); |
||
257 | } |
||
258 | |||
259 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
260 | static void SD4_ResetFpgaByDevice(uint deviceId) |
||
261 | { |
||
262 | SD4.ResetFpga(deviceId); |
||
263 | } |
||
264 | |||
265 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
266 | static void SD4_ResetUsb(int index) |
||
267 | { |
||
268 | SD4.ResetUsb(index); |
||
269 | } |
||
270 | |||
271 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
272 | static void SD4_ResetUsbByDevice(uint deviceId) |
||
273 | { |
||
274 | SD4.ResetUsb(deviceId); |
||
275 | } |
||
276 | |||
277 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
278 | static void SD4_Write(uint deviceId, int address, ushort data) |
||
279 | { |
||
280 | SD4.Write(deviceId, address, data); |
||
281 | } |
||
282 | |||
283 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
284 | static int SD4_WriteArray(uint deviceId, int address, IntPtr data, int len) |
||
285 | { |
||
286 | ushort[] udata = new ushort[len]; |
||
287 | Marshal.Copy( data, (int[])(object) udata,0, len); |
||
288 | return SD4.Write(deviceId, address, udata); |
||
289 | } |
||
290 | /* |
||
291 | public sealed class DeviceChangedEventArgs : EventArgs |
||
292 | { |
||
293 | public DeviceChangedEventArgs(); |
||
294 | |||
295 | public uint DeviceId { get; set; } |
||
296 | public int DeviceIndex { get; set; } |
||
297 | } |
||
298 | public sealed class FirmwareStatusEventArgs : EventArgs |
||
299 | { |
||
300 | public FirmwareStatusEventArgs(); |
||
301 | |||
302 | public uint DeviceId { get; set; } |
||
303 | public int DeviceIndex { get; set; } |
||
304 | public string FirmwareStatus { get; set; } |
||
305 | } |
||
306 | */ |
||
307 | |||
308 | |||
309 | |||
310 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
311 | static int NE_Ptr2String(IntPtr c, int len) |
||
312 | { |
||
313 | string d = Marshal.PtrToStringAnsi(c); |
||
314 | Console.WriteLine(d); |
||
315 | return 0; |
||
316 | } |
||
317 | |||
318 | |||
319 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
320 | static int NE_Ptr2ByteArray(IntPtr c, int len) |
||
321 | { |
||
322 | System.Console.WriteLine(len); |
||
323 | byte[] b = new byte[len]; |
||
324 | Marshal.Copy(c, b, 0, len); |
||
325 | foreach (byte element in b) |
||
326 | { |
||
327 | System.Console.WriteLine(element); |
||
328 | } |
||
329 | System.Console.WriteLine(); |
||
330 | return b.Length; |
||
331 | } |
||
332 | |||
333 | //----------------------------------- |
||
334 | // http://stackoverflow.com/questions/14934016/unmanaged-exports-with-arrays |
||
335 | // https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net/blob/master/Visual%20Studio%20Project%20Template%20C%23/PluginInfrastructure/UnmanagedExports.cs |
||
336 | static IntPtr _unmanagedMemory = IntPtr.Zero; |
||
337 | static int _unmanagedMemoryLength = 0; |
||
338 | |||
339 | static void SD4_AllocateMemory(int len) |
||
340 | { |
||
341 | |||
342 | if (_unmanagedMemoryLength < len) |
||
343 | { |
||
344 | if (_unmanagedMemory != IntPtr.Zero) |
||
345 | Marshal.FreeHGlobal(_unmanagedMemory); |
||
346 | _unmanagedMemory = Marshal.AllocHGlobal(len); |
||
347 | _unmanagedMemoryLength = len; |
||
348 | |||
349 | } |
||
350 | } |
||
351 | |||
352 | |||
353 | static ushort[] SD4_UShortBuffer = null; |
||
354 | static ushort[] SD4_AllocateUshortBuffer(int len) { |
||
355 | if (SD4_UShortBuffer == null || SD4_UShortBuffer.Length < len) { |
||
356 | Array.Resize(ref SD4_UShortBuffer, len); |
||
357 | } |
||
358 | return SD4_UShortBuffer; |
||
359 | } |
||
360 | |||
361 | static byte[] SD4_ByteBuffer = null; |
||
362 | static byte[] SD4_AllocateByteBuffer(int len) |
||
363 | { |
||
364 | if (SD4_ByteBuffer == null || SD4_ByteBuffer.Length < len) |
||
365 | { |
||
366 | Array.Resize(ref SD4_ByteBuffer, len); |
||
367 | } |
||
368 | return SD4_ByteBuffer; |
||
369 | } |
||
370 | |||
371 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
372 | static IntPtr NE_ByteArray2Ptr(ref IntPtr unmanagedArray, out int length) |
||
373 | { |
||
374 | var valueList = new[] { 1, 2, 3, 3, 2, 1, 12, 11, 10 }; |
||
375 | System.Console.WriteLine("NE_ByteArray2Ptr"); |
||
376 | |||
377 | foreach (var element in valueList) |
||
378 | { |
||
379 | System.Console.WriteLine(element); |
||
380 | } |
||
381 | System.Console.WriteLine(); |
||
382 | |||
383 | length = valueList.Length; |
||
384 | int len = valueList.Length * Marshal.SizeOf(typeof(int)); |
||
385 | SD4_AllocateMemory(len); |
||
386 | Marshal.Copy(valueList, 0, _unmanagedMemory, length); |
||
387 | // |
||
388 | //unmanagedArray = Marshal.AllocHGlobal(len); |
||
389 | //Marshal.Copy(valueList, 0, unmanagedArray, length); |
||
390 | return _unmanagedMemory; |
||
391 | } |
||
392 | |||
393 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
394 | static int NE_ByteArray2Ptr1(ref IntPtr dest, ref int len) |
||
395 | //static int NE_ByteArray2Ptr( ref byte[] src, ref int len) |
||
396 | { |
||
397 | len = 5; |
||
398 | //Marshal.AllocHGlobal(len); |
||
399 | byte[] src = new byte[len]; |
||
400 | for (int i = 0; i < len; i++) |
||
401 | { |
||
402 | src[i] = Convert.ToByte(i); |
||
403 | //Marshal.WriteByte(dest, i, src[i] ); |
||
404 | } |
||
405 | System.Console.WriteLine("NE_ByteArray2Ptr"); |
||
406 | System.Console.WriteLine(len); |
||
407 | foreach (byte element in src) |
||
408 | { |
||
409 | System.Console.WriteLine(element); |
||
410 | } |
||
411 | System.Console.WriteLine(); |
||
412 | |||
413 | // Marshal.Copy(src, 0, dest, len); |
||
414 | |||
415 | return src.Length; |
||
416 | } |
||
417 | |||
418 | |||
419 | |||
420 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
421 | static int NE_String2Ptr(ref IntPtr c) |
||
422 | { |
||
423 | |||
424 | |||
425 | |||
426 | // Copy the array to unmanaged memory. |
||
427 | //c = Marshal.StringToHGlobalAnsi(d); |
||
428 | string inputStr = Marshal.PtrToStringAnsi(c); |
||
429 | Console.ForegroundColor = ConsoleColor.Green; |
||
430 | Console.WriteLine(inputStr); |
||
431 | |||
432 | |||
433 | string outputStr = "NE_String2Ptr"; |
||
434 | Console.ForegroundColor = ConsoleColor.Red; |
||
435 | Console.WriteLine(outputStr); |
||
436 | Console.ForegroundColor = ConsoleColor.White; |
||
437 | |||
438 | // http://stackoverflow.com/questions/21378132/export-managed-c-sharp-function-to-return-changed-char-parameter-to-unmanaged-c |
||
439 | byte[] outputBytes = Encoding.Default.GetBytes(outputStr); |
||
440 | //problemi Marshal.Copy(outputBytes, 0, c, outputBytes.Length); |
||
441 | |||
442 | |||
443 | return outputStr.Length; |
||
444 | } |
||
445 | |||
446 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
447 | static IntPtr NE_String() |
||
448 | { |
||
449 | string d = "NE_String"; |
||
450 | |||
451 | |||
452 | // Copy the array to unmanaged memory. |
||
453 | //c = Marshal.StringToHGlobalAnsi(d); |
||
454 | |||
455 | Console.ForegroundColor = ConsoleColor.Green; |
||
456 | Console.WriteLine(d); |
||
457 | Console.ForegroundColor = ConsoleColor.White; |
||
458 | |||
459 | return Marshal.StringToHGlobalAnsi(d); |
||
460 | } |
||
461 | |||
462 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
463 | static IntPtr NE_UInt(ref int len) |
||
464 | { |
||
465 | len = 10; |
||
466 | uint[] b = new uint[len]; |
||
467 | for (uint i = 0; i < len; i++) |
||
468 | { |
||
469 | b[i] = i; |
||
470 | } |
||
471 | //PrintBytes(b); |
||
472 | IntPtr p = Marshal.AllocHGlobal(len * 4); |
||
473 | Marshal.Copy((int[])(object)b, 0, p, len); |
||
474 | return p; |
||
475 | } |
||
476 | |||
477 | |||
478 | [DllExport(CallingConvention = CallingConvention.Cdecl)] |
||
479 | static void NE_Rainbow() |
||
480 | { |
||
481 | Console.ForegroundColor = ConsoleColor.Red; |
||
482 | Console.Write('R'); |
||
483 | Console.ForegroundColor = ConsoleColor.DarkRed; |
||
484 | Console.Write('a'); |
||
485 | Console.ForegroundColor = ConsoleColor.Yellow; |
||
486 | Console.Write('i'); |
||
487 | Console.ForegroundColor = ConsoleColor.Green; |
||
488 | Console.Write('n'); |
||
489 | Console.ForegroundColor = ConsoleColor.Cyan; |
||
490 | Console.Write('b'); |
||
491 | Console.ForegroundColor = ConsoleColor.Blue; |
||
492 | Console.Write('o'); |
||
493 | Console.ForegroundColor = ConsoleColor.Magenta; |
||
494 | Console.Write('w'); |
||
495 | |||
496 | Console.ResetColor(); |
||
497 | Console.WriteLine(); |
||
498 | } |
||
499 | } |
||
500 | } |