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#include <xplatform.h>
5#include "platformdefines.h"
6
7const char strManaged[] = " \0Managed\0String\0 ";
8size_t lenstrManaged = ARRAYSIZE(strManaged) - 1; // the length of strManaged
9
10const char strReturn[] = "a";
11size_t lenstrReturn = ARRAYSIZE(strReturn) - 1; //the length of strReturn
12
13const char strNative[] = "Native String";
14size_t lenstrNative = ARRAYSIZE(strNative) - 1; //the len of strNative
15
16//Test Method1
17extern "C" DLL_EXPORT BSTR STDMETHODCALLTYPE Marshal_In(/*[in]*/ BSTR s)
18{
19 //Check the input
20 size_t len = TP_SysStringByteLen(s);
21 if ((len != lenstrManaged) || (memcmp(s, strManaged, len) != 0))
22 {
23 printf("Error in Function Marshal_In(Native Client)\n");
24
25 //Expected
26 printf("Expected:");
27 for (size_t i = 0; i < lenstrManaged; ++i)
28 putchar(*(((char *)strManaged) + i));
29 printf("\tThe length of Expected:%zd\n", lenstrManaged);
30
31 //Actual
32 printf("Actual:");
33 for (size_t j = 0; j < len; ++j)
34 putchar(*(((char *)s) + j));
35 printf("\tThe length of Actual:%zd\n", len);
36 }
37
38 return TP_SysAllocStringByteLen(strReturn, lenstrReturn);
39}
40
41//Test Method2
42extern "C" DLL_EXPORT BSTR STDMETHODCALLTYPE Marshal_InOut(/*[In,Out]*/ BSTR s)
43{
44
45 //Check the Input
46 size_t len = TP_SysStringByteLen(s);
47 if ((len != lenstrManaged) || (memcmp(s, strManaged, len) != 0))
48 {
49 printf("Error in Function Marshal_InOut(Native Client)\n");
50
51 //Expected
52 printf("Expected:");
53 for (size_t i = 0; i < lenstrManaged; ++i)
54 putchar(*(((char *)strManaged) + i));
55 printf("\tThe length of Expected:%zd\n", lenstrManaged);
56
57 //Actual
58 printf("Actual:");
59 for (size_t j = 0; j < len; ++j)
60 putchar(*(((char *)s) + j));
61 printf("\tThe length of Actual:%zd\n", len);
62 }
63
64 //In-Place Change
65 memcpy((char *)s, strNative, lenstrNative);
66 *((UINT *)s - 1) = (UINT) TP_SysStringByteLen(s);
67
68 //Return
69 return TP_SysAllocStringByteLen(strReturn, lenstrReturn);
70}
71extern "C" DLL_EXPORT BSTR STDMETHODCALLTYPE Marshal_Out(/*[Out]*/ BSTR s)
72{
73 s = TP_SysAllocStringByteLen(strNative, lenstrNative);
74
75 //In-Place Change
76 memcpy((char *)s, strNative, lenstrNative);
77 *((UINT *)s - 1) = (UINT) TP_SysStringByteLen(s);
78
79 //Return
80 return TP_SysAllocStringByteLen(strReturn, lenstrReturn);
81}
82
83extern "C" DLL_EXPORT BSTR STDMETHODCALLTYPE MarshalPointer_In(/*[in]*/ BSTR *s)
84{
85 //CheckInput
86 size_t len = TP_SysStringByteLen(*s);
87 if ((len != lenstrManaged) || (memcmp(*s, strManaged, len) != 0))
88 {
89 printf("Error in Function MarshalPointer_In\n");
90
91 //Expected
92 printf("Expected:");
93 for (size_t i = 0; i < lenstrManaged; ++i)
94 putchar(*(((char *)strManaged) + i));
95 printf("\tThe length of Expected:%zd\n", lenstrManaged);
96
97 //Actual
98 printf("Actual:");
99 for (size_t j = 0; j < len; ++j)
100 putchar(*(((char *)*s) + j));
101 printf("\tThe length of Actual:%zd\n", len);
102 }
103
104 return TP_SysAllocStringByteLen(strReturn, lenstrReturn);
105}
106
107extern "C" DLL_EXPORT BSTR STDMETHODCALLTYPE MarshalPointer_InOut(/*[in,out]*/ BSTR *s)
108{
109 //Check the Input
110 size_t len = TP_SysStringByteLen(*s);
111 if ((len != lenstrManaged) || (memcmp(*s, strManaged, len) != 0))
112 {
113 printf("Error in Function MarshalPointer_InOut\n");
114
115 //Expected
116 printf("Expected:");
117 for (size_t i = 0; i < lenstrManaged; ++i)
118 putchar(*(((char *)strManaged) + i));
119
120 printf("\tThe length of Expected:%zd\n", lenstrManaged);
121
122 //Actual
123 printf("Actual:");
124 for (size_t j = 0; j < len; ++j)
125 putchar(*(((char *)*s) + j));
126
127 printf("\tThe length of Actual:%zd\n", len);
128 }
129
130 //Allocate New
131 TP_SysFreeString(*s);
132 *s = TP_SysAllocStringByteLen(strNative, lenstrNative);
133
134 //Return
135 return TP_SysAllocStringByteLen(strReturn, lenstrReturn);
136}
137
138extern "C" DLL_EXPORT BSTR STDMETHODCALLTYPE MarshalPointer_Out(/*[out]*/ BSTR *s)
139{
140 *s = TP_SysAllocStringByteLen(strNative, lenstrNative);
141 return TP_SysAllocStringByteLen(strReturn, lenstrReturn);
142}
143