[C#] An attempt was made to load a program with an incorrect format. (Exception from

Discussion in 'Mixed Languages' started by FreeStyler, Aug 6, 2012.

  1. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #1 FreeStyler, Aug 6, 2012
    Last edited: Aug 6, 2012
    I have a C# project with quite a few embedded resources, file size got rather big so i thought to compress it a bit using mpress.
    These embedded resources are extracted to a temporary location when the program is run.
    Unfortunately now i always get this error message: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

    if i inspect the extracted resources with originals the are 100% binary the same (same hashes)

    Ideas?
     
  2. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,034
    1,061
    60
    are you using the latest mpress 2.19 ?

    when running the exe and getting the error what OS and Arch are you using?
    have you tried under a different OS and/or Arch to see if same error occurs?
     
  3. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #3 FreeStyler, Aug 6, 2012
    Last edited: Aug 6, 2012
    (OP)
    Yes, latest 2.19
    Application is written as x86 executable, development is done on a x64 box though...

    gonna try compressing under x86

    [EDIT]
    Same problem :confused:
     
  4. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,858
    2,112
    60
    #4 Muerto, Aug 6, 2012
    Last edited: Aug 6, 2012
    What IDE, Visual Studio?

    Clean the project and go to Bin/Release - Clean out anything unnecessary in there, then rebuild > use mpress

    If you're using third party dll's, what's the architecture of them?
     
  5. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    Visual Studio 2010, how can i determine what architecture the 3th party dll is in? (@user_hidden, it is ProductKeyUtilities.dll from new VAMT)
     
  6. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,171
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,858
    2,112
    60
    You can use the CorFlags CLI tool to look at the header if the .dll. I'd link you but I'm on my phone.

    Googling 'dll architecture coreflags' should give you a decent enough solution.
     
  8. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #8 FreeStyler, Aug 7, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Ok, thx...guess this explain it reason for failing, right?

    Code:
    Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  3.5.30729.1
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    corflags : error CF008 : The specified file does not have a valid managed header
     
  9. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,034
    1,061
    60
    I haven't played around with it yet.
    When I sit down hopefully this afternoon or evening I'll get back to you.


     
  10. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,858
    2,112
    60
    #10 Muerto, Aug 7, 2012
    Last edited by a moderator: Apr 20, 2017
    Hmm, not neccesarily, I'm not sure whether CorFlags is just for .net to be honest.

    What dlls are they, do you know their origin? Do they work when you don't use mpress?
     
  11. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,034
    1,061
    60
    Freestyler,

    I finally sat down and updated my PID checker.
    Yes I am using the new resources from VAMT3 as you are in you app with the issue.
    I compiled the exe .... compressed with mpress
    Executable works perfect and no errors when loading the DLL (6.2.9200.16384)

    I am compiling for dotNet2....what framework is your code compiled for?
     
  12. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #12 FreeStyler, Aug 7, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    origin Microsoft, without mpress it runs fine


    @user_hidden
    .NET 2.x as well

    Besides the dll's I also packed up the 3 Windows pkconfig's files... but doubt these are the issue

    [​IMG]

    On program start the dll is extracted from embedded resources to:
    %APPDATA%\Roaming\KeyChecker

    The pkconfig's are extracted to the same directory, little later, eg: depending on the win version they checked with radiobutton



    Code:
    ************** Exception Text **************
    System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
       at KeyChecker.KeyChecker.PidGenX(String ProductKey, String PkeyPath, String MSPID, String OEMID, IntPtr ProductID, IntPtr DigitalProductID, IntPtr DigitalProductID4)
       at KeyChecker.KeyChecker.CheckProductKey(String ProductKey)
       at KeyChecker.KeyChecker.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
    
    You compiled it for x86 or AnyCpu?
     
  13. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,858
    2,112
    60
    I think you need to look away from mpress. CorFlags won't of worked as M$ native dlls won't be a .net assembly.

    I compile and compress everything with SmartAssemly. Never had an issue. You can also obfuscate etc.
     
  14. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,034
    1,061
    60
    yes in my checker i have these in my resources:
    PidGenX.dll
    pkeyconfig-embedded.xrm-ms
    pkeyconfig-exchange.xrm-ms
    pkeyconfig-office14.xrm-ms
    pkeyconfig-office15.xrm-ms
    pkeyconfig-officeweb.xrm-ms
    pkeyconfig-vista.xrm-ms
    pkeyconfig-weposr.xrm-ms
    pkeyconfig-wes7.xrm-ms
    pkeyconfig-Wes8.xrm-ms
    pkeyconfig-Wes8csvlk.xrm-ms
    pkeyconfig-win7.xrm-ms
    pkeyconfig-Win8.xrm-ms
    pkeyconfig-Win8csvlk.xrm-ms
    pkeyconfig-wssewhs.xrm-ms

    all my resources are extracted to "My.Computer.FileSystem.SpecialDirectories.Temp"


    AnyCpu
     
  15. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,280
    210
    #15 Alphawaves, Aug 8, 2012
    Last edited: Aug 8, 2012
    FS does your app run fine before mpressing it ?
    Also your added resources, can they not be coded into c# rather than 3rd party apps ?
    mpress is a dodgy compressor IMHO, i wouldn't use it. :eek:
    Also maybe compile as AnyCpu?
     
  16. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,858
    2,112
    60
    Yeah his application works fine without mpress, I did suggest SmartAssembly, the only compression I use is in there.
     
  17. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    #17 FreeStyler, Aug 9, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Plz, anyone can confirm 'ProductKeyUtilities.dll' as in VAMT 3.0 can not be used on a 64-bit OS when Visual Studio project is configured to compile as AnyCPU or x64?

    Code:
    [DllImport("ProductKeyUtilities.dll", EntryPoint = "PidGenX", CharSet = CharSet.Auto)]
    static extern int PidGenX(string ProductKey, string PkeyPath, string MSPID, string OEMID, IntPtr ProductID, IntPtr DigitalProductID, IntPtr DigitalProductID4);
    private string CheckProductKey(string ProductKey)
    {
    string result = "";
    try
    {
    int RetID;
    byte[] gpid = new byte[0x32];
    byte[] opid = new byte[0xA4];
    byte[] npid = new byte[0x04F8];
    
    IntPtr PID = Marshal.AllocHGlobal(0x32);
    IntPtr DPID = Marshal.AllocHGlobal(0xA4);
    IntPtr DPID4 = Marshal.AllocHGlobal(0x04F8);
    
    string PKeyPath = configFile;
    string MSPID = "XXXXX";
    string OEMID = null;
    gpid[0] = 0x32;
    opid[0] = 0xA4;
    npid[0] = 0xF8;
    npid[1] = 0x04;
    
    Marshal.Copy(gpid, 0, PID, 0x32);
    Marshal.Copy(opid, 0, DPID, 0xA4);
    Marshal.Copy(npid, 0, DPID4, 0x04F8);
    
    RetID = PidGenX(ProductKey, PKeyPath, MSPID, OEMID, PID, DPID, DPID4);
    
    switch (RetID)
    {
    case 0:
    Marshal.Copy(PID, gpid, 0, gpid.Length);
    Marshal.Copy(DPID4, npid, 0, npid.Length);
    string pid = GetString(gpid, 0x0000);
    string eid = GetString(npid, 0x0008);
    string aid = GetString(npid, 0x0088);
    string edi = GetString(npid, 0x0118);
    string sub = GetString(npid, 0x0378);
    string lit = GetString(npid, 0x03F8);
    string lic = GetString(npid, 0x0478);
    string cid = GetCryptoID(PKeyPath, "{" + aid + "}").Trim();
    string dsc = GetProductDescription(PKeyPath, "{" + aid + "}", edi).Trim();
    result += "Product Key\t: " + ProductKey + Environment.NewLine;
    result += "Validity\t\t: Valid" + Environment.NewLine;
    result += "Product ID\t: " + pid + Environment.NewLine;
    //Extended PID diclosed unneeded information about the users OS (eg: Chosen Regionial Settings)
    //result += "Extended PID\t: " + eid + Environment.NewLine;
    result += "Activation ID\t: " + aid + Environment.NewLine;
    result += "Edition Type\t: " + edi + Environment.NewLine;
    result += "Description\t: " + dsc + Environment.NewLine;
    result += "Edition ID\t\t: " + sub + Environment.NewLine;
    result += "Key Type\t\t: " + lit + Environment.NewLine;
    //License Channel is obvious, already disclosed by Key Type value
    //result += "License Channel\t: " + lic + Environment.NewLine;
    result += "Crypto ID\t\t: " + cid;
    break;
    case -2147024809:
    result += "error\t\t: Invalid Arguments";
    break;
    case -1979645695:
    case -1979645951:
    result += "Product Key\t: " + ProductKey + Environment.NewLine;
    result += "Validity\t\t: Invalid" + Environment.NewLine;
    break;
    case -2147024894:
    result += "error\t\t: pkeyconfig.xrm.ms file is not found";
    break;
    default:
    result += "error\t\t: Invalid input!!!";
    break;
    }
    Marshal.FreeHGlobal(PID);
    Marshal.FreeHGlobal(DPID);
    Marshal.FreeHGlobal(DPID4); 
    }
    catch (Exception ex)
    {
    result = ex.Message;
    }
    return result;
    }
    
    
    static string GetProductDescription(string pkey, string aid, string edi)
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(pkey);
    MemoryStream stream = new MemoryStream(Convert.FromBase64String(doc.GetElementsByTagName("tm:infoBin")[0].InnerText));
    doc.Load(stream);
    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
    ns.AddNamespace("pkc", "http://www.microsoft.com/DRM/PKEY/Configuration/2.0");
    try
    {
    XmlNode node = doc.SelectSingleNode("/pkc:ProductKeyConfiguration/pkc:Configurations/pkc:Configuration[pkc:ActConfigId='" + aid + "']", ns);
    if (node == null)
    {
    node = doc.SelectSingleNode("/pkc:ProductKeyConfiguration/pkc:Configurations/pkc:Configuration[pkc:ActConfigId='" + aid.ToUpper() + "']", ns);
    }
    if (node.HasChildNodes)
    {
    if (node.ChildNodes[2].InnerText.Contains(edi))
    {
    return node.ChildNodes[3].InnerText;
    }
    return "Not Found";
    }
    return "Not Found";
    }
    catch (Exception)
    {
    return "Not Found";
    }
    finally
    {
    stream.Dispose();
    }
    }
    
    
    static string GetCryptoID(string pkey, string aid)
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(pkey);
    MemoryStream stream = new MemoryStream(Convert.FromBase64String(doc.GetElementsByTagName("tm:infoBin")[0].InnerText));
    doc.Load(stream);
    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
    ns.AddNamespace("pkc", "http://www.microsoft.com/DRM/PKEY/Configuration/2.0");
    try
    {
    XmlNode node = doc.SelectSingleNode("/pkc:ProductKeyConfiguration/pkc:Configurations/pkc:Configuration[pkc:ActConfigId='" + aid + "']", ns);
    if (node == null)
    {
    node = doc.SelectSingleNode("/pkc:ProductKeyConfiguration/pkc:Configurations/pkc:Configuration[pkc:ActConfigId='" + aid.ToUpper() + "']", ns);
    }
    if (node.HasChildNodes)
    {
    return node.ChildNodes[1].InnerText;
    }
    return "Not Found";
    }
    catch (Exception)
    {
    return "Not Found";
    }
    finally
    {
    stream.Dispose();
    }
    }
    
    private static string GetString(byte[] bytes, int index)
    {
    int n = index;
    while (!(bytes[n] == 0 && bytes[n + 1] == 0)) n++;
    return Encoding.ASCII.GetString(bytes, index, n - index).Replace("\0", "");
    }
    
     
  18. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
    Still got issues with this using mpress, SmartAssemly works just fine so i start using that ;)