Skip to content

Commit 42b6f2e

Browse files
committed
Fix libc importing cause .NET Native compliation error. issue #41
1 parent 0c8faae commit 42b6f2e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/MsgPack/UnsafeNativeMethods.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2012 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -46,11 +46,15 @@ internal static class UnsafeNativeMethods
4646
[DllImport( "msvcrt", CallingConvention = CallingConvention.Cdecl, EntryPoint = "memcmp", ExactSpelling = true, SetLastError = false )]
4747
private static extern int memcmpVC( byte[] s1, byte[] s2, /*SIZE_T*/UIntPtr size );
4848

49+
#if !NETFX_CORE
50+
// libc is for non Windows environment.
51+
// Note that libc caused compilation error on .NET Native, so the DllImport itself should not be included in the first time.
4952
#if NETFX_35
5053
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule", Justification = "OK, this is SecurityCritical" )]
5154
#endif // NETFX_35
5255
[DllImport( "libc", CallingConvention = CallingConvention.Cdecl, EntryPoint = "memcmp", ExactSpelling = true, SetLastError = false )]
5356
private static extern int memcmpLibC( byte[] s1, byte[] s2, /*SIZE_T*/UIntPtr size );
57+
#endif // !NETFX_CORE
5458

5559
public static bool TryMemCmp( byte[] s1, byte[] s2, /*SIZE_T*/UIntPtr size, out int result )
5660
{
@@ -69,10 +73,15 @@ public static bool TryMemCmp( byte[] s1, byte[] s2, /*SIZE_T*/UIntPtr size, out
6973
}
7074
catch ( DllNotFoundException )
7175
{
76+
#if !NETFX_CORE
7277
Interlocked.Exchange( ref _libCAvailability, _libCAvailability_LibC );
78+
#else
79+
Interlocked.Exchange( ref _libCAvailability, _libCAvailability_None );
80+
#endif // !NETFX_CORE
7381
}
7482
}
7583

84+
#if !NETFX_CORE
7685
if ( _libCAvailability <= _libCAvailability_LibC )
7786
{
7887
try
@@ -85,6 +94,7 @@ public static bool TryMemCmp( byte[] s1, byte[] s2, /*SIZE_T*/UIntPtr size, out
8594
Interlocked.Exchange( ref _libCAvailability, _libCAvailability_None );
8695
}
8796
}
97+
#endif // !NETFX_CORE
8898

8999
result = 0;
90100
return false;

0 commit comments

Comments
 (0)