diff --git a/Conari/Conari.csproj b/Conari/Conari.csproj index dc25757..7332676 100644 --- a/Conari/Conari.csproj +++ b/Conari/Conari.csproj @@ -87,6 +87,7 @@ + diff --git a/Conari/Core/IBinder.cs b/Conari/Core/IBinder.cs index 5343fc5..afb4809 100644 --- a/Conari/Core/IBinder.cs +++ b/Conari/Core/IBinder.cs @@ -22,11 +22,15 @@ * THE SOFTWARE. */ +using System; using System.Reflection; using System.Runtime.InteropServices; +using net.r_eg.Conari.Types.Methods; namespace net.r_eg.Conari.Core { + using Method = Method; + public interface IBinder { /// @@ -37,6 +41,14 @@ public interface IBinder /// Delegate of exported function. T bindFunc(string lpProcName) where T : class; + /// + /// Alias `bindFunc<Action>(string lpProcName)` + /// Binds the exported function. + /// + /// The full name of exported function. + /// Delegate of exported function. + Action bindFunc(string lpProcName); + /// /// Binds the exported C API Function. /// @@ -46,16 +58,24 @@ public interface IBinder T bind(string func) where T : class; /// - /// Binds the exported Function via MethodInfo. + /// Alias `bind<Action>(string func)` + /// Binds the exported C API Function. /// - /// Prepared signature with valid function name. - /// Add prefix to function name from IProvider.Prefix if true. - /// - /// Complete information to: - /// * create delegates ~ `dyn.CreateDelegate(type.declaringType) as T` - /// * or to invoke methods ~ `dyn.Invoke(null, mParams)` - /// - TDyn bind(MethodInfo mi, bool prefix = false); + /// The name of exported C API function. + /// Delegate of exported function. + Action bind(string func); + + ///// + ///// Binds the exported Function via MethodInfo. + ///// + ///// Prepared signature with valid function name. + ///// Add prefix to function name from IProvider.Prefix if true. + ///// + ///// Complete information to: + ///// * create delegates ~ `dyn.CreateDelegate(type.declaringType) as T` + ///// * or to invoke methods ~ `dyn.Invoke(null, mParams)` + ///// + //TDyn bind(MethodInfo mi, bool prefix = false); /// /// Binds the exported Function via MethodInfo and an specific name. @@ -78,5 +98,45 @@ public interface IBinder /// How it should be called. It overrides only for current method. /// Complete information to create delegates or to invoke methods. TDyn bind(MethodInfo mi, string name, CallingConvention conv); + + /// + /// Alias `bindFunc<object>(string lpProcName, Type ret, params Type[] args)` + /// Binds the exported function. + /// + /// The full name of exported function. + /// The type of return value. + /// The type of arguments. + /// Delegate of exported function. + Method bindFunc(string lpProcName, Type ret, params Type[] args); + + /// + /// Binds the exported function. + /// + /// The return type for new Delegate should be as T type. + /// The full name of exported function. + /// The type of return value. + /// The type of arguments. + /// Delegate of exported function. + Method bindFunc(string lpProcName, Type ret, params Type[] args); + + /// + /// Alias `bind<object>(string func, Type ret, params Type[] args)` + /// Binds the exported C API Function. + /// + /// The name of exported C API function. + /// The type of return value. + /// The type of arguments. + /// Delegate of exported function. + Method bind(string func, Type ret, params Type[] args); + + /// + /// Binds the exported C API Function. + /// + /// The return type for new Delegate should be as T type. + /// The name of exported C API function. + /// The type of return value. + /// The type of arguments. + /// Delegate of exported function. + Method bind(string func, Type ret, params Type[] args); } } diff --git a/Conari/Core/Provider.cs b/Conari/Core/Provider.cs index d1550fa..61324df 100644 --- a/Conari/Core/Provider.cs +++ b/Conari/Core/Provider.cs @@ -31,11 +31,13 @@ using net.r_eg.Conari.Exceptions; using net.r_eg.Conari.Log; using net.r_eg.Conari.Types; +using net.r_eg.Conari.Types.Methods; using net.r_eg.Conari.WinAPI; namespace net.r_eg.Conari.Core { using CMangling = Mangling.C; + using Method = Method; public abstract class Provider: Loader, ILoader, IProvider { @@ -104,6 +106,17 @@ public T bindFunc(string lpProcName) where T : class return getDelegate(lpProcName); } + /// + /// Alias `bindFunc<Action>(string lpProcName)` + /// Binds the exported function. + /// + /// The full name of exported function. + /// Delegate of exported function. + public Action bindFunc(string lpProcName) + { + return bindFunc(lpProcName); + } + /// /// Binds the exported C API Function. /// @@ -116,17 +129,28 @@ public T bind(string func) where T : class } /// - /// Binds the exported Function via MethodInfo. + /// Alias `bind<Action>(string func)` + /// Binds the exported C API Function. /// - /// Prepared signature. - /// Add prefix to function name from IProvider.Prefix if true. - /// Complete information to create delegates or to invoke methods. - public TDyn bind(MethodInfo mi, bool prefix = false) + /// The name of exported C API function. + /// Delegate of exported function. + public Action bind(string func) { - string func = prefix ? funcName(mi.Name) : mi.Name; - return wire(mi, func); + return bind(func); } + ///// + ///// Binds the exported Function via MethodInfo. + ///// + ///// Prepared signature. + ///// Add prefix to function name from IProvider.Prefix if true. + ///// Complete information to create delegates or to invoke methods. + //public TDyn bind(MethodInfo mi, bool prefix = false) + //{ + // string func = prefix ? funcName(mi.Name) : mi.Name; + // return wire(mi, func); + //} + /// /// Binds the exported Function via MethodInfo and an specific name. /// @@ -150,6 +174,63 @@ public TDyn bind(MethodInfo mi, string name, CallingConvention conv) return wire(mi, name, conv); } + /// + /// Alias `bindFunc<object>(string lpProcName, Type ret, params Type[] args)` + /// Binds the exported function. + /// + /// The full name of exported function. + /// The type of return value. + /// The type of arguments. + /// Delegate of exported function. + public Method bindFunc(string lpProcName, Type ret, params Type[] args) + { + return bindFunc(lpProcName, ret, args); + } + + /// + /// Binds the exported function. + /// + /// The return type for new Delegate should be as T type. + /// The full name of exported function. + /// The type of return value. + /// The type of arguments. + /// Delegate of exported function. + public Method bindFunc(string lpProcName, Type ret, params Type[] args) + { + MethodInfo mi = Dynamic.GetMethodInfo(lpProcName, ret, args); + TDyn dyn = bind(mi, lpProcName, Convention); + + return delegate (object[] _args) { + return (T)dyn.dynamic.Invoke(null, _args); + }; + } + + /// + /// Alias `bind<object>(string func, Type ret, params Type[] args)` + /// Binds the exported C API Function. + /// + /// The name of exported C API function. + /// The type of return value. + /// The type of arguments. + /// Delegate of exported function. + public Method bind(string func, Type ret, params Type[] args) + { + return bind(func, ret, args); + } + + /// + /// Binds the exported C API Function. + /// + /// The return type for new Delegate should be as T type. + /// The name of exported C API function. + /// The type of return value. + /// The type of arguments. + /// Delegate of exported function. + public Method bind(string func, Type ret, params Type[] args) + { + return bindFunc(funcName(func), ret, args); + } + /// /// Returns full name of exported function. /// diff --git a/Conari/Types/Methods/Method.cs b/Conari/Types/Methods/Method.cs new file mode 100644 index 0000000..f19938c --- /dev/null +++ b/Conari/Types/Methods/Method.cs @@ -0,0 +1,32 @@ +/* + * 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. +*/ + +namespace net.r_eg.Conari.Types.Methods +{ + /// The type of return value. + /// The type of arguments. + /// Argument list. + /// Return value. + public delegate TRes Method(params T[] args); +} diff --git a/ConariTest/BindingTest.cs b/ConariTest/BindingTest.cs index 8c434e3..74d673a 100644 --- a/ConariTest/BindingTest.cs +++ b/ConariTest/BindingTest.cs @@ -150,6 +150,91 @@ public void basicTest11() } } + [TestMethod] + public void basicTest12() + { + using(var l = new ConariL(UNLIB_DLL)) + { + Assert.AreEqual(7, l.bindFunc("get_VarSeven", typeof(int))()); + Assert.AreEqual(null, l.bind("set_VarSeven", typeof(void), typeof(int))(5)); + Assert.AreEqual(5, l.bind("get_VarSeven", typeof(int))()); + Assert.AreEqual(null ,l.bind("reset_VarSeven", null)()); + Assert.AreEqual(-1, (int)l.bind("get_VarSeven", typeof(int))()); + } + } + + [TestMethod] + public void basicTest13() + { + using(var l = new ConariL(UNLIB_DLL)) + { + Assert.AreEqual(7, l.bind>("get_VarSeven")()); + + l.bind>("set_VarSeven")(5); + Assert.AreEqual(5, l.bind("get_VarSeven", typeof(int))()); + + l.bind("reset_VarSeven")(); + Assert.AreEqual(-1, l.bind>("get_VarSeven")()); + } + } + + [TestMethod] + public void basicTest14() + { + using(var l = new ConariL(UNLIB_DLL)) + { + Assert.AreEqual(7, l.DLR.get_VarSeven()); + Assert.AreEqual(null, l.DLR.set_VarSeven(5)); + Assert.AreEqual(5, l.DLR.get_VarSeven()); + Assert.AreEqual(null ,l.DLR.reset_VarSeven()); + Assert.AreEqual(-1, l.DLR.get_VarSeven()); + } + } + + [TestMethod] + public void basicTest15() + { + using(var l = new ConariL(UNLIB_DLL)) + { + Assert.AreEqual(7, l.bind(Dynamic.GetMethodInfo(typeof(int)), "get_VarSeven") + .dynamic + .Invoke(null, null)); + + Assert.AreEqual(null, l.bind(Dynamic.GetMethodInfo(typeof(void), typeof(int)), "set_VarSeven") + .dynamic + .Invoke(null, new object[] { 5 })); + + Assert.AreEqual(5, l.bind(Dynamic.GetMethodInfo(typeof(int)), "get_VarSeven") + .dynamic + .Invoke(null, new object[0])); + + Assert.AreEqual(null, l.bind(Dynamic.GetMethodInfo(null), "reset_VarSeven") + .dynamic + .Invoke(null, null)); + + Assert.AreEqual(-1, l.bind(Dynamic.GetMethodInfo(typeof(int)), "get_VarSeven") + .dynamic + .Invoke(null, null)); + } + } + + [TestMethod] + public void namingTest1() + { + using(var l = new ConariL(UNLIB_DLL, "apiprefix_")) + { + Assert.AreEqual(4, l.DLR.GetMagicNum()); + + Assert.AreEqual(4, l.bind>("GetMagicNum")()); + Assert.AreEqual(-1, l.bindFunc>("GetMagicNum")()); + + Assert.AreEqual(-1, l.bind(Dynamic.GetMethodInfo(typeof(int)), "GetMagicNum").dynamic.Invoke(null, null)); + + Assert.AreEqual(-1, l.bindFunc("GetMagicNum", typeof(int))()); + Assert.AreEqual(4, l.bind("GetMagicNum", typeof(int))()); + } + } + [TestMethod] public void manglingTest1() { diff --git a/UnLib/UnLibAPI.cpp b/UnLib/UnLibAPI.cpp index 71ce45c..860ecf2 100644 --- a/UnLib/UnLibAPI.cpp +++ b/UnLib/UnLibAPI.cpp @@ -28,6 +28,33 @@ namespace NS_UNLIB_API_ return "Hello World !"; } + int _varSevenData = 7; + LIBAPI int get_VarSeven() + { + return _varSevenData; + } + + LIBAPI void set_VarSeven(int v) + { + _varSevenData = v; + } + + LIBAPI void reset_VarSeven() + { + _varSevenData = -1; + } + + /* naming */ + + LIBAPI int GetMagicNum() + { + return -1; + } + + LIBAPI int apiprefix_GetMagicNum() + { + return 4; + } /* mangling */ diff --git a/UnLib/UnLibAPI.h b/UnLib/UnLibAPI.h index eb74ae9..425c4b2 100644 --- a/UnLib/UnLibAPI.h +++ b/UnLib/UnLibAPI.h @@ -12,6 +12,14 @@ namespace NS_UNLIB_API_ LIBAPI bool get_False(); LIBAPI unsigned short int get_Seven(); LIBAPI const char* get_HelloWorld(); + LIBAPI int get_VarSeven(); + LIBAPI void set_VarSeven(int v); + LIBAPI void reset_VarSeven(); + + /* naming */ + + LIBAPI int GetMagicNum(); + LIBAPI int apiprefix_GetMagicNum(); /* mangling */