locked
Structure Conversion - vshost.exe killed RRS feed

  • Question

  • I use this pack of code:

                if (nBytesNeeded > 0) //data exists (about 1700 bytes)
                {
                    ptrJobInfo = Marshal.AllocHGlobal(nBytesNeeded);

                    bRet = WinPrint.GetJob(hPrinter, job.JobID, 2, ptrJobInfo, nBytesNeeded, out nBytesNeeded);

                    if (true == bRet)
                    {
                        JOB_INFO_2 pinfo;
                        pinfo = (JOB_INFO_2)Marshal.PtrToStructure(ptrJobInfo, typeof(JOB_INFO_2));

                        ChargeUser(job); // throw exception if error

                        //change jop priority and position in queue
                        pinfo.Priority = WinPrint.MAX_JOB_PRIORITY;
                        pinfo.Position = WinPrint.JOB_POSITION_UNSPECIFIED;

                        Marshal.StructureToPtr(pinfo, ptrJobInfo, true); //Here


    Program breaks vshost.exe and sends some data to microsoft. Does anybody knows how to avoid this error? This code is from Vista x64 .NET 2.0, the same code compiled on XP SP2 x86 works ok.

    Help?
    Wednesday, July 4, 2007 8:55 AM

Answers

  • You definitely need to pass "false" as the 3rd argument to StructureToPtr(), the string pointers in "pinfo" shouldn't be released.
    Wednesday, July 4, 2007 6:15 PM

All replies

  • Can't tell you much without seeing the source for the ChargeUser method, or creating a sample that reproduces the problem without it.
    Wednesday, July 4, 2007 2:55 PM
  • You definitely need to pass "false" as the 3rd argument to StructureToPtr(), the string pointers in "pinfo" shouldn't be released.
    Wednesday, July 4, 2007 6:15 PM
  • True, true. I've changed it into pinned gchandle to structure.
    And all is fine, but there should be some nicer messages in those error cases.
    Thursday, July 5, 2007 8:39 PM