| 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 <stdlib.h> |
| 7 | #include <xplatform.h> |
| 8 | |
| 9 | /*-----------------------------------------------------------------------------* |
| 10 | * * |
| 11 | * For MarshalDelegateAsParam_AsFunctionPtr.cs * |
| 12 | * * |
| 13 | *-----------------------------------------------------------------------------*/ |
| 14 | |
| 15 | //auxiliary verification value |
| 16 | const int COMMONMETHODCALLED1_RIGHT_RETVAL = 10; |
| 17 | const int COMMONMETHODCALLED2_RIGHT_RETVAL = 20; |
| 18 | |
| 19 | //common method called by function pointer(Delegate) |
| 20 | extern "C" DLL_EXPORT int STDMETHODCALLTYPE CommonMethodCalled1() |
| 21 | { |
| 22 | printf("\n\tCalling CommonMethodCalled1() by FuncPtr..." ); |
| 23 | return COMMONMETHODCALLED1_RIGHT_RETVAL; |
| 24 | } |
| 25 | |
| 26 | extern "C" DLL_EXPORT int STDMETHODCALLTYPE CommonMethodCalled2() |
| 27 | { |
| 28 | printf("\n\tCalling CommonMethodCalled2() by FuncPtr..." ); |
| 29 | return COMMONMETHODCALLED2_RIGHT_RETVAL; |
| 30 | } |
| 31 | |
| 32 | //define function pointer |
| 33 | typedef int (STDMETHODCALLTYPE *DelegateParam)(); |
| 34 | |
| 35 | //delegate marshalled by val |
| 36 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateByValParam(DelegateParam deleParam) |
| 37 | { |
| 38 | printf("\tdelegate marshaled by val." ); |
| 39 | |
| 40 | //verify return value |
| 41 | if(deleParam == NULL) |
| 42 | { |
| 43 | printf("\n\tNULL delegate!" ); |
| 44 | return FALSE; |
| 45 | } |
| 46 | else if( deleParam() != COMMONMETHODCALLED1_RIGHT_RETVAL) |
| 47 | { |
| 48 | printf("\n\tReturn Value Err!" ); |
| 49 | return FALSE; |
| 50 | } |
| 51 | |
| 52 | return TRUE; |
| 53 | } |
| 54 | |
| 55 | //delegate marshalled by ref |
| 56 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateByRefParam(DelegateParam* deleParam) |
| 57 | { |
| 58 | printf("\n\tdelegate marshaled by ref." ); |
| 59 | |
| 60 | //verify value |
| 61 | if((*deleParam) == NULL) |
| 62 | { |
| 63 | printf("\n\tNULL delegate!" ); |
| 64 | return FALSE; |
| 65 | } |
| 66 | else if((*deleParam)() != COMMONMETHODCALLED1_RIGHT_RETVAL) |
| 67 | { |
| 68 | printf("\n\tReturn Value Err!" ); |
| 69 | return FALSE; |
| 70 | } |
| 71 | |
| 72 | //make funcptr point to CommonMethodCalled2 |
| 73 | *deleParam = CommonMethodCalled2; |
| 74 | printf("\n\tNow FuncPtr point to CommonMethodCalled2 !" ); |
| 75 | //verify value return again |
| 76 | if((*deleParam)() != COMMONMETHODCALLED2_RIGHT_RETVAL) |
| 77 | { |
| 78 | printf("\n\tReturn Value Err!" ); |
| 79 | return FALSE; |
| 80 | } |
| 81 | |
| 82 | return TRUE; |
| 83 | } |
| 84 | |
| 85 | //delegate marshalled by in,val |
| 86 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateByInValParam(DelegateParam deleParam) |
| 87 | { |
| 88 | printf("\n\tdelegate marshalled by in,val." ); |
| 89 | |
| 90 | //verify return value |
| 91 | if(deleParam == NULL) |
| 92 | { |
| 93 | printf("\n\tNULL delegate!" ); |
| 94 | return FALSE; |
| 95 | } |
| 96 | else if( deleParam() != COMMONMETHODCALLED1_RIGHT_RETVAL) |
| 97 | { |
| 98 | printf("\n\tReturn Value Err!" ); |
| 99 | return FALSE; |
| 100 | } |
| 101 | |
| 102 | deleParam = CommonMethodCalled2; |
| 103 | |
| 104 | return TRUE; |
| 105 | } |
| 106 | |
| 107 | //delegate marshalled by in,ref |
| 108 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateByInRefParam(DelegateParam* deleParam) |
| 109 | { |
| 110 | printf("\n\tdelegate marshalled by in,ref." ); |
| 111 | |
| 112 | //verify return value |
| 113 | if(*deleParam == NULL) |
| 114 | { |
| 115 | printf("\n\tNULL delegate!" ); |
| 116 | return FALSE; |
| 117 | } |
| 118 | else if((*deleParam)() != COMMONMETHODCALLED1_RIGHT_RETVAL) |
| 119 | { |
| 120 | printf("\n\tReturn Value Err!" ); |
| 121 | return FALSE; |
| 122 | } |
| 123 | |
| 124 | *deleParam = CommonMethodCalled2; |
| 125 | |
| 126 | return TRUE; |
| 127 | } |
| 128 | |
| 129 | //delegate marshalled by out,val |
| 130 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateByOutValParam(DelegateParam deleParam) |
| 131 | { |
| 132 | printf("\n\tdelegate marshalled by out,val." ); |
| 133 | |
| 134 | //verify return value |
| 135 | if(deleParam == NULL) |
| 136 | { |
| 137 | printf("\n\tNULL delegate!" ); |
| 138 | return FALSE; |
| 139 | } |
| 140 | else if( deleParam() != COMMONMETHODCALLED1_RIGHT_RETVAL) |
| 141 | { |
| 142 | printf("\n\tReturn Value Err!" ); |
| 143 | return FALSE; |
| 144 | } |
| 145 | |
| 146 | return TRUE; |
| 147 | } |
| 148 | |
| 149 | //delegate marshalled by out,ref |
| 150 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateByOutRefParam(DelegateParam* deleParam) |
| 151 | { |
| 152 | printf("\n\tdelegate marshalled by out,ref." ); |
| 153 | |
| 154 | //arg getted should be NULL |
| 155 | //when args ref marshaled as [Out] attribute, the args actually initialized on the native side |
| 156 | if(*deleParam != NULL) |
| 157 | { |
| 158 | printf("\n\tDelegate is not NULL !" ); |
| 159 | return FALSE; |
| 160 | } |
| 161 | |
| 162 | //initial value of delegate marshaled |
| 163 | *deleParam = CommonMethodCalled2; |
| 164 | printf("\n\tNow FuncPtr point to CommonMethodCalled2 !" ); |
| 165 | |
| 166 | //verify value return again |
| 167 | if((*deleParam)() != COMMONMETHODCALLED2_RIGHT_RETVAL) |
| 168 | { |
| 169 | printf("\n\tReturn Value Err!" ); |
| 170 | return FALSE; |
| 171 | } |
| 172 | |
| 173 | return TRUE; |
| 174 | } |
| 175 | |
| 176 | //delegate marshalled by in,out,val |
| 177 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateByInOutValParam(DelegateParam deleParam) |
| 178 | { |
| 179 | printf("\n\tdelegate marshalled by in,out,val." ); |
| 180 | //verify return value |
| 181 | if(deleParam == NULL) |
| 182 | { |
| 183 | printf("\n\tNULL delegate!" ); |
| 184 | return FALSE; |
| 185 | } |
| 186 | else if( deleParam() != COMMONMETHODCALLED1_RIGHT_RETVAL) |
| 187 | { |
| 188 | printf("\n\tReturn Value Err!" ); |
| 189 | return FALSE; |
| 190 | } |
| 191 | |
| 192 | return TRUE; |
| 193 | } |
| 194 | |
| 195 | //delegate marshalled by in,out,ref |
| 196 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateByInOutRefParam(DelegateParam* deleParam) |
| 197 | { |
| 198 | printf("\n\tdelegate marshalled by in,out,ref." ); |
| 199 | |
| 200 | //verify value |
| 201 | if((*deleParam) == NULL) |
| 202 | { |
| 203 | printf("\n\tNULL delegate!" ); |
| 204 | return FALSE; |
| 205 | } |
| 206 | else if((*deleParam)() != COMMONMETHODCALLED1_RIGHT_RETVAL) |
| 207 | { |
| 208 | printf("\n\tReturn Value Err!" ); |
| 209 | return FALSE; |
| 210 | } |
| 211 | |
| 212 | //make funcptr point to CommonMethodCalled2 |
| 213 | *deleParam = CommonMethodCalled2; |
| 214 | printf("\n\tNow FuncPtr point to CommonMethodCalled2 !" ); |
| 215 | //verify value return again |
| 216 | if((*deleParam)() != COMMONMETHODCALLED2_RIGHT_RETVAL) |
| 217 | { |
| 218 | printf("\n\tReturn Value Err!" ); |
| 219 | return FALSE; |
| 220 | } |
| 221 | |
| 222 | return TRUE; |
| 223 | } |
| 224 | |
| 225 | //ret delegate by val |
| 226 | extern "C" DLL_EXPORT DelegateParam STDMETHODCALLTYPE ReturnDelegateByVal() |
| 227 | { |
| 228 | printf("\n\tdelegate marshalled by val." ); |
| 229 | |
| 230 | return CommonMethodCalled1; |
| 231 | } |
| 232 | |
| 233 | #ifdef _WIN32 |
| 234 | |
| 235 | #include <windows.h> |
| 236 | |
| 237 | /* -----------------------------------------------------------------------------* |
| 238 | * * |
| 239 | * For MarshalDelegateAsParam_AsInterface.cs * |
| 240 | * * |
| 241 | * -----------------------------------------------------------------------------*/ |
| 242 | |
| 243 | #import "mscorlib.tlb" no_namespace named_guids raw_interfaces_only rename("ReportEvent","ReportEventNew") |
| 244 | |
| 245 | typedef struct{ |
| 246 | int result1; |
| 247 | int result2; |
| 248 | int result3; |
| 249 | } Result; |
| 250 | |
| 251 | const int COMMONMETHOD1_RESULT = 10; |
| 252 | const int COMMONMETHOD2_RESULT = 20; |
| 253 | const int COMMONMETHOD3_RESULT = 30; |
| 254 | |
| 255 | const Result expected = { |
| 256 | COMMONMETHOD1_RESULT, |
| 257 | COMMONMETHOD2_RESULT, |
| 258 | COMMONMETHOD3_RESULT |
| 259 | }; |
| 260 | |
| 261 | Result result = {0,0,0}; |
| 262 | |
| 263 | void STDMETHODCALLTYPE ResetToZero() |
| 264 | { |
| 265 | result.result1 = result.result2 = result.result3 = 0; |
| 266 | } |
| 267 | |
| 268 | extern "C" DLL_EXPORT void STDMETHODCALLTYPE CommonMethod1() |
| 269 | { |
| 270 | printf("\n\tCommonMethod1() Calling..." ); |
| 271 | result.result1 = 10; |
| 272 | } |
| 273 | |
| 274 | extern "C" DLL_EXPORT void STDMETHODCALLTYPE CommonMethod2() |
| 275 | { |
| 276 | printf("\n\tCommonMethod2() Calling..." ); |
| 277 | result.result2 = 20; |
| 278 | } |
| 279 | |
| 280 | extern "C" DLL_EXPORT void STDMETHODCALLTYPE CommonMethod3() |
| 281 | { |
| 282 | printf("\n\tCommonMethod3() Calling..." ); |
| 283 | result.result3 = 30; |
| 284 | } |
| 285 | |
| 286 | BOOL STDMETHODCALLTYPE Verify(Result expectedR, Result resultR) |
| 287 | { |
| 288 | return expectedR.result1 == resultR.result1 |
| 289 | && expectedR.result2 == resultR.result2 |
| 290 | && expectedR.result3 == resultR.result3; |
| 291 | } |
| 292 | |
| 293 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrByValParam(_Delegate * p_dele) |
| 294 | { |
| 295 | ResetToZero(); |
| 296 | |
| 297 | HRESULT hr; |
| 298 | hr = p_dele->DynamicInvoke(NULL, NULL); |
| 299 | if(FAILED(hr)) |
| 300 | return FALSE; |
| 301 | else |
| 302 | return Verify(expected, result); |
| 303 | } |
| 304 | |
| 305 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrByRefParam(_Delegate **pp_dele) |
| 306 | { |
| 307 | ResetToZero(); |
| 308 | |
| 309 | HRESULT hr; |
| 310 | hr = (*pp_dele)->DynamicInvoke(NULL, NULL); |
| 311 | if(FAILED(hr)) |
| 312 | { |
| 313 | return FALSE; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | *pp_dele = NULL; |
| 318 | return Verify(expected, result); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrByInValParam(_Delegate * p_dele) |
| 323 | { |
| 324 | ResetToZero(); |
| 325 | |
| 326 | HRESULT hr; |
| 327 | hr = p_dele->DynamicInvoke(NULL, NULL); |
| 328 | if(FAILED(hr)) |
| 329 | { |
| 330 | return FALSE; |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | return Verify(expected, result); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrByInRefParam(_Delegate **pp_dele) |
| 339 | { |
| 340 | ResetToZero(); |
| 341 | |
| 342 | HRESULT hr; |
| 343 | hr = (*pp_dele)->DynamicInvoke(NULL, NULL); |
| 344 | if(FAILED(hr)) |
| 345 | { |
| 346 | return FALSE; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | *pp_dele = NULL; |
| 351 | return Verify(expected, result); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrByOutValParam(_Delegate * p_dele) |
| 356 | { |
| 357 | ResetToZero(); |
| 358 | |
| 359 | HRESULT hr; |
| 360 | hr = p_dele->DynamicInvoke(NULL, NULL); |
| 361 | if(FAILED(hr)) |
| 362 | { |
| 363 | return FALSE; |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | p_dele = NULL; |
| 368 | return Verify(expected, result); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | //verification method |
| 373 | extern "C" DLL_EXPORT int STDMETHODCALLTYPE RetFieldResult1() |
| 374 | { |
| 375 | return result.result1; |
| 376 | } |
| 377 | |
| 378 | extern "C" DLL_EXPORT int STDMETHODCALLTYPE RetFieldResult2() |
| 379 | { |
| 380 | return result.result2; |
| 381 | } |
| 382 | |
| 383 | extern "C" DLL_EXPORT int STDMETHODCALLTYPE RetFieldResult3() |
| 384 | { |
| 385 | return result.result3; |
| 386 | } |
| 387 | |
| 388 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrByOutRefParam(_Delegate ** pp_dele, _Delegate * pdeleHelper) |
| 389 | { |
| 390 | printf("In Take_DelegatePtrByOutRefParam native side \n" ); |
| 391 | ResetToZero(); |
| 392 | |
| 393 | if( *pp_dele != NULL) |
| 394 | { |
| 395 | return FALSE; |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | *pp_dele = pdeleHelper; |
| 400 | (*pp_dele)->AddRef(); |
| 401 | return TRUE; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrByInOutValParam(_Delegate * p_dele) |
| 406 | { |
| 407 | ResetToZero(); |
| 408 | |
| 409 | HRESULT hr; |
| 410 | hr = p_dele->DynamicInvoke(NULL, NULL); |
| 411 | if(FAILED(hr)) |
| 412 | { |
| 413 | return FALSE; |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | return Verify(expected, result); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrByInOutRefParam(_Delegate **pp_dele) |
| 422 | { |
| 423 | ResetToZero(); |
| 424 | |
| 425 | HRESULT hr; |
| 426 | hr = (*pp_dele)->DynamicInvoke(NULL, NULL); |
| 427 | if(FAILED(hr)) |
| 428 | { |
| 429 | return FALSE; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | *pp_dele = NULL; |
| 434 | return Verify(expected, result); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | extern "C" DLL_EXPORT _Delegate* ReturnDelegatePtrByVal(_Delegate * pdeleHelper) |
| 439 | { |
| 440 | pdeleHelper->AddRef(); |
| 441 | return pdeleHelper; |
| 442 | } |
| 443 | |
| 444 | #endif |
| 445 | |