10

All my code is under source control, so I'm 100% sure that the source code hasn't changed. But if I build a C# DLL two times their content is slightly different. I can reproduce the problem 100% of the time by just building, and then building again.

This doesn't seem to impact the program at all, but tools like MSIMSP, used for creating patches from two MSI files are thrown off by these minute changes. Making patches (for my product) 40x bigger than they should be.

I've decompiled both DLLs and their assembly information, classes, etc... are exactly the same. The files are also exactly the same size, but of course have a different creation time. So I really cannot fathom what has changed.

So I dug a little deeper.

I've used WinDiff to find the changes and then cross referenced these in a hex editor. WinDiff shows a change in the second 'line' and in a line at about 80% of the file.

In the hex editor I see that the first byte that is changed is byte 0x088 (byte 136). This seems to be the only byte changed on this 'line'. I trouble finding the second change as WinDiff doesn't tell me the exact byte offset of the change.

Here is an image of the changes, the right file is the contents of the newer file. Hex view

Does anybody familiar with the make-up of (C#) DLL files know what the changed byte might mean? Or better yet how to make sure that DLL files stay exactly the same when you rebuild them?

Roy T.
  • 9,429
  • 2
  • 48
  • 70
  • Sometimes there is a file named $RANDOM_SEED$ in my bin/release folder, and the content of that file changes every time I rebuild. I have not gone into detail, but I can imagine that being one of the two changes. Someone will need to do some reserch. **EDIT:** (its nunit, probably has no influence into the dll, so forget my comment) http://stackoverflow.com/a/40382757/5962841 – Mafii Dec 02 '16 at 11:03
  • It may be worth your while to try out a good text/hex editor like Vedit. I've been using it since 1989. It is a good tool to have at your disposal even in these IDE & intellisense days. www.vedit.com – Michael Gorsich Dec 02 '16 at 11:06
  • 3
    [How can I tell whether two Net DLLs are the same](http://stackoverflow.com/questions/2735643/how-can-i-tell-whether-two-net-dlls-are-the-same) seems to approach the same issue from a different angle. – Damien_The_Unbeliever Dec 02 '16 at 11:06
  • 1
    http://stackoverflow.com/questions/8927558/why-is-the-binary-output-not-equal-when-compiling-again – Matteo Umili Dec 02 '16 at 11:07
  • @Matteo's link should clear it up! – TaW Dec 02 '16 at 11:19
  • It is [IMAGE_FILE_HEADER.TimeDateStamp](https://msdn.microsoft.com/en-us/library/windows/desktop/ms680313(v=vs.85).aspx). – Hans Passant Dec 02 '16 at 12:45

2 Answers2

5

This might have something to do with differences between Build and a ReBuild

Difference between Build Solution, Rebuild Solution, and Clean Solution in Visual Studio?

if it doesn't think it needs to rebuild a project, it won't. It may also use partially-built bits of the project if they haven't changed

Performing a build uses metadata behind the scenes and thus your dll's can be different

Why is a different dll produced after a clean build, with no code changes?

SOLUTION:

You will need to perform a deterministic build as described here

Community
  • 1
  • 1
Fuzzybear
  • 1,388
  • 2
  • 25
  • 42
  • Ye but building multiple times should produce the exact same binaries everytime, no? – Mafii Dec 02 '16 at 11:08
  • 1
    @Mafii Unfortunately there is always something going on behind the scenes... metadata it looks like http://stackoverflow.com/questions/107196/why-is-a-different-dll-produced-after-a-clean-build-with-no-code-changes so No they would not produce the same binaries – Fuzzybear Dec 02 '16 at 11:11
  • @Fuzzybear any way to ignore those metadata bytes for creating patches? (Maybe I should ask that as a separate question) – Roy T. Dec 02 '16 at 11:14
  • @Roy T Not that I am aware of sorry, I would try a rebuild over a build and if that did not work then yes I would be asking how to strip out the metadata etc... might be another compiler that allows you to do what you need maybe? – Fuzzybear Dec 02 '16 at 11:21
  • 1
    @FuzzyBear I stumbled upon http://blog.paranoidcoding.com/2016/04/05/deterministic-builds-in-roslyn.html which says there is a /deterministic build parameter for Roslyn. Hope that works :D – Roy T. Dec 02 '16 at 12:06
  • Amazing find @RoyT. and his first line : "It seems silly to celebrate features which should have been there from the start" going to add link to my answer – Fuzzybear Dec 02 '16 at 12:15
0

If you have made changes to the dll then the size will be affected I added a few extra properties to an enumeration and the file became about 3kb larger. It could also be an unseen change due to decompression which causes the file to appear different.

Nemavhidi
  • 108
  • 8