diff options
author | Yi Zhang (CLR) <yzha@microsoft.com> | 2016-04-03 00:34:51 -0700 |
---|---|---|
committer | Yi Zhang (CLR) <yzha@microsoft.com> | 2016-04-04 21:58:23 -0700 |
commit | 0f911d1c6c5b96a3a6704bc8b126547dc781c9c4 (patch) | |
tree | 32d17e122eadc375d06a32df9c385a44a80cc19e /tests/src/Interop | |
parent | d7653d909ad252b9e9ffab9c6d2089767cb6981f (diff) | |
download | coreclr-0f911d1c6c5b96a3a6704bc8b126547dc781c9c4.tar.gz coreclr-0f911d1c6c5b96a3a6704bc8b126547dc781c9c4.tar.bz2 coreclr-0f911d1c6c5b96a3a6704bc8b126547dc781c9c4.zip |
Fix NativeCallableTest to use custom DLL instead of EnumWindows Win32 API
Diffstat (limited to 'tests/src/Interop')
-rw-r--r-- | tests/src/Interop/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/src/Interop/NativeCallable/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/src/Interop/NativeCallable/NativeCallableDll.cpp | 12 | ||||
-rw-r--r-- | tests/src/Interop/NativeCallable/NativeCallableTest.cs | 50 |
4 files changed, 51 insertions, 23 deletions
diff --git a/tests/src/Interop/CMakeLists.txt b/tests/src/Interop/CMakeLists.txt index f3478fd56f..c1c37d598b 100644 --- a/tests/src/Interop/CMakeLists.txt +++ b/tests/src/Interop/CMakeLists.txt @@ -1,5 +1,6 @@ include_directories(common) +add_subdirectory(NativeCallable) add_subdirectory(PrimitiveMarshalling/Bool) add_subdirectory(PrimitiveMarshalling/UIntPtr) add_subdirectory(ArrayMarshalling/BoolArray) @@ -10,4 +11,4 @@ add_subdirectory(RefCharArray) add_subdirectory(StringMarshalling/LPSTR) add_subdirectory(StringMarshalling/LPTSTR) add_subdirectory(MarshalAPI/FunctionPointer) -add_subdirectory(MarshalAPI/IUnknown)
\ No newline at end of file +add_subdirectory(MarshalAPI/IUnknown) diff --git a/tests/src/Interop/NativeCallable/CMakeLists.txt b/tests/src/Interop/NativeCallable/CMakeLists.txt new file mode 100644 index 0000000000..6363b2ecd3 --- /dev/null +++ b/tests/src/Interop/NativeCallable/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required (VERSION 2.6) +project (NativeCallableDll) +set(SOURCES NativeCallableDll.cpp ) + +# add the executable +add_library (NativeCallableDll SHARED ${SOURCES}) + +# add the install targets +install (TARGETS NativeCallableDll DESTINATION Interop/NativeCallable) diff --git a/tests/src/Interop/NativeCallable/NativeCallableDll.cpp b/tests/src/Interop/NativeCallable/NativeCallableDll.cpp new file mode 100644 index 0000000000..6ba63ff875 --- /dev/null +++ b/tests/src/Interop/NativeCallable/NativeCallableDll.cpp @@ -0,0 +1,12 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#include <xplatform.h> + +typedef int (*CALLBACKADDPROC)(int n); + +extern "C" DLL_EXPORT int WINAPI CallManagedAdd(CALLBACKADDPROC pCallbackAddProc, int n) +{ + return pCallbackAddProc(n); +} diff --git a/tests/src/Interop/NativeCallable/NativeCallableTest.cs b/tests/src/Interop/NativeCallable/NativeCallableTest.cs index 4fd390226d..0dcc184b4a 100644 --- a/tests/src/Interop/NativeCallable/NativeCallableTest.cs +++ b/tests/src/Interop/NativeCallable/NativeCallableTest.cs @@ -14,53 +14,60 @@ public class Program { public static class NativeMethods { - [DllImport("user32.dll")] - public static extern int EnumWindows(IntPtr enumProc, IntPtr lParam); + [DllImport("NativeCallableDll")] + public static extern int CallManagedAdd(IntPtr callbackProc, int n); } + private delegate int IntNativeMethodInvoker(); private delegate void NativeMethodInvoker(); - static EventWaitHandle waitHandle = new AutoResetEvent(false); public static int Main() { + int ret; //NegativeTest_NonBlittable(); - TestNativeCallableValid(); + ret = TestNativeCallableValid(); + if (ret != 100) + return ret; //NegativeTest_ViaDelegate(); //NegativeTest_ViaLdftn(); return 100; } - public static void TestNativeCallableValid() + public static int TestNativeCallableValid() { /* void TestNativeCallable() { .locals init ([0] native int ptr) IL_0000: nop - IL_0002: ldftn int32 CallbackMethod(native int,native int) + IL_0002: ldftn int32 CallbackMethod(int32) IL_0012: stloc.0 IL_0013: ldloc.0 - IL_0014: ldsfld native int [mscorlib]System.IntPtr::Zero - IL_0019: call bool NativeMethods::EnumWindows(native int, - native int) + IL_0014: ldc.i4 100 + IL_0019: call bool NativeMethods::CallNativeAdd(native int, int) IL_001e: pop IL_001f: ret } */ - DynamicMethod testNativeCallable = new DynamicMethod("TestNativeCallable", null, null, typeof(Program).Module); + DynamicMethod testNativeCallable = new DynamicMethod("TestNativeCallable", typeof(int), null, typeof(Program).Module); ILGenerator il = testNativeCallable.GetILGenerator(); il.DeclareLocal(typeof(IntPtr)); il.Emit(OpCodes.Nop); - il.Emit(OpCodes.Ldftn, typeof(Program).GetMethod("CallbackMethod")); + + // Get native function pointer of the callback + il.Emit(OpCodes.Ldftn, typeof(Program).GetMethod("ManagedAddCallback")); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ldloc_0); - il.Emit(OpCodes.Ldsfld, typeof(IntPtr).GetField("Zero")); - il.Emit(OpCodes.Call, typeof(NativeMethods).GetMethod("EnumWindows")); - il.Emit(OpCodes.Pop); + + // return 111+100 + il.Emit(OpCodes.Ldc_I4, 111); + il.Emit(OpCodes.Call, typeof(NativeMethods).GetMethod("CallManagedAdd")); il.Emit(OpCodes.Ret); - NativeMethodInvoker testNativeMethod = (NativeMethodInvoker)testNativeCallable.CreateDelegate(typeof(NativeMethodInvoker)); - testNativeMethod(); + IntNativeMethodInvoker testNativeMethod = (IntNativeMethodInvoker)testNativeCallable.CreateDelegate(typeof(IntNativeMethodInvoker)); + if (testNativeMethod() != 211) + return 0; + return 100; } public static void NegativeTest_ViaDelegate() @@ -68,8 +75,8 @@ public class Program // Try invoking method directly try { - Func<IntPtr, IntPtr, int> invoker = CallbackMethod; - invoker(IntPtr.Zero, IntPtr.Zero); + Func<int, int> invoker = ManagedAddCallback; + invoker(0); } catch (Exception) { @@ -134,10 +141,9 @@ public class Program } [NativeCallable] - public static int CallbackMethod(IntPtr hWnd, IntPtr lParam) + public static int ManagedAddCallback(int n) { - waitHandle.Set(); - return 1; + return n + 100; } [NativeCallable] @@ -153,4 +159,4 @@ public class Program } #endregion //callbacks -}
\ No newline at end of file +} |