dmColor and dmDuplex values of the DEVMODE structure always returning 0

Asked

Viewed 84 times

1

I am doing a C# printing monitoring job and using the winspool API. I was able to capture much of the data I need, such as number of pages, printed document, number of printed pages, user who printed, etc. Using the structure JOB_INFO_2.

That contains a pointer to a structure DEVMODE where I should get information such as, number of copies(dmCopies), whether the print is color or P&B(dmColor), and whether it is a Duplex print(dmDuplex).

I was able to access this structure and some information is right, for example dmCopies always returns me the correct amount of copies sent for printing, but dmColor that should be 1 for COLOR and 2 for MONOCHROME always returns me 0. The same happens with dmDuplex. It should be 1 for SIMPLEX, 2 for HORIZONTAL and 3 for VERTICAL.

I am doing something wrong by accessing DEVMODE?

public static JOB_INFO_2[] getJobsAPI(IntPtr _printerHandle)
    {

        //impressora = identificador(handle)
        var hPrinter = _printerHandle;

        /* Configurações de busca do job em EnumJobs */
        uint firstJob = 0;
        const uint noJobs = 99;
        const uint level = 2;

        // obtem o tamanho em bytes requeridos para a função 
        uint needed;
        uint returned;
        bool b1 = EnumJobsW(hPrinter, firstJob, noJobs, level, IntPtr.Zero, 0, out needed, out returned);
        uint lastError = GetLastError();

        //aloca memoria e popula as estruturas
        IntPtr pJob = Marshal.AllocHGlobal((int)needed);
        uint bytesCopied;
        uint structsCopied;
        bool b2 = EnumJobsW(hPrinter, firstJob, noJobs, level, pJob, needed, out bytesCopied, out structsCopied);
        var jobInfos = new JOB_INFO_2[structsCopied];
        int sizeOf = Marshal.SizeOf(typeof(JOB_INFO_2));
        IntPtr pStruct = pJob;

        for (int i = 0; i < structsCopied; i++)
        {
            var jobInfo = (JOB_INFO_2)Marshal.PtrToStructure(pStruct, typeof(JOB_INFO_2));
            jobInfos[i] = jobInfo;

            //acessa estrutura DEVMODE
            IntPtr pDevMode = jobInfos[i].pDevMode;
            DEVMODE devMode = (DEVMODE)Marshal.PtrToStructure(pDevMode, typeof(DEVMODE));
            pStruct += sizeOf;
        }


        Marshal.FreeHGlobal(pJob);
        return jobInfos;
    }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.