summaryrefslogtreecommitdiff
path: root/tests/src/Interop/PInvoke/DateTime/NativeDateTime.cpp
blob: 35a9ecc59065d29a8f055c6bb3142c119416ea97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// 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.

// NativeDateTime.cpp : Defines the exported functions for the DLL application.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <oleauto.h>
#include <xplatform.h>

#define LCID_ENGLISH MAKELCID(MAKELANGID(0x09, 0x01), SORT_DEFAULT)

#pragma pack (push)
#pragma pack (8)
struct Stru_Seq_DateAsStructAsFld // size = 16 bytes
{
    DATE dt;
    INT iInt;
    BSTR bstr;
};
#pragma pack (pop)

#pragma pack (push)
#pragma pack (1)
struct Stru_Exp_DateAsStructAsFld // size = 16 bytes
{
    INT iInt;
    INT padding;
    DATE dt;
};
#pragma pack (pop)

extern "C" BOOL VerifySeqStruct(struct Stru_Seq_DateAsStructAsFld* StDate)
{
    BSTR str;
    VarBstrFromDate(StDate->dt, LCID_ENGLISH, VAR_FOURDIGITYEARS, &str);
    if(wcscmp(L"7/4/2008", (wchar_t *)str) != 0 )
    {
        wprintf(L"FAILURE! InDATE expected '07/04/2008' but received: %s\n", str);
        return FALSE;
    }
    if(StDate->iInt != 100)
    {
        wprintf(L"FAILURE! iInt expected 100 but received: %d\n", StDate->iInt);
        return FALSE;
    }
    if(wcscmp(L"Managed", (wchar_t *)(StDate->bstr)) != 0 )
    {
        wprintf(L"FAILURE! bstr expected 'Managed' but received: %s\n", StDate->bstr);
        return FALSE;
    }
    return TRUE;
}

extern "C" BOOL VerifyExpStruct(struct Stru_Exp_DateAsStructAsFld* StDate)
{
    BSTR str;
    VarBstrFromDate(StDate->dt, LCID_ENGLISH, VAR_FOURDIGITYEARS, &str);
    if(wcscmp(L"7/4/2008", (wchar_t *)str) != 0 )
    {
        wprintf(L"FAILURE! InDATE expected '07/04/2008' but received: %s\n", str);
        return FALSE;
    }
    if(StDate->iInt != 100)
    {
        wprintf(L"FAILURE! iInt expected 100 but received: %d\n", StDate->iInt);
        return FALSE;
    }
    return TRUE;
}

extern "C" void ChangeStru_Seq_DateAsStructAsFld(Stru_Seq_DateAsStructAsFld * StDate)
{
    VarDateFromStr(SysAllocString(L"8/15/1947"), 0, 0, &(StDate->dt));
}

extern "C" void ChangeStru_Exp_DateAsStructAsFld(Stru_Exp_DateAsStructAsFld * StDate)
{
    VarDateFromStr(SysAllocString(L"8/15/1947"), 0, 0, &(StDate->dt));
}

extern "C" DLL_EXPORT BOOL __stdcall Marshal_In_stdcall(DATE d)
{
    BSTR str;
    //DATE ptoD;
    
    // always use the ENGLISH locale so that the string comes out as 11/16/1977 as opposed to 
    // say 16/11/1977 for German locale; otherwise this test would fail on non-ENU locales

    VarBstrFromDate(d, LCID_ENGLISH, VAR_FOURDIGITYEARS, &str);

    if(wcscmp(L"7/4/2008", (wchar_t *)str) != 0 )
    {
        wprintf(L"FAILURE! InDATE expected '07/04/2008' but received: %s\n", str);
        return FALSE;
    }

    VarDateFromStr(SysAllocString(L"8/15/1947"), 0, 0, &d);
    return TRUE;

}

extern "C" DLL_EXPORT BOOL __cdecl Marshal_InOut_cdecl(/*[in,out]*/ DATE* d)
{
    BSTR str;
    //DATE ptoD;
    
    // always use the ENGLISH locale so that the string comes out as 11/16/1977 as opposed to 
    // say 16/11/1977 for German locale; otherwise this test would fail on non-ENU locales

    VarBstrFromDate(*d, LCID_ENGLISH, VAR_FOURDIGITYEARS, &str);

    if(wcscmp(L"7/4/2008", (wchar_t *)str) != 0 )
    {
        wprintf(L"FAILURE! InDATE expected '07/04/2008' but received: %s\n", str);
        return FALSE;
    }

    VarDateFromStr(SysAllocString(L"8/15/1947"), 0, 0, d);
    return TRUE;
}

extern "C" DLL_EXPORT BOOL __stdcall Marshal_Out_stdcall(/*[out]*/ DATE* d)
{
    VarDateFromStr(SysAllocString(L"8/15/1947"), 0, 0, d);
    return TRUE;
}

extern "C" DLL_EXPORT DATE __stdcall Marshal_Ret_stdcall()
{
    DATE d;
    VarDateFromStr(SysAllocString(L"8/15/1947"), 0, 0, &d);
    return d;
}

typedef BOOL (__cdecl * Datetime_Del_Marshal_InOut_cdecl)(/*[in,out]*/ DATE* t);
extern "C" DLL_EXPORT Datetime_Del_Marshal_InOut_cdecl __stdcall GetDel_Marshal_InOut_cdecl()
{
    return Marshal_InOut_cdecl;
}

typedef DATE (__stdcall * Datetime_Del_Marshal_Ret_stdcall)();
extern "C" DLL_EXPORT Datetime_Del_Marshal_Ret_stdcall __stdcall GetDel_Marshal_Ret_stdcall()
{
    return Marshal_Ret_stdcall;
}


typedef BOOL (__stdcall * Datetime_Del_Marshal_Out_stdcall)(/*[out]*/ DATE* t);
extern "C" DLL_EXPORT  Datetime_Del_Marshal_Out_stdcall __stdcall GetDel_Marshal_Out_stdcall()
{
    return Marshal_Out_stdcall;
}

extern "C" DLL_EXPORT BOOL __cdecl RevP_Marshal_InOut_cdecl(Datetime_Del_Marshal_InOut_cdecl d)
{
    DATE ptoD;
    BSTR str;

    VarDateFromStr(SysAllocString(L"8/15/1947"), 0, 0, &ptoD);
    if(d(&ptoD) == FALSE)
    {
        wprintf(L"FAILURE! RevP_Marshal_InOut_cdecl : Date on managed side didn't match\n");
        return FALSE;
    }
    
    //Verify the changes are visible
    VarBstrFromDate(ptoD, LCID_ENGLISH, VAR_FOURDIGITYEARS, &str);
    if(wcscmp(L"8/14/1947", (wchar_t *)str) != 0 )
    {
        wprintf(L"FAILURE! RevP_Marshal_InOut_cdecl : InDATE expected '8/14/1947' but received: %s\n", str);
        return FALSE;
    }
    return TRUE;
}


extern "C" DLL_EXPORT BOOL __stdcall RevP_Marshal_Ret_stdcall(Datetime_Del_Marshal_Ret_stdcall d)
{
    DATE date;
    BSTR str;

    date = d();

    VarBstrFromDate(date, LCID_ENGLISH, VAR_FOURDIGITYEARS, &str);
    if(wcscmp(L"7/4/2008", (wchar_t *)str) != 0 )
    {
        wprintf(L"FAILURE! RevP_Marshal_Ret_stdcall : InDATE expected '07/04/2008' but received: %s\n", str);
        return FALSE;
    }
    return TRUE;
}

extern "C" DLL_EXPORT BOOL __cdecl MarshalSeqStruct_InOut_cdecl(/*[in,out]*/ struct Stru_Seq_DateAsStructAsFld * t)
{
    if(!VerifySeqStruct(t))
        return FALSE;
    
    ChangeStru_Seq_DateAsStructAsFld(t);
    return TRUE;
}

extern "C" DLL_EXPORT BOOL __cdecl MarshalExpStruct_InOut_cdecl(/*[in,out]*/ struct Stru_Exp_DateAsStructAsFld * t)
{
    if(!VerifyExpStruct(t))
        return FALSE;

    ChangeStru_Exp_DateAsStructAsFld(t);
    return TRUE;
}


typedef BOOL (__cdecl * Datetime_Del_MarshalExpStruct_InOut_cdecl)(/*[in,out]*/ struct Stru_Exp_DateAsStructAsFld * t);
extern "C" DLL_EXPORT Datetime_Del_MarshalExpStruct_InOut_cdecl __stdcall GetDel_Del_MarshalExpStruct_InOut_cdecl()
{
    return MarshalExpStruct_InOut_cdecl;
}


typedef BOOL (__cdecl * Datetime_Del_MarshalSeqStruct_InOut_cdecl)(/*[in,out]*/ struct Stru_Seq_DateAsStructAsFld * t);
extern "C" DLL_EXPORT Datetime_Del_MarshalSeqStruct_InOut_cdecl __stdcall GetDel_Del_MarshalSeqStruct_InOut_cdecl()
{
    return MarshalSeqStruct_InOut_cdecl;
}

extern "C" DATE DLL_EXPORT STDMETHODCALLTYPE PassThroughDate(DATE d)
{
    return d;
}