2

I want to compare 2 bytes array using memcmp function using below code

[DllImport("msvcrt.dll",EntryPoint = "memcmp", CallingConvention = CallingConvention.Cdecl)]
  static extern int memcmp(byte[] b1, byte[] b2, long count);

When I run my application on Windows it is working fine. But when I run it on Linux it giving below exception

Unable to load shared library 'msvcrt.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libmsvcrt.dll: cannot open shared object file: No such file or directory

Below is the docker file

FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
COPY NuGet.Config ./
RUN dotnet restore

# copy everything else and build
COPY . ./

RUN dotnet publish -c Release -o out

# build runtime image
FROM microsoft/dotnet:2.2-runtime

WORKDIR /app
COPY --from=build-env /app/out .

ENTRYPOINT ["dotnet", "XXX.dll", "YYY.dll"]

Please let me know what should I use?

TylerH
  • 20,799
  • 66
  • 75
  • 101

2 Answers2

2

You cannot just use a 'windows' DLL on Linux. Just because you have a .NET Core runtime on Linux doesn't mean you can use other libraries / executables from the other platform/OS.

You should find some other code that can compare two byte arrays or stick to windows as the underlying OS.

Take a look here: Comparing two byte arrays in .NET

Rick
  • 3,361
  • 1
  • 22
  • 29
  • I have used foor loop, sequence equal, structural equal and finally i choose memcmp which gives good performance as compared to all. Could someone tell how I can i use equivalent dll for linux. I think libc.so can be use but dont know how to implement it in code. Is it some thing to add in docker file? – vaishali khatri Sep 20 '19 at 08:33
  • @vaishalikhatri With the knowledge gained, I'd say you could compose a new SO question when stuck. – Rick Sep 20 '19 at 08:36
  • You can PInvoke the libc.so library. Check https://stackoverflow.com/a/42118816/2265446 – Cleptus Sep 20 '19 at 10:53
0

@Damien_The_Unbeliever. Thanks for your help. I used below code

 if (((ReadOnlySpan<byte>)slice).SequenceCompareTo((ReadOnlySpan<byte>)masterSlice) == 0)
                    isEqual = true;