converting a c++ function into c#

Dear Experts

I have a c++ function that i would like to convert into c#, Im not the best at Marshalling so can someone help with it?

Basically I would like to use the Setjob function to change a documents name

The Job_INFO is where the name is going to be set, its a structure that needs to be set and i have

However how do i marshal a byte to a structure?

Any help would be greatly appreciated
//in C++
c =	SetJob(hPrinter, pJobs[iIndex].JobId, 2, (LPBYTE) pJobs,0);

 SetJobA(pHandle, jobinfo.JobId, 2, ref b, 0);

Open in new window

Who is Participating?
nffvrxqgrcfqvvcConnect With a Mentor Commented:
Sure. You can post your example but if your using framework 3.0 or later maybe you should consider using the managed Printing Class which internally uses the printing API. You might need to Add Reference to System.Printing from the .NET Tab.
System.Printing Namespace
PrintQueue Methods
You should use deserialize.  Check the code sample on the solution of this thread, it's pretty good :)

samelaminAuthor Commented:
thank you but that converts byte to structure when i want the opposite
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

public static Byte[] SerializeMessage< T >(T msg) where T : struct
            int objsize = Marshal.SizeOf(typeof(T));
            Byte[] ret = new Byte[objsize];

            IntPtr buff = Marshal.AllocHGlobal(objsize);

            Marshal.StructureToPtr(msg, buff, true);

            Marshal.Copy(buff, ret, 0, objsize);


            return ret;

Very high detail description here, it's worth a read.  It also has the reverse of this function.  They're templated for easy use:
PS: In case you don't read it,

First, we have to make sure the structs we declare in C# and packed. Here is an example:

using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack=1)]
struct msg1{
     int a;
     int b
     short c;

This will ensure there are no gaps between elements in the struct.

samelaminAuthor Commented:
Yes thanks for that. But again thats a byte array and i only need a bye

I cant use  a byte array in SetJobA
samelaminAuthor Commented:
got it working but for some reason it isnt changing the job
      protected void Button2_Click(object sender, EventArgs e)
            PrinterSettings test = new PrinterSettings();
            IntPtr hPandle;
            string szPrinter = "pulltest";
            OpenPrinter(szPrinter, out hPrinter, ref pd);
            JOB_INFO_2 jobinfo = new JOB_INFO_2();
            int counter = 0;
            ManagementObjectSearcher mos = new ManagementObjectSearcher(new ObjectQuery("SELECT *     FROM Win32_PrintJob"));
            foreach (ManagementObject mo in mos.Get())
                //'loop thru the document names and find if that is what you need to delete.
                string file = mo["Document"] as string;

                jobinfo.pDocument = "Test";
                jobinfo.pUserName = "Working";

                int nSize = Marshal.SizeOf(jobinfo);
                //pJobPtr = Marshal.AllocHGlobal(nSize);
                //Marshal.StructureToPtr(jobinfo, pJobPtr, false);

                //byte[] c = SerializeMessage(jobinfo);
                byte[] c = StructureToArray(jobinfo);

                   SetJob(hPrinter, jobinfo.JobId, 2, c, 0);



Open in new window

How would a byte be equivilent to an entire structure? A byte is the size of a char... it would have to be  a byte array.

Moving on... are you sure the SetJob function actually works properly?  How is it written?
The SetJob() API wants a pointer to a JOB_INFO structure.. Very different than a single byte as the others have pointed out.. You could just reference the JOB_INFO structure in your API declerations and pass the structure instead of doing all the conversions. You can also use an IntPtr for (pJobs) then Marshal.StructureToPtr() and simply pass the pointer. If your C# decleration is using ref Byte than that would take the byte array and pass pointer to the byte array so it's another way of passing the structure to the API.
"You must have PRINTER_ACCESS_ADMINISTER access permission for a printer in order to change a print job's position in the print queue."
"The following members of a JOB_INFO_1, JOB_INFO_2, or JOB_INFO_4 structure are ignored on a call to SetJob: JobId, pPrinterName, pMachineName, pUserName, pDrivername, Size, Submitted, Time, and TotalPages."
samelaminAuthor Commented:
thanks for your comment eq1044 and thanks to everyoneelse

I have set the structure as you instructed, and marshalled a the structure to a pointer (IntPTR)

However the problem is that the SetJob Function needs to be passed a byte array instead of a pointer

Would you like me to past emy full code?
samelaminAuthor Commented:
Alternative solution

Many thanks
All Courses

From novice to tech pro — start learning today.