Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I was trying to read emirates id with card reader following this blog href="https://www.codeproject.com/Articles/1265249/Read-Emirates-ID-in-a-Web-Application">Read Emirates ID in a Web Application. However I was getting an error
"An attempt was made to load a program with an incorrect format" while running the console application at line "readerManager.EstablishContext()".
I tried all the solutions in internet but nothing helped me . Could anyone help me please . Any help will be really appreciated . Thanks in advance.

What I have tried:

program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace EIDSampleConsole
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var model = Controller.CreateFromEIDReader();
            if (model.HasData)
            {
                Clipboard.SetText(Newtonsoft.Json.JsonConvert.SerializeObject(model));
            }
            else
                Clipboard.SetText("NO DATA");
        }
    }
}


controller.cs

using EmiratesId.AE.PublicData;
using EmiratesId.AE.ReadersMgt;
using EmiratesId.AE.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EIDSampleConsole
{
    public class Controller
    {
        public static EIDModel CreateFromEIDReader()
        {
            EIDModel model = new EIDModel();
            ReaderManagement readerManager;
            PCSCReader reader;
            try
            {
                
                readerManager = new ReaderManagement();
                readerManager.EstablishContext();
                readerManager.DiscoverReaders();
                //System.Windows.Forms.MessageBox.Show("after reader discover");
                if (readerManager.Readers.Length > 0)
                {
                    reader = readerManager.Readers[0];
                    if (reader.IsConnected())
                    {
                        //System.Windows.Forms.MessageBox.Show("after connection to reader, before getting data");
                        PublicDataFacade pDataFacade = reader.GetPublicDataFacade();
                        CardHolderPublicData pData = pDataFacade.ReadPublicData(true, true, true, true, false);
                        CardHolderPublicDataEx pDataEx = pDataFacade.ReadPublicDataEx(true, false, true, true, false, true, true, true);

                        model.EIDNumber = Utils.ByteArrayToUTF8String(pData.IdNumber);
                        model.Phone = Utils.ByteArrayToUTF8String(pDataEx.HomeAddress.ResidentPhoneNumber);
                        model.Mobile = Utils.ByteArrayToUTF8String(pDataEx.HomeAddress.MobilePhoneNumber);
                        model.Email = Utils.ByteArrayToUTF8String(pDataEx.HomeAddress.Email);
                        model.Pobox = Utils.ByteArrayToUTF8String(pDataEx.HomeAddress.POBox);
                        model.Emirate = Utils.ByteArrayToUTF8String(pDataEx.HomeAddress.EmirateDescriptionEnglish);
                        model.City = Utils.ByteArrayToUTF8String(pDataEx.HomeAddress.CityDescriptionEnglish);
                        model.Area = Utils.ByteArrayToUTF8String(pDataEx.HomeAddress.AreaDescriptionEnglish);

                        model.Sex = Utils.ByteArrayToUTF8String(pData.Sex);
                        model.Occupation = Utils.ByteArrayToUTF8String(pData.Occupation);
                        model.Occupation = Utils.ByteArrayToUTF8String(pDataEx.OccupationEnglish);
                        model.Occupation = Utils.ByteArrayToUTF8String(pDataEx.OccupationArabic);
                        model.Occupation = Utils.ByteArrayToUTF8String(pDataEx.OccupationTypeEnglish);
                        model.Occupation = Utils.ByteArrayToUTF8String(pDataEx.OccupationTypeArabic);

                        model.ResidencyType = Utils.ByteArrayToHex(pData.ResidencyType, "");
                        model.DOB = Utils.ByteArrayToStringDate(pData.DateOfBirth).Replace("/", "-");

                        model.ResidencyExpiry = Utils.ByteArrayToStringDate(pData.ResidencyExpiryDate);
                        model.Name = RemoveComma(Utils.ByteArrayToUTF8String(pData.FullName));
                        model.NameAr = RemoveComma(Utils.ByteArrayToUTF8String(pData.ArabicFullName));

                        model.Title = Utils.ByteArrayToUTF8String(pData.Title);
                        model.TitleAr = Utils.ByteArrayToUTF8String(pData.ArabicTitle);
                        model.NationalityID = (Utils.ByteArrayToUTF8String(pData.Nationality));
                        model.PassportNumber = Utils.ByteArrayToUTF8String(pDataEx.PassportNumber);

                        model.SponsorType = Utils.ByteArrayToHex(pData.SponsorType, "");
                        model.SponsorNumber = Utils.ByteArrayToHex(pData.SponsorNumber, "");
                        model.SponsorName = Utils.ByteArrayToUTF8String(pData.SponsorName);
                        model.CompanyName = Utils.ByteArrayToUTF8String(pDataEx.CompanyNameEnglish);
                        if (pData.ResidencyNumber != null)
                            model.ResidencyNumber = Utils.ByteArrayToUTF8String(pData.ResidencyNumber).Trim();

                        model.PhotoPath = @"C:\Temp\EID_PHO_" + model.EIDNumber.Replace(" ", "").Trim() + ".jpg";
                        model.Photo = pData.Photography;
                        if (pData.Photography != null)
                        {
                            System.IO.MemoryStream ms = new System.IO.MemoryStream(pData.Photography);
                            System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                            image.Save(model.PhotoPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }

                        model.Signature = pDataEx.HolderSignatureImage;
                        if (pDataEx.HolderSignatureImage != null)
                        {
                            System.IO.MemoryStream ms = new System.IO.MemoryStream(pDataEx.HolderSignatureImage);
                            System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                            image.Save(@"C:\Temp\EID_SIG_" + model.EIDNumber.Replace(" ", "").Trim() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        model.HasData = true;
                    }

                }

            }
            catch (Exception ex)
            {
                model.HasData = false;
                Console.WriteLine("ERROR: " + ex.Message);
            }
            return model;
        }
        public static string RemoveComma(string value)
        {
            string result = "";
            string[] valuelist = value.Split(new Char[] { ',' });
            foreach (string v in valuelist)
            {
                if (v.Trim().Length > 0)
                    result += v.Trim() + " ";
            }
            return result.Trim();
        }
    }
}
Posted
Updated 3-Nov-19 22:43pm
v2
Comments
Richard Deeming 4-Nov-19 4:37am    
That error usually suggests you're trying to load a 32-bit assembly in a 64-bit application, or vice-versa.
F-ES Sitecore 4-Nov-19 4:43am    
Have you googled the error and gone through the usual causes of this problem?
Member 12926744 4-Nov-19 5:10am    
Tried everything on google but nothing could solve the problem .
Richard MacCutchan 4-Nov-19 6:14am    
Try the message forum at the end of the article. Maybe the author can help.

1 solution

Check your application settings: if your app runs as a 32 bit app, it can't load 64 bit EXE or DLL assemblies, and you will get the error: equally, you will get the same problem trying to load a 32 bit assembly into a 64 bit app.

Look at the tool bar at the top of the VS page: beside the "Debug / Release" drop down is the "Platforms" selector - try changing that via the "Environment manager" drop down selection. If you find a version that works, you can then check if a different version is available for your prefered platform.
 
Share this answer
 
Comments
Member 12926744 4-Nov-19 5:09am    
I tried changing to X64 , x86 ,Any Cpu.But nothing helps .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900