Skip to content

Commit

Permalink
Started support of exported variables via IExVar #7
Browse files Browse the repository at this point in the history
* IProvider:

    * `IExVar ExVar` - Access to exported variables.
    * `IProviderSvc Svc` - Additional services.

```
l.ExVar.get<UInt32>("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 !
```
  • Loading branch information
3F committed Dec 25, 2016
1 parent 80f08d9 commit d14c47c
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Conari/Conari.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\ExVar.cs" />
<Compile Include="Core\IExVar.cs" />
<Compile Include="Core\IDynamic.cs" />
<Compile Include="Core\IProviderSvc.cs" />
<Compile Include="Core\ProcAddressArgs.cs" />
<Compile Include="Core\DataArgs.cs" />
<Compile Include="Core\IMem.cs" />
Expand Down
121 changes: 121 additions & 0 deletions Conari/Core/ExVar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Denis Kuzmin <[email protected]>
*
* 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;

/// <summary>
/// Gets value from exported Variable. Full name is required.
/// </summary>
/// <typeparam name="T">The type of variable.</typeparam>
/// <param name="lpProcName">The full name of exported variable.</param>
/// <returns>The value from exported variable.</returns>
public T getVar<T>(string lpProcName)
{
return getField<T>(lpProcName).value;
}

/// <summary>
/// Gets value from exported Variable.
/// The main prefix will affects on this result.
/// </summary>
/// <typeparam name="T">The type of variable.</typeparam>
/// <param name="variable">The name of exported variable.</param>
/// <returns>The value from exported variable.</returns>
public T get<T>(string variable)
{
return getVar<T>(provider.procName(variable));
}

/// <summary>
/// Get field with native data from export table.
/// Uses type for information about data.
/// </summary>
/// <param name="type">To consider it as this type.</param>
/// <param name="name">The name of record.</param>
/// <returns></returns>
public Native.Core.Field getField(Type type, string name)
{
return getField(
name,
provider
.Svc
.native(name)
.t(type, name)
);
}

/// <summary>
/// Alias to `getField(Type type, string name)`
///
/// Get field with native data from export table.
/// Uses type for information about data.
/// </summary>
/// <typeparam name="T">To consider it as T type.</typeparam>
/// <param name="name">The name of record.</param>
/// <returns></returns>
public Native.Core.Field getField<T>(string name)
{
return getField(typeof(T), name);
}

/// <summary>
/// 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`
/// </summary>
/// <param name="size">The size of raw-data in bytes.</param>
/// <param name="name">The name of record.</param>
/// <returns></returns>
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);
}
}
}
78 changes: 78 additions & 0 deletions Conari/Core/IExVar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Denis Kuzmin <[email protected]>
*
* 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
{
/// <summary>
/// Gets value from exported Variable. Full name is required.
/// </summary>
/// <typeparam name="T">The type of variable.</typeparam>
/// <param name="lpProcName">The full name of exported variable.</param>
/// <returns>The value from exported variable.</returns>
T getVar<T>(string lpProcName);

/// <summary>
/// Gets value from exported Variable.
/// The main prefix will affects on this result.
/// </summary>
/// <typeparam name="T">The type of variable.</typeparam>
/// <param name="variable">The name of exported variable.</param>
/// <returns>The value from exported variable.</returns>
T get<T>(string variable);

/// <summary>
/// Get field with native data from export table.
/// Uses type for information about data.
/// </summary>
/// <param name="type">To consider it as this type.</param>
/// <param name="name">The name of record.</param>
/// <returns></returns>
Native.Core.Field getField(Type type, string name);

/// <summary>
/// Alias to `getField(Type type, string name)`
///
/// Get field with native data from export table.
/// Uses type for information about data.
/// </summary>
/// <typeparam name="T">To consider it as T type.</typeparam>
/// <param name="name">The name of record.</param>
/// <returns></returns>
Native.Core.Field getField<T>(string name);

/// <summary>
/// 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`
/// </summary>
/// <param name="size">The size of raw-data in bytes.</param>
/// <param name="name">The name of record.</param>
/// <returns></returns>
Native.Core.Field getField(int size, string name);
}
}
10 changes: 10 additions & 0 deletions Conari/Core/IProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public interface IProvider: IBinder, IMem
/// </summary>
bool Mangling { get; set; }

/// <summary>
/// Access to exported variables.
/// </summary>
IExVar ExVar { get; }

/// <summary>
/// Additional services.
/// </summary>
IProviderSvc Svc { get; }

/// <summary>
/// Returns full lpProcName with main prefix etc.
/// </summary>
Expand Down
46 changes: 46 additions & 0 deletions Conari/Core/IProviderSvc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Denis Kuzmin <[email protected]>
*
* 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
{
/// <summary>
/// Retrieves the address of an exported function or variable.
/// </summary>
/// <param name="lpProcName">The name of function or variable, or the function's ordinal value.</param>
/// <returns>The address if found.</returns>
IntPtr getProcAddr(string lpProcName);

/// <summary>
/// Prepare NativeData for active provider.
/// </summary>
/// <param name="lpProcName">The name of function or variable, or the function's ordinal value.</param>
/// <returns></returns>
NativeData native(string lpProcName);
}
}
59 changes: 59 additions & 0 deletions Conari/Core/Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +96,64 @@ public bool Mangling
set;
}

/// <summary>
/// Access to exported variables.
/// </summary>
public IExVar ExVar
{
get {
if(exvar == null) {
exvar = new ExVar(this);
}
return exvar;
}
}
protected IExVar exvar;

/// <summary>
/// Additional services.
/// </summary>
public IProviderSvc Svc
{
get {
if(svc == null) {
svc = new ProviderSvc(this);
}
return svc;
}
}
private IProviderSvc svc;

protected sealed class ProviderSvc: IProviderSvc
{
private Provider provider;

/// <summary>
/// Retrieves the address of an exported function or variable.
/// </summary>
/// <param name="lpProcName">The name of function or variable, or the function's ordinal value.</param>
/// <returns>The address if found.</returns>
public IntPtr getProcAddr(string lpProcName)
{
return provider.getProcAddress(lpProcName);
}

/// <summary>
/// Prepare NativeData for active provider.
/// </summary>
/// <param name="lpProcName">The name of function or variable, or the function's ordinal value.</param>
/// <returns></returns>
public NativeData native(string lpProcName)
{
return getProcAddr(lpProcName).Native();
}

public ProviderSvc(Provider p)
{
provider = p;
}
}

/// <summary>
/// Binds the exported Function. Full name is required.
/// </summary>
Expand Down
12 changes: 11 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(2, "a", "b")
.t<IntPtr>("name")
.Raw;
Expand Down Expand Up @@ -132,6 +132,16 @@ using(var l = new ConariL("Library.dll", CallingConvention.StdCall))
}
```

**Exported Variables & Raw access:**

```csharp
// v1.3+
l.ExVar.get<UInt32>("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
Expand Down

0 comments on commit d14c47c

Please sign in to comment.