A friend and fellow MVP Jeff Arnett recently collaborated on a project to replace some DLL files in an existing NK.BIN file for testing.  Jeff tried to use BinMod.exe, but it didn’t work. When trying to extract or replace a DLL the following message was output to the console:
BinMod V1.0 built Apr 5 2006 11:32:47
 
Checking record #70 for potential TOC (ROMOFFSET = 0xFEC634B4)
Checking record #195 for potential TOC (ROMOFFSET = 0x00000000)
NOTICE! Record 195 looked like a TOC except DLL first = 0x4001C001, and DLL last = 0x4152C0A1
Error: File not found in image
Error: Extraction failed!
I did find with some further testing that this doesn’t happen with all files, but some.  It depends on the address of the DLL.  With some investigation we found that a bug in BinMod that causes this.
The following are the steps we followed to modify BinMod so that it could replace DLLs in the FILES section of an NK.BIN:
Build BinMod:
1.       Create empty console project in Visual Studio
2.       Copy source from C:\WINCE600\PRIVATE\WINCEOS\COREOS\NK\TOOLS\ROMIMAGE\BINMOD
3.       Add source to project
4.       In cbinmod.cpp change:
     #include "..\compress\compress.h"
to:
    #include "compress.h"
5.       Add the following directories to the include path:
     C:\WINCE600\PUBLIC\COMMON\OAK\INC
     C:\WINCE600\PRIVATE\WINCEOS\COREOS\NK\TOOLS\ROMIMAGE\COMPRESS
6.     Set the Character Set to “Not Set”
Now that we can build binmod, we can modify it so that it can actually modify a bin file. The following steps will accomplish that:
1.       Modify lines 809 and 810 in cbinmod.cpp from:
               if((DWORD)(HIWORD(pTOCLoc->dllfirst) << 16) <= pTOCLoc->dlllast &&
                  (DWORD)(LOWORD(pTOCLoc->dllfirst) << 16) <= pTOCLoc->dlllast)
to:
              if((DWORD)pTOCLoc->dllfirst <= (DWORD)pTOCLoc->dlllast )
2.       Build the project
The new BinMod needs to load compress.dll from C:\program files\microsoft platform builder\6.00\cepb\idevs\imgutils, so that folder needs to be in your PATH. The easiest way to have it in your path is to use a Platform Builder build window when you test your BinMod.
Examples of using BinMod:
Extracting a DLL:
>>c:\SRC\binmod\debug\mybinmod.exe -i nk.bin -e amdnord.dll
BinMod V1.0 built May 16 2009 19:17:32
 
Checking record #70 for potential TOC (ROMOFFSET = 0xFEC634B4)
Checking record #195 for potential TOC (ROMOFFSET = 0x00000000)
Found pTOC = 0x8195fab4
rom offset = 0x00000000
Found 'amdnord.dll' in the image! - located at 819576b4, size 00008400 uncompressed
File extracted!
 
Replacing a DLL:
 
>>c:\SRC\binmod\debug\mybinmod.exe -i nk.bin -r amdnord.dll
BinMod V1.0 built May 16 2009 19:17:32
 
Checking record #70 for potential TOC (ROMOFFSET = 0xFEC634B4)
Checking record #195 for potential TOC (ROMOFFSET = 0x00000000)
Found pTOC = 0x8195fab4
rom offset = 0x00000000
Found 'amdnord.dll' in the image! - located at 819576b4, size 00008400 uncompressed
New file uncompressed = 00008400
Original record checksum: 0x2d7268
New record checksum: 0x2d7268
New data written to image!
Original record checksum: 0xd7f22
New record checksum: 0xd7f22
TOC updated!
Done!
Copyright © 2009 – Bruce Eitman
All Rights Reserved