forked from florentbr/SeleniumBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOMExt.cs
More file actions
52 lines (46 loc) · 2.22 KB
/
COMExt.cs
File metadata and controls
52 lines (46 loc) · 2.22 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
using Selenium.Core;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Selenium.Internal {
class COMExt {
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int CallBack([Out]out object result, ref object argument1, ref object argument2);
public static object WaitUntilProc(object function, object argument1, object argument2, int timeout) {
DateTime endtime = DateTime.UtcNow.AddMilliseconds(timeout);
try {
if (function is int) {
//handles AddressOf
var ptr = new IntPtr((int)function);
CallBack callback0 = (CallBack)Marshal.GetDelegateForFunctionPointer(ptr, typeof(CallBack));
while (true) {
object result = null;
callback0(out result, ref argument1, ref argument2);
if (result != null && !false.Equals(result))
return result;
if (DateTime.UtcNow > endtime)
throw new Errors.TimeoutError(timeout);
SysWaiter.Wait();
}
} else if (Marshal.IsComObject(function)) {
Type t = function.GetType();
object[] args = new object[] { argument1, argument2 };
while (true) {
object result = t.InvokeMember(string.Empty, BindingFlags.InvokeMethod, null, function, args);
if (result != null && !false.Equals(result))
return result;
if (DateTime.UtcNow > endtime)
throw new Errors.TimeoutError(timeout);
SysWaiter.Wait();
}
} else {
throw new Errors.ArgumentError("Invalid argument at position 0. A function is expected.");
}
} catch (ArgumentException) {
throw new Errors.ArgumentError("The procedure has an invalide signature.");
} catch (COMException ex) {
throw new SeleniumError(ex.Message);
}
}
}
}