Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/tests/Interop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ add_subdirectory(NativeLibrary/NativeLibraryToLoad)
add_subdirectory(DllImportAttribute/DllImportPath)
add_subdirectory(DllImportAttribute/ExactSpelling)
add_subdirectory(ICustomMarshaler/ConflictingNames)
add_subdirectory(ICustomMarshaler/Primitives)
add_subdirectory(LayoutClass)
add_subdirectory(PInvoke/DateTime)
if(CLR_CMAKE_TARGET_WIN32)
Expand Down
10 changes: 10 additions & 0 deletions src/tests/Interop/ICustomMarshaler/Primitives/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project (CustomMarshalersPrimitives)
include_directories(${INC_PLATFORM_DIR})
set(SOURCES ICustomMarshalerNative.cpp )

# add the executable
add_library (CustomMarshalersPrimitives SHARED ${SOURCES})
target_link_libraries(CustomMarshalersPrimitives ${LINK_LIBRARIES_ADDITIONAL})

# add the install targets
install (TARGETS CustomMarshalersPrimitives DESTINATION bin)
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,37 @@ public static void Parameter_CustomMarshalerProvided_CallsMethodsInCorrectOrderi
"Called CleanUpNativeData"
});
Assert.Equal(expectedOrderingSecondCall, OrderTrackingCustomMarshaler.Events);

// GetInstance is only called once.
string val3 = "7488";
Assert.Equal(val3, OrderTrackingMethodRef(ref val3));
IEnumerable<string> expectedOrderingThirdCall = expectedOrderingSecondCall.Concat(new string[]
{
"Called MarshalManagedToNative",
"Called MarshalNativeToManaged",
"Called CleanUpManagedData",
"Called MarshalNativeToManaged",
"Called CleanUpNativeData",
});
Assert.Equal(expectedOrderingThirdCall.Skip(7), OrderTrackingCustomMarshaler.Events.Skip(7));

OrderTrackingMethodOut(out var val4);
Assert.Equal("2334", val4);
IEnumerable<string> expectedOrderingForthCall = expectedOrderingThirdCall.Concat(new string[]
{
"Called MarshalNativeToManaged",
});
Assert.Equal(expectedOrderingForthCall.Skip(12), OrderTrackingCustomMarshaler.Events.Skip(12));

var val5 = OrderTrackingMethodDelegate(439, (x) => x.ToString());
Assert.Equal("439", val5);
IEnumerable<string> expectedOrderingFifthCall = expectedOrderingForthCall.Concat(new string[]
{
"Called MarshalManagedToNative",
"Called CleanUpManagedData",
"Called MarshalNativeToManaged",
});
Assert.Equal(expectedOrderingFifthCall.Skip(13), OrderTrackingCustomMarshaler.Events.Skip(13));
}

// This should only be used *once*, as it uses static state.
Expand Down Expand Up @@ -201,7 +232,7 @@ public IntPtr MarshalManagedToNative(object ManagedObj)
public object MarshalNativeToManaged(IntPtr pNativeData)
{
Events.Add("Called MarshalNativeToManaged");
return pNativeData.ToInt32().ToString();
return pNativeData.ToInt64().ToString();
}

public static ICustomMarshaler GetInstance(string cookie)
Expand All @@ -216,6 +247,20 @@ public static ICustomMarshaler GetInstance(string cookie)
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(OrderTrackingCustomMarshaler))]
public static extern string OrderTrackingMethod([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(OrderTrackingCustomMarshaler))] string str);

[DllImport("CustomMarshalersPrimitives", EntryPoint = "NativeParseIntRef")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(OrderTrackingCustomMarshaler))]
public static extern string OrderTrackingMethodRef([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(OrderTrackingCustomMarshaler))] ref string str);

[DllImport("CustomMarshalersPrimitives", EntryPoint = "NativeParseIntOut")]
public static extern void OrderTrackingMethodOut([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(OrderTrackingCustomMarshaler))] out string str);

[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(OrderTrackingCustomMarshaler))]
public delegate string TestDelegate(int val);

[DllImport("CustomMarshalersPrimitives", EntryPoint = "NativeParseIntDelegate")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(OrderTrackingCustomMarshaler))]
public static extern string OrderTrackingMethodDelegate(int val, TestDelegate dlg);

public static void CustomMarshaler_BothMarshalTypeRefAndMarshalTypeProvided_PicksMarshalType()
{
Assert.Equal(2, BothTypeRefAndTypeMethod("64001"));
Expand Down Expand Up @@ -613,6 +658,17 @@ public void CleanUpManagedData(object ManagedObj) { }
[DllImport(LibcLibrary, EntryPoint = "atoi", CallingConvention = CallingConvention.Cdecl)]
public static extern int DifferentCustomMarshalerType([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(OuterCustomMarshaler))] string str);

[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(BoxedValueTypeCustomMarshaler))]
public delegate string TestDelegateRef([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(BoxedValueTypeCustomMarshaler))] ref int val);

[DllImport("CustomMarshalersPrimitives", EntryPoint = "NativeParseIntDelegateRef")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(BoxedValueTypeCustomMarshaler))]
public static extern string CustomMarshallerWithDelegateRef(int val, TestDelegateRef dlg);

public static void DelegateParameter_MarshalerOnRefInt_ThrowsMarshalDirectiveException()
{
Assert.Throws<MarshalDirectiveException>(() => CustomMarshallerWithDelegateRef(84664, (ref int x) => x.ToString()));
}
public static int Main(String[] args)
{
try
Expand Down Expand Up @@ -643,6 +699,7 @@ public static int Main(String[] args)
Parameter_CleanUpNativeDataMethodThrows_ThrowsActualException();
Field_ParentIsStruct_ThrowsTypeLoadException();
Parameter_DifferentCustomMarshalerType_MarshalsCorrectly();
DelegateParameter_MarshalerOnRefInt_ThrowsMarshalDirectiveException();
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <stdlib.h>
#include <xplatform.h>

using TestDelegate = LPCSTR(STDMETHODCALLTYPE*)(int);
using TestDelegateRef = LPCSTR(STDMETHODCALLTYPE*)(int*);

extern "C" int DLL_EXPORT STDMETHODCALLTYPE NativeParseInt(LPCSTR str)
{
return atoi(str);
}

extern "C" int DLL_EXPORT STDMETHODCALLTYPE NativeParseIntRef(LPCSTR* str)
{
return atoi(*str);
}

extern "C" void DLL_EXPORT STDMETHODCALLTYPE NativeParseIntOut(int* outVal)
{
*outVal = 2334;
}

extern "C" int DLL_EXPORT STDMETHODCALLTYPE NativeParseIntDelegate(int val, TestDelegate del)
{
return atoi(del(val));
}

extern "C" int DLL_EXPORT STDMETHODCALLTYPE NativeParseIntDelegateRef(int val, TestDelegateRef del)
{
return atoi(del(&val));
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<ItemGroup>
<Compile Include="ICustomMarshaler.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="CMakeLists.txt" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
<ItemGroup>
<Compile Include="ICustomMarshaler.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="CMakeLists.txt" />
</ItemGroup>
</Project>