From d14c47c3f86a7d263df279db50c72e91f1ee35b0 Mon Sep 17 00:00:00 2001 From: "Denis Kuzmin (reg)" Date: Sun, 25 Dec 2016 16:41:17 +0300 Subject: [PATCH] Started support of exported variables via IExVar #7 * IProvider: * `IExVar ExVar` - Access to exported variables. * `IProviderSvc Svc` - Additional services. ``` l.ExVar.get("ADDR_SPEC"); // 0x00001CE8 l.ExVar.getField(typeof(UInt32).NativeSize(), "ADDR_SPEC"); // Native.Core.Field via raw size l.Svc.native("lpProcName"); // Raw access via NativeData & Native.Core ! ``` --- Conari/Conari.csproj | 3 + Conari/Core/ExVar.cs | 121 ++++++++++++++++++++++++++++++++++++ Conari/Core/IExVar.cs | 78 +++++++++++++++++++++++ Conari/Core/IProvider.cs | 10 +++ Conari/Core/IProviderSvc.cs | 46 ++++++++++++++ Conari/Core/Provider.cs | 59 ++++++++++++++++++ Readme.md | 12 +++- 7 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 Conari/Core/ExVar.cs create mode 100644 Conari/Core/IExVar.cs create mode 100644 Conari/Core/IProviderSvc.cs diff --git a/Conari/Conari.csproj b/Conari/Conari.csproj index 200cc3a..3e6fb68 100644 --- a/Conari/Conari.csproj +++ b/Conari/Conari.csproj @@ -44,7 +44,10 @@ + + + diff --git a/Conari/Core/ExVar.cs b/Conari/Core/ExVar.cs new file mode 100644 index 0000000..9c09c07 --- /dev/null +++ b/Conari/Core/ExVar.cs @@ -0,0 +1,121 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Denis Kuzmin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. +*/ + +using System; +using net.r_eg.Conari.Native; + +namespace net.r_eg.Conari.Core +{ + public class ExVar: IExVar + { + private IProvider provider; + + /// + /// Gets value from exported Variable. Full name is required. + /// + /// The type of variable. + /// The full name of exported variable. + /// The value from exported variable. + public T getVar(string lpProcName) + { + return getField(lpProcName).value; + } + + /// + /// Gets value from exported Variable. + /// The main prefix will affects on this result. + /// + /// The type of variable. + /// The name of exported variable. + /// The value from exported variable. + public T get(string variable) + { + return getVar(provider.procName(variable)); + } + + /// + /// Get field with native data from export table. + /// Uses type for information about data. + /// + /// To consider it as this type. + /// The name of record. + /// + public Native.Core.Field getField(Type type, string name) + { + return getField( + name, + provider + .Svc + .native(name) + .t(type, name) + ); + } + + /// + /// Alias to `getField(Type type, string name)` + /// + /// Get field with native data from export table. + /// Uses type for information about data. + /// + /// To consider it as T type. + /// The name of record. + /// + public Native.Core.Field getField(string name) + { + return getField(typeof(T), name); + } + + /// + /// Get field with native data from export table. + /// Uses size of unspecified unmanaged type in bytes. + /// To calculate it from managed types, see: `NativeData.SizeOf` + /// + /// The size of raw-data in bytes. + /// The name of record. + /// + public Native.Core.Field getField(int size, string name) + { + return getField( + name, + provider + .Svc + .native(name) + .t(size, name) + ); + } + + public ExVar(IProvider p) + { + if(p == null) { + throw new ArgumentException("Provider cannot be null."); + } + provider = p; + } + + protected Native.Core.Field getField(string name, NativeData n) + { + return n.Raw.Type.getFieldByName(name); + } + } +} diff --git a/Conari/Core/IExVar.cs b/Conari/Core/IExVar.cs new file mode 100644 index 0000000..64d0d7e --- /dev/null +++ b/Conari/Core/IExVar.cs @@ -0,0 +1,78 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Denis Kuzmin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. +*/ + +using System; + +namespace net.r_eg.Conari.Core +{ + public interface IExVar + { + /// + /// Gets value from exported Variable. Full name is required. + /// + /// The type of variable. + /// The full name of exported variable. + /// The value from exported variable. + T getVar(string lpProcName); + + /// + /// Gets value from exported Variable. + /// The main prefix will affects on this result. + /// + /// The type of variable. + /// The name of exported variable. + /// The value from exported variable. + T get(string variable); + + /// + /// Get field with native data from export table. + /// Uses type for information about data. + /// + /// To consider it as this type. + /// The name of record. + /// + Native.Core.Field getField(Type type, string name); + + /// + /// Alias to `getField(Type type, string name)` + /// + /// Get field with native data from export table. + /// Uses type for information about data. + /// + /// To consider it as T type. + /// The name of record. + /// + Native.Core.Field getField(string name); + + /// + /// Get field with native data from export table. + /// Uses size of unspecified unmanaged type in bytes. + /// To calculate it from managed types, see: `NativeData.SizeOf` + /// + /// The size of raw-data in bytes. + /// The name of record. + /// + Native.Core.Field getField(int size, string name); + } +} diff --git a/Conari/Core/IProvider.cs b/Conari/Core/IProvider.cs index feee0f9..d1cdb7f 100644 --- a/Conari/Core/IProvider.cs +++ b/Conari/Core/IProvider.cs @@ -59,6 +59,16 @@ public interface IProvider: IBinder, IMem /// bool Mangling { get; set; } + /// + /// Access to exported variables. + /// + IExVar ExVar { get; } + + /// + /// Additional services. + /// + IProviderSvc Svc { get; } + /// /// Returns full lpProcName with main prefix etc. /// diff --git a/Conari/Core/IProviderSvc.cs b/Conari/Core/IProviderSvc.cs new file mode 100644 index 0000000..82db44e --- /dev/null +++ b/Conari/Core/IProviderSvc.cs @@ -0,0 +1,46 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Denis Kuzmin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. +*/ + +using System; +using net.r_eg.Conari.Native; + +namespace net.r_eg.Conari.Core +{ + public interface IProviderSvc + { + /// + /// Retrieves the address of an exported function or variable. + /// + /// The name of function or variable, or the function's ordinal value. + /// The address if found. + IntPtr getProcAddr(string lpProcName); + + /// + /// Prepare NativeData for active provider. + /// + /// The name of function or variable, or the function's ordinal value. + /// + NativeData native(string lpProcName); + } +} diff --git a/Conari/Core/Provider.cs b/Conari/Core/Provider.cs index c1ba267..c974c59 100644 --- a/Conari/Core/Provider.cs +++ b/Conari/Core/Provider.cs @@ -30,6 +30,7 @@ using System.Runtime.InteropServices; using net.r_eg.Conari.Exceptions; using net.r_eg.Conari.Log; +using net.r_eg.Conari.Native; using net.r_eg.Conari.Types; using net.r_eg.Conari.Types.Methods; using net.r_eg.Conari.WinAPI; @@ -95,6 +96,64 @@ public bool Mangling set; } + /// + /// Access to exported variables. + /// + public IExVar ExVar + { + get { + if(exvar == null) { + exvar = new ExVar(this); + } + return exvar; + } + } + protected IExVar exvar; + + /// + /// Additional services. + /// + public IProviderSvc Svc + { + get { + if(svc == null) { + svc = new ProviderSvc(this); + } + return svc; + } + } + private IProviderSvc svc; + + protected sealed class ProviderSvc: IProviderSvc + { + private Provider provider; + + /// + /// Retrieves the address of an exported function or variable. + /// + /// The name of function or variable, or the function's ordinal value. + /// The address if found. + public IntPtr getProcAddr(string lpProcName) + { + return provider.getProcAddress(lpProcName); + } + + /// + /// Prepare NativeData for active provider. + /// + /// The name of function or variable, or the function's ordinal value. + /// + public NativeData native(string lpProcName) + { + return getProcAddr(lpProcName).Native(); + } + + public ProviderSvc(Provider p) + { + provider = p; + } + } + /// /// Binds the exported Function. Full name is required. /// diff --git a/Readme.md b/Readme.md index 881b28f..82a3bf7 100644 --- a/Readme.md +++ b/Readme.md @@ -100,7 +100,7 @@ dynamic idd = (new NativeData(data)) ```csharp IntPtr ptr ... -Raw mt = ptr.Native() // v1.3+ / NativeData._(ptr) +Raw mt = ptr.Native() .align(2, "a", "b") .t("name") .Raw; @@ -132,6 +132,16 @@ using(var l = new ConariL("Library.dll", CallingConvention.StdCall)) } ``` +**Exported Variables & Raw access:** + +```csharp +// v1.3+ +l.ExVar.get("ADDR_SPEC"); // 0x00001CE8 +l.ExVar.getField(typeof(UInt32).NativeSize(), "ADDR_SPEC"); // Native.Core.Field via raw size +l.Svc.native("lpProcName"); // Raw access via NativeData & Native.Core ! +//v1.0+: Use Provider or ConariL frontend via your custom wrapper. +``` + **Additional types:** * BSTR, CharPtr, float_t, int_t, ptrdiff_t, size_t, uint_t, WCharPtr