1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5#include <stdio.h>
6#include <xplatform.h>
7
8typedef BOOL(__stdcall *HandleCallback)(void*);
9
10extern "C" DLL_EXPORT size_t __stdcall In(void* handle, HandleCallback handleCallback)
11{
12 if (handleCallback != nullptr && !handleCallback(handle))
13 {
14 return (size_t)(-1);
15 }
16
17 return reinterpret_cast<size_t>(handle);
18}
19
20extern "C" DLL_EXPORT void* __stdcall Ret(void* handleValue)
21{
22 return handleValue;
23}
24
25extern "C" DLL_EXPORT void __stdcall Out(void* handleValue, void** pHandle)
26{
27 if (pHandle == nullptr)
28 {
29 return;
30 }
31
32 *pHandle = handleValue;
33}
34
35extern "C" DLL_EXPORT size_t __stdcall Ref(void** pHandle, HandleCallback handleCallback)
36{
37 if (handleCallback != nullptr && !handleCallback(*pHandle))
38 {
39 return (size_t)(-1);
40 }
41
42 return reinterpret_cast<size_t>(*pHandle);
43}
44
45extern "C" DLL_EXPORT size_t __stdcall RefModify(void* handleValue, void** pHandle, HandleCallback handleCallback)
46{
47 if (handleCallback != nullptr && !handleCallback(*pHandle))
48 {
49 return (size_t)(-1);
50 }
51
52 void* originalHandle = *pHandle;
53
54 *pHandle = handleValue;
55
56 return reinterpret_cast<size_t>(originalHandle);
57}
58
59typedef void(__stdcall *InCallback)(void*);
60
61extern "C" DLL_EXPORT void __stdcall InvokeInCallback(InCallback callback, void* handle)
62{
63 callback(handle);
64}
65
66typedef void(__stdcall *RefCallback)(void**);
67
68extern "C" DLL_EXPORT void __stdcall InvokeRefCallback(RefCallback callback, void** pHandle)
69{
70 callback(pHandle);
71}
72
73typedef void*(__stdcall *RetCallback)();
74
75extern "C" DLL_EXPORT void* __stdcall InvokeRetCallback(RetCallback callback)
76{
77 return callback();
78}
79