This project is read-only.

Is CCI compilable for Compact Framework?

Topics: PE reader
May 4, 2009 at 12:43 PM

Is it possible to compile the CCI assemblies for the Compact Framework? I tried to buid and run the FindGeneric on the Windows Mobile 6 Emulator, but it stops without error when reading a PE file (mscorlib.dll from the .Net CF). To do so, I compiled the MetadataModel, MetadataHelper, PeReader and FindGeneric projects with the COMPACTFX and LITLLEENDIAN definitions (using BIGENDIAN doesn't compile...).

Thanks a lot in advance for your help.


May 5, 2009 at 4:19 AM
The pe reader at some stage did compile on the compact framework, hence the conditional compilation directives. It is not currently being built for the CF on a regular basis and it is thus possible/likely that changes have crept in that prevent it from compiling.

In principal there is no reason why these problems can't be fixed. I'll look into it when I get time.
May 5, 2009 at 9:56 AM
Thanks your for your reply.

After investigating further, I think I have found the problem: at a given position while reading the input PE file, the program tries to read an int32 on a non-DWORD aligned pointer, and it makes it crash!

Here is a simplified version to illustrate the problem:

 int i;
fixed (byte* p = array)
                byte* p1 = p;       << OK DWORD-aligned
                i = *(int*)p1;

                byte* p2 = p + 4;      << OK DWORD-aligned
                i = *(int*)p2;

                byte* p3 = p + 6;      << not DWORD-aligned!!!
                i = *(int*)p3;            << the test program crashes here!!! >>

Now I have no idea how to solve this problem, as the input PE file (the CF mscorlib.dll) is perfectly correct, but the method used to read it on a device doesn't work...

Have you an idea?

May 5, 2009 at 10:15 AM
Finally, I have found a solution: using the Marshal static methods to read Int32 values. The following program works well:

fixed (byte* p = array)
                byte* p1 = p;
                i = Marshal.ReadInt32(new IntPtr(p1));

                byte* p2 = p + 4;
                i = Marshal.ReadInt32(new IntPtr(p2));

                byte* p3 = p + 6;
                i = Marshal.ReadInt32(new IntPtr(p3));

But how to avoid the creation of IntPtr ?...
May 7, 2009 at 9:50 AM
You can work around this by reading eight bytes and discarding the bytes that you dont need. Something like this (just brainstorming here):

    byte* p3 = p + 6;      << not DWORD-aligned!!!
    i = *(int*)p3;

    byte* p3 = p + 4;               << aligned
    ulong temp = *(ulong*)p3;  << read 8 bytes
    i = (int)(temp >> 16);         << probably need to make this unchecked

You may be bitten by endianness. I'm not sure what the original code is doing, but if the array comes from disk then this may not work on an embedded device.
May 10, 2009 at 8:13 PM

I have fixed the compilation errors that have crept in over time and I've added a solution and some projects for the Compact Framework. I'll see if I get time next week to actually run something on a CF device.

You should define the BIGENDIAN constant for big endian machines. It now compiles and should take care of all of the byte ordering issues discussed above.

Aug 31, 2009 at 3:57 PM

I can't find any correction that would take care of byte ordering for COMPACTFX.... Is it available on CodePlex ?

Aug 31, 2009 at 6:23 PM

Look for the projects named PeReaderCF.csproj, MetadataModelCF.csproj and MetadataHelperCF.csproj.

Sep 1, 2009 at 7:58 AM

Great ! Thanks a lot !!!


Sep 1, 2009 at 12:36 PM

I am sorry, but I can't manage to run the sample FindGeneric on the device emulator with CF 3.5, after compiling the *CF.csproj you have created... The program still fails while reading a PE file.

In fact, I can't find in the PeReader source code any code that manage the problem of byte ordering, and that's why the error persists...



Sep 1, 2009 at 3:43 PM

Look for code that is condtionally compiled only if the BIGENDIAN constant is defined. This code lives only in the UnmanagedReadWrite.cs file in the PeReader directory. The constant is defined by default in the Debug configuration of the PeReaderCF project (but I see that I've forgotten to define it for the Release configuration).

At some stage, the code did run on an emulator, so it should be possible to get it to run again, supposing that bit rot has set in since then.

Sep 8, 2009 at 1:13 PM

I have successfully modified the UnmanagedReadWrite.cs in order to manage the misalignement problem on Compact Framework !

I can send you the modified code, and a screen copy of the the FindGenericCF program running correctly on Windows Mobile, but I don't know how to do that.

Can you give me your email to post these files ?


Thanks a lot in advance !



Sep 8, 2009 at 6:26 PM

My e-mail is

There are some legal niceties that need to be observed when contributing code. Please see for details.

If the modification amounts to a few lines of code and represents a straightforward fix to buggy code, you can simply create an issue and paste the before and after versions of the code in the issue.