Skip to content

Commit

Permalink
+kernel32 +user32 via new ConariX [DLR version]:
Browse files Browse the repository at this point in the history
~

```
dynamic user32 = new User32();

    user32.ShowWindow(0x000A0A28, 3);
    user32.MessageBoxA(0, "Conari in action", "Hello!", 0);
```

```
dynamic kernel32 = new Kernel32();

    kernel32.GetModuleHandleA<IntPtr>("libcurl-x64");
    kernel32.GetModuleHandleW<IntPtr>((WCharPtr)ustr);
```

+Added tests
  • Loading branch information
3F committed Nov 29, 2019
1 parent 0773f18 commit 3be8617
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 4 deletions.
77 changes: 77 additions & 0 deletions Conari/Accessors/WinAPI/Kernel32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2019 Denis Kuzmin < [email protected] > GitHub/3F
* Copyright (c) Conari contributors: https://github.com/3F/Conari/graphs/contributors
*
* 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 System.Runtime.InteropServices;
using net.r_eg.Conari.Core;

namespace net.r_eg.Conari.Accessors.WinAPI
{
/// <summary>
/// kernel32 via Conari engine [DLR version]:
/// https://github.com/3F/Conari
/// https://docs.microsoft.com/en-us/windows/win32/api/
/// </summary>
public sealed class Kernel32: ConariX
{
private const CallingConvention __v_conv = CallingConvention.Winapi;
private const string __v_module = "kernel32";

public override CallingConvention Convention
{
get => __v_conv;
set => throw new NotSupportedException();
}

/// <summary>
/// Initialize kernel32 via Conari engine.
/// </summary>
/// <param name="cfg">Custom configuration. Module cannot be overridden.</param>
public Kernel32(IConfig cfg)
: base(DefConf(cfg), __v_conv, null)
{

}

/// <summary>
/// Initialize kernel32 via Conari engine.
/// </summary>
public Kernel32()
: this(DefConf())
{

}

private static IConfig DefConf(IConfig cfg = null)
{
if(cfg == null) {
cfg = new Config();
}

cfg.Module = __v_module;
return cfg;
}
}
}
77 changes: 77 additions & 0 deletions Conari/Accessors/WinAPI/User32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2019 Denis Kuzmin < [email protected] > GitHub/3F
* Copyright (c) Conari contributors: https://github.com/3F/Conari/graphs/contributors
*
* 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 System.Runtime.InteropServices;
using net.r_eg.Conari.Core;

namespace net.r_eg.Conari.Accessors.WinAPI
{
/// <summary>
/// user32 via Conari engine [DLR version]:
/// https://github.com/3F/Conari
/// https://docs.microsoft.com/en-us/windows/win32/api/winuser/
/// </summary>
public sealed class User32: ConariX
{
private const CallingConvention __v_conv = CallingConvention.Winapi;
private const string __v_module = "user32";

public override CallingConvention Convention
{
get => __v_conv;
set => throw new NotSupportedException();
}

/// <summary>
/// Initialize user32 via Conari engine.
/// </summary>
/// <param name="cfg">Custom configuration. Module cannot be overridden.</param>
public User32(IConfig cfg)
: base(DefConf(cfg), __v_conv, null)
{

}

/// <summary>
/// Initialize user32 via Conari engine.
/// </summary>
public User32()
: this(DefConf())
{

}

private static IConfig DefConf(IConfig cfg = null)
{
if(cfg == null) {
cfg = new Config();
}

cfg.Module = __v_module;
return cfg;
}
}
}
18 changes: 18 additions & 0 deletions Conari/Conari.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@
.Raw;
```

🏄 Most powerful PInvoke and even most convenient use of WinAPI. Our recipe is simple: *Just use it!*

```
dynamic user32 = new User32();

user32.ShowWindow(0x000A0A28, 3);
user32.MessageBoxA(0, "Conari in action", "Hello!", 0);
```

```
dynamic kernel32 = new Kernel32();

kernel32.GetModuleHandleA&lt;IntPtr&gt;("libcurl-x64");
kernel32.GetModuleHandleW&lt;IntPtr&gt;((WCharPtr)ustr);
```

**Important note:** Conari does not provide anything from above. It will just generate and adapt everything at runtime. Specially for you!

🔖 Modern **.NET Core**

Conari is ready for .NET Core starting from 1.4.
Expand Down
82 changes: 82 additions & 0 deletions ConariTest/Accessors/WinAPI/Kernel32Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Runtime.InteropServices;
using net.r_eg.Conari.Accessors.WinAPI;
using net.r_eg.Conari.Core;
using Xunit;

namespace ConariTest.Accessors.WinAPI
{
public class Kernel32Test
{
[Fact]
public void ctorTest1()
{
using(dynamic kernel32 = new Kernel32())
{
Assert.True(kernel32.Library.IsActive);
Assert.NotEqual(IntPtr.Zero, kernel32.Library.handle);
Assert.False(kernel32.Library.isolated);

Assert.NotNull(kernel32.Library.resolved);
Assert.True(kernel32.Library.resolved.value);

string module = (string)kernel32.Library.module;
Assert.NotNull(module);
Assert.True(module.Contains("kernel32", StringComparison.OrdinalIgnoreCase));
}
}

[Fact]
public void ctorTest2()
{
using(dynamic kernel32 = new Kernel32(new Config()
{
Module = "CustomModule.dll"
}))
{
Assert.Equal(CallingConvention.Winapi, kernel32.Convention);

Assert.True(kernel32.Library.IsActive);
Assert.NotEqual(IntPtr.Zero, kernel32.Library.handle);
Assert.False(kernel32.Library.isolated);

Assert.NotNull(kernel32.Library.resolved);
Assert.True(kernel32.Library.resolved.value);

string module = (string)kernel32.Library.module;
Assert.NotNull(module);
Assert.True(module.Contains("kernel32", StringComparison.OrdinalIgnoreCase));
}
}

[Fact]
public void ctorTest3()
{
using(dynamic kernel32 = new Kernel32((IConfig)null))
{
Assert.True(kernel32.Library.IsActive);
Assert.NotEqual(IntPtr.Zero, kernel32.Library.handle);
Assert.False(kernel32.Library.isolated);

Assert.NotNull(kernel32.Library.resolved);
Assert.True(kernel32.Library.resolved.value);

string module = (string)kernel32.Library.module;
Assert.NotNull(module);
Assert.True(module.Contains("kernel32", StringComparison.OrdinalIgnoreCase));
}
}

[Fact]
public void convTest1()
{
Assert.Throws<NotSupportedException>(() =>
{
using(dynamic kernel32 = new Kernel32())
{
kernel32.Convention = CallingConvention.Cdecl;
}
});
}
}
}
82 changes: 82 additions & 0 deletions ConariTest/Accessors/WinAPI/User32Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Runtime.InteropServices;
using net.r_eg.Conari.Accessors.WinAPI;
using net.r_eg.Conari.Core;
using Xunit;

namespace ConariTest.Accessors.WinAPI
{
public class User32Test
{
[Fact]
public void ctorTest1()
{
using(dynamic user32 = new User32())
{
Assert.True(user32.Library.IsActive);
Assert.NotEqual(IntPtr.Zero, user32.Library.handle);
Assert.False(user32.Library.isolated);

Assert.NotNull(user32.Library.resolved);
Assert.True(user32.Library.resolved.value);

string module = (string)user32.Library.module;
Assert.NotNull(module);
Assert.True(module.Contains("user32", StringComparison.OrdinalIgnoreCase));
}
}

[Fact]
public void ctorTest2()
{
using(dynamic user32 = new User32(new Config()
{
Module = "CustomModule.dll"
}))
{
Assert.Equal(CallingConvention.Winapi, user32.Convention);

Assert.True(user32.Library.IsActive);
Assert.NotEqual(IntPtr.Zero, user32.Library.handle);
Assert.False(user32.Library.isolated);

Assert.NotNull(user32.Library.resolved);
Assert.True(user32.Library.resolved.value);

string module = (string)user32.Library.module;
Assert.NotNull(module);
Assert.True(module.Contains("user32", StringComparison.OrdinalIgnoreCase));
}
}

[Fact]
public void ctorTest3()
{
using(dynamic user32 = new User32((IConfig)null))
{
Assert.True(user32.Library.IsActive);
Assert.NotEqual(IntPtr.Zero, user32.Library.handle);
Assert.False(user32.Library.isolated);

Assert.NotNull(user32.Library.resolved);
Assert.True(user32.Library.resolved.value);

string module = (string)user32.Library.module;
Assert.NotNull(module);
Assert.True(module.Contains("user32", StringComparison.OrdinalIgnoreCase));
}
}

[Fact]
public void convTest1()
{
Assert.Throws<NotSupportedException>(() =>
{
using(dynamic user32 = new User32())
{
user32.Convention = CallingConvention.Cdecl;
}
});
}
}
}
26 changes: 22 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

🧬 Conari engine represents powerful platform for work with unmanaged memory, pe-modules, related PInvoke features, and more for: Libraries, Executable Modules, enjoying of the unmanaged native C/C++ in .NET world, and other raw binary data. Even accessing to complex types like structures without their declaration at all.

[![Build status](https://ci.appveyor.com/api/projects/status/qc1d3ofsso8fd67t/branch/master?svg=true)](https://ci.appveyor.com/project/3Fs/conari/branch/master)
[![Build status](https://ci.appveyor.com/api/projects/status/xbb5imyn9lr8dxbb/branch/master?svg=true)](https://ci.appveyor.com/project/3Fs/conari-wkygr/branch/master)
[![release-src](https://img.shields.io/github/release/3F/Conari.svg)](https://github.com/3F/Conari/releases/latest)
[![License](https://img.shields.io/badge/License-MIT-74A5C2.svg)](https://github.com/3F/Conari/blob/master/LICENSE)
[![NuGet package](https://img.shields.io/nuget/v/Conari.svg)](https://www.nuget.org/packages/Conari/)
[![Tests](https://img.shields.io/appveyor/tests/3Fs/conari/master.svg)](https://ci.appveyor.com/project/3Fs/conari/build/tests)
[![Tests](https://img.shields.io/appveyor/tests/3Fs/conari-wkygr/master.svg)](https://ci.appveyor.com/project/3Fs/conari-wkygr/build/tests)

[![Build history](https://buildstats.info/appveyor/chart/3Fs/conari?buildCount=10&includeBuildsFromPullRequest=true&showStats=true)](https://ci.appveyor.com/project/3Fs/conari/history)
[![Build history](https://buildstats.info/appveyor/chart/3Fs/conari-wkygr?buildCount=15&includeBuildsFromPullRequest=true&showStats=true)](https://ci.appveyor.com/project/3Fs/conari-wkygr/history)

> 1:[ ***[Quick start](https://github.com/3F/Conari/wiki/Quick-start)*** ] 2:[ [Basic examples for C++ and C#](https://www.youtube.com/watch?v=9Hyg3_WE9Ks) ] 3:[ [Complex types and Strings](https://www.youtube.com/watch?v=QXMj9-8XJnY) ]
> -> { **[Wiki](https://github.com/3F/Conari/wiki)** }
Expand Down Expand Up @@ -61,6 +61,24 @@ ptr.Native().align<int>(2, "x", "y")
.Raw;
```

🏄 Most powerful PInvoke and even most convenient use of WinAPI. Our recipe is simple: *Just use it!*

```csharp
dynamic user32 = new User32();

user32.ShowWindow(0x000A0A28, 3);
user32.MessageBoxA(0, "Conari in action", "Hello!", 0);
```

```csharp
dynamic kernel32 = new Kernel32();

kernel32.GetModuleHandleA<IntPtr>("libcurl-x64");
kernel32.GetModuleHandleW<IntPtr>((WCharPtr)ustr);
```

**Important note:** Conari does not provide anything from above. It will just generate and adapt everything at runtime. Specially for you!

🔖 Modern **.NET Core**

Conari is ready for .NET Core starting from 1.4.
Expand Down Expand Up @@ -275,4 +293,4 @@ have fun!
* NuGet: [![NuGet package](https://img.shields.io/nuget/v/Conari.svg)](https://www.nuget.org/packages/Conari/)
* [GetNuTool](https://github.com/3F/GetNuTool): `msbuild gnt.core /p:ngpackages="Conari"` or **[gnt](https://3f.github.io/GetNuTool/releases/latest/gnt/)** /p:ngpackages="Conari"
* [GitHub Releases](https://github.com/3F/Conari/releases) [ [latest](https://github.com/3F/Conari/releases/latest) ]
* CI builds: [`CI /artifacts`](https://ci.appveyor.com/project/3Fs/conari/history) or find `🎲 CI build` on [GitHub Releases](https://github.com/3F/Conari/releases) page.
* CI builds: [`CI /artifacts`](https://ci.appveyor.com/project/3Fs/conari-wkygr/history) ( [old CI](https://ci.appveyor.com/project/3Fs/conari/history) ) or find `🎲 CI build` on [GitHub Releases](https://github.com/3F/Conari/releases) page.

0 comments on commit 3be8617

Please sign in to comment.