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
14 changes: 13 additions & 1 deletion src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ public unsafe struct InlineUnicodeStruct
[DllImport("PInvokeNative", CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetFunctionPointer();

[DllImport("PInvokeNative", CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetNativeFuncFunctionPointer();

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal unsafe struct InlineString
Expand Down Expand Up @@ -344,6 +346,7 @@ public static int Main()
TestForwardDelegateWithUnmanagedCallersOnly();
TestDecimal();
TestDifferentModopts();
TestFunctionPointers();

return 100;
}
Expand Down Expand Up @@ -677,6 +680,15 @@ private static void TestDelegate()
Marshal.GetDelegateForFunctionPointer<SetLastErrorFuncDelegate>(procAddress);
funcDelegate(0x204);
ThrowIfNotEquals(0x204, Marshal.GetLastWin32Error(), "Not match");
}

private static unsafe void TestFunctionPointers()
{
IntPtr procAddress = GetNativeFuncFunctionPointer();
delegate* unmanaged[Cdecl] <int, int> unmanagedFuncDelegate =
(delegate* unmanaged[Cdecl] <int, int>)procAddress;
var result = unmanagedFuncDelegate(100);
ThrowIfNotEquals(1422, result, "Function pointer did not set expected error code");
}

static int Sum(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
Expand Down Expand Up @@ -1125,7 +1137,7 @@ public static unsafe void TestForwardDelegateWithUnmanagedCallersOnly()
Action a = Marshal.GetDelegateForFunctionPointer<Action>((IntPtr)(void*)(delegate* unmanaged<void>)&UnmanagedCallback);
a();
}

public static unsafe void TestDifferentModopts()
{
byte storage;
Expand Down
9 changes: 9 additions & 0 deletions src/tests/nativeaot/SmokeTests/PInvoke/PInvokeNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,15 @@ DLL_EXPORT void* __stdcall GetFunctionPointer()
return (void*)&SetLastErrorFunc;
}

DLL_EXPORT int __cdecl NativeFunc(int errorCode)
{
return errorCode + 1322;
}
DLL_EXPORT void* __stdcall GetNativeFuncFunctionPointer()
{
return (void*)&NativeFunc;
}

typedef struct {
int c;
char inlineString[260];
Expand Down