add_subdirectory(PInvoke/Delegate/MarshalDelegateAsField)
add_subdirectory(PInvoke/Delegate/MarshalDelegateAsParam)
add_subdirectory(PInvoke/Primitives/Int)
+add_subdirectory(PInvoke/Miscellaneous/HandleRef)
+add_subdirectory(PInvoke/Miscellaneous/MultipleAssembliesWithSamePInvoke)
add_subdirectory(PInvoke/ExactSpelling)
add_subdirectory(PInvoke/AsAny)
add_subdirectory(NativeCallable)
--- /dev/null
+#VCXPROJ
+cmake_minimum_required (VERSION 2.6)
+project (HandleRefNative)
+include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake")
+include_directories(${INC_PLATFORM_DIR})
+set(SOURCES
+ HandleRefNative.cpp
+)
+# Additional files to reference:
+# HandleRefNative.def
+# add the executable
+add_library (HandleRefNative SHARED ${SOURCES})
+# add the install targets
+install (TARGETS HandleRefNative DESTINATION bin)
--- /dev/null
+// 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 <stdio.h>
+#include <xplatform.h>
+
+const int intManaged = 1000;
+const int intNative = 2000;
+const int intReturn = 3000;
+const int intErrReturn = 4000;
+const int expectedStackGuard = 5000;
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_In(/*[in]*/int *pintValue, int stackGuard)
+{
+ if (*pintValue != intManaged)
+ {
+ printf("Error in Function MarshalPointer_In(Native Client)\n");
+ printf("Expected:%u\n", intManaged);
+ printf("Actual:%u\n",*pintValue);
+
+ // Return the error value instead if verification failed
+ return intErrReturn;
+ }
+ if (stackGuard != expectedStackGuard)
+ {
+ printf("Stack error in Function MarshalPointer_In(Native Client)\n");
+ return intErrReturn;
+ }
+
+ return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_InOut(/*[in,out]*/int *pintValue, int stackGuard)
+{
+ if(*pintValue != intManaged)
+ {
+ printf("Error in Function MarshalPointer_InOut(Native Client)\n");
+ printf("Expected:%u\n", intManaged);
+ printf("Actual:%u\n",*pintValue);
+
+ // Return the error value instead if verification failed
+ return intErrReturn;
+ }
+ if (stackGuard != expectedStackGuard)
+ {
+ printf("Stack error in Function MarshalPointer_In(Native Client)\n");
+ return intErrReturn;
+ }
+
+ // In-Place Change
+ *pintValue = intNative;
+
+ return intReturn;
+}
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE MarshalPointer_Out(/*[out]*/ int *pintValue, int stackGuard)
+{
+ *pintValue = intNative;
+ if (stackGuard != expectedStackGuard)
+ {
+ printf("Stack error in Function MarshalPointer_In(Native Client)\n");
+ return intErrReturn;
+ }
+
+ return intReturn;
+}
+
+typedef void (*GCCallback)(void);
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE TestNoGC(int *pintValue, GCCallback gcCallback)
+{
+ int origValue = *pintValue;
+ gcCallback();
+ int afterGCValue = *pintValue;
+ if (origValue != afterGCValue)
+ {
+ printf("Error in Function TestNoGC(Native Client)\n");
+ printf("Expected:%u\n", origValue);
+ printf("Actual:%u\n", afterGCValue);
+ return intErrReturn;
+ }
+
+ return intReturn;
+}
+
--- /dev/null
+// 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.
+
+using System.Runtime.InteropServices;
+using System;
+using System.Reflection;
+using System.Text;
+using TestLibrary;
+
+class BoxedInt
+{
+ public int MyInt;
+}
+
+class HandleRefTest
+{
+ [DllImport(@"HandleRefNative", CallingConvention = CallingConvention.Winapi)]
+ private static extern int MarshalPointer_In(HandleRef pintValue, int stackGuard);
+
+ [DllImport(@"HandleRefNative", CallingConvention = CallingConvention.Winapi)]
+ private static extern int MarshalPointer_InOut(HandleRef pintValue, int stackGuard);
+
+ [DllImport(@"HandleRefNative", CallingConvention = CallingConvention.Winapi)]
+ private static extern int MarshalPointer_Out(HandleRef pintValue, int stackGuard);
+
+ [DllImport(@"HandleRefNative", CallingConvention = CallingConvention.Winapi)]
+ private static extern int TestNoGC(HandleRef pintValue, Action gcCallback);
+
+ public unsafe static int Main(string[] args)
+ {
+ try{
+ const int intManaged = 1000;
+ const int intNative = 2000;
+ const int intReturn = 3000;
+ const int stackGuard = 5000;
+
+ Console.WriteLine("MarshalPointer_In");
+ int int1 = intManaged;
+ int* int1Ptr = &int1;
+ HandleRef hr1 = new HandleRef(new Object(), (IntPtr)int1Ptr);
+ Assert.AreEqual(intReturn, MarshalPointer_In(hr1, stackGuard), "The return value is wrong");
+ Assert.AreEqual(intManaged, int1, "The parameter value is changed");
+
+ Console.WriteLine("MarshalPointer_InOut");
+ int int2 = intManaged;
+ int* int2Ptr = &int2;
+ HandleRef hr2 = new HandleRef(new Object(), (IntPtr)int2Ptr);
+ Assert.AreEqual(intReturn, MarshalPointer_InOut(hr2, stackGuard), "The return value is wrong");
+ Assert.AreEqual(intNative, int2, "The passed value is wrong");
+
+ Console.WriteLine("MarshalPointer_Out");
+ int int3 = intManaged;
+ int* int3Ptr = &int3;
+ HandleRef hr3 = new HandleRef(new Object(), (IntPtr)int3Ptr);
+ Assert.AreEqual(intReturn, MarshalPointer_Out(hr3, stackGuard), "The return value is wrong");
+ Assert.AreEqual(intNative, int3, "The passed value is wrong");
+
+ // Note that this scenario will always pass in a debug build because all values
+ // stay rooted until the end of the method.
+ Console.WriteLine("TestNoGC");
+
+ // Keep the int boxed and pinned to prevent it from getting collected.
+ // That way, we can safely reference it from finalizers that run on shutdown.
+ BoxedInt boxedInt = new BoxedInt();
+ GCHandle.Alloc(boxedInt, GCHandleType.Normal);
+ int* int4Ptr;
+ fixed (int* tempIntPtr = &boxedInt.MyInt)
+ {
+ // Smuggle the pointer out of the fixed scope
+ int4Ptr = tempIntPtr;
+ }
+ Console.WriteLine("2");
+ *int4Ptr = intManaged;
+ CollectableClass collectableClass = new CollectableClass(int4Ptr);
+ HandleRef hr4 = new HandleRef(collectableClass, (IntPtr)int4Ptr);
+ Action gcCallback = () => { Console.WriteLine("GC callback now"); GC.Collect(2, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); GC.Collect(2, GCCollectionMode.Forced); };
+ Assert.AreEqual(intReturn, TestNoGC(hr4, gcCallback), "The return value is wrong");
+ Console.WriteLine("Native code finished");
+
+ return 100;
+ } catch (Exception e){
+ Console.WriteLine($"Test Failure: {e}");
+ return 101;
+ }
+ }
+
+ /// <summary>
+ /// Class that will change a pointer passed to native code when this class gets finalized.
+ /// Native code can check whether the pointer changed during a P/Invoke
+ /// </summary>
+ unsafe class CollectableClass
+ {
+ int* PtrToChange;
+ public CollectableClass(int* ptrToChange)
+ {
+ PtrToChange = ptrToChange;
+ }
+
+ ~CollectableClass()
+ {
+ Console.WriteLine("CollectableClass collected");
+ *PtrToChange = Int32.MaxValue;
+ }
+ }
+}
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <AssemblyName>HandleRefTest</AssemblyName>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\</SolutionDir>
+ <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="HandleRefTest.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="CMakeLists.txt" />
+ </ItemGroup>
+ <Import Project="../../../Interop.settings.targets" />
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
--- /dev/null
+#VCXPROJ
+cmake_minimum_required (VERSION 2.6)
+project (MAWSPINative)
+include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake")
+include_directories(${INC_PLATFORM_DIR})
+set(SOURCES
+ MAWSPINative.cpp
+)
+# Additional files to reference:
+# MAWSPINative.def
+# add the executable
+add_library (MAWSPINative SHARED ${SOURCES})
+# add the install targets
+install (TARGETS MAWSPINative DESTINATION bin)
--- /dev/null
+// 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>
+
+extern "C" DLL_EXPORT int STDMETHODCALLTYPE GetInt()
+{
+ return 24;
+}
--- /dev/null
+// 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.
+
+using System;
+using System.Runtime.InteropServices;
+using TestLibrary;
+
+class MultipleAssembliesWithSamePInvokeTest
+{
+ [DllImport(@"MAWSPINative", CallingConvention = CallingConvention.StdCall)]
+ private static extern int GetInt();
+
+ public static int Main(string[] args)
+ {
+ try{
+ Assert.AreEqual(24, GetInt(), "MultipleAssembliesWithSamePInvoke.GetInt() failed.");
+ Assert.AreEqual(24, ManagedDll1.Class1.GetInt(), "ManagedDll.Class1.GetInt() failed.");
+ Assert.AreEqual(24, ManagedDll2.Class2.GetInt(), "ManagedDll.Class2.GetInt() failed.");
+
+ return 100;
+ } catch (Exception e){
+ Console.WriteLine($"Test Failure: {e}");
+ return 101;
+ }
+ }
+}
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <AssemblyName>MAWSPITest</AssemblyName>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\</SolutionDir>
+ <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="MAWSPITest.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="CMakeLists.txt" />
+ <ProjectReference Include="ManagedDll1\ManagedDll1.csproj" />
+ <ProjectReference Include="ManagedDll2\ManagedDll2.csproj" />
+ </ItemGroup>
+ <Import Project="../../../Interop.settings.targets" />
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
--- /dev/null
+// 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.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace ManagedDll1
+{
+ public class Class1
+ {
+ [DllImport(@"MAWSPINative", EntryPoint="GetInt", CallingConvention = CallingConvention.StdCall)]
+ private static extern int GetIntNative();
+
+ public static int GetInt()
+ {
+ return GetIntNative();
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <AssemblyName>ManagedDll1</AssemblyName>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\..\</SolutionDir>
+ <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="ManagedDll1.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\CMakeLists.txt" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
\ No newline at end of file
--- /dev/null
+// 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.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace ManagedDll2
+{
+ public class Class2
+ {
+ [DllImport(@"MAWSPINative", EntryPoint="GetInt", CallingConvention = CallingConvention.StdCall)]
+ private static extern int GetIntNative();
+
+ public static int GetInt()
+ {
+ return GetIntNative();
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <AssemblyName>ManagedDll2</AssemblyName>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\..\</SolutionDir>
+ <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="ManagedDll2.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\CMakeLists.txt" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
\ No newline at end of file