Solved

converting a c++ function into c#

Posted on 2010-09-16
11
987 Views
Last Modified: 2013-12-16
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);


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

Open in new window

0
Comment
Question by:samelamin
  • 5
  • 4
  • 2
11 Comments
 
LVL 12

Expert Comment

by:w00te
ID: 33691986
You should use deserialize.  Check the code sample on the solution of this thread, it's pretty good :)

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d48b51bd-56be-45f9-a0d7-1481d3889b40

-w00te
0
 

Author Comment

by:samelamin
ID: 33692157
thank you but that converts byte to structure when i want the opposite
0
 
LVL 12

Expert Comment

by:w00te
ID: 33692390
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);

            Marshal.FreeHGlobal(buff);

            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:

http://dooba.net/2009/07/02/c-sharp-and-serializing-byte-arrays/
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 12

Expert Comment

by:w00te
ID: 33692407
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.

-w00te
0
 

Author Comment

by:samelamin
ID: 33692430
Yes thanks for that. But again thats a byte array and i only need a bye

I cant use  a byte array in SetJobA
0
 

Author Comment

by:samelamin
ID: 33693229
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;
            PRINTER_DEFAULTS pd=new PRINTER_DEFAULTS();
            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);




                }

                counter++;
            }
}

Open in new window

0
 
LVL 12

Expert Comment

by:w00te
ID: 33693969
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?
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33703829
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.
SetJob
http://msdn.microsoft.com/en-us/library/dd162978(VS.85).aspx 
"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."
 
0
 

Author Comment

by:samelamin
ID: 33714485
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?
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 33769949
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
http://msdn.microsoft.com/en-us/library/ms574466.aspx
PrintQueue Methods
http://msdn.microsoft.com/en-us/library/ms576293(v=VS.85).aspx
0
 

Author Closing Comment

by:samelamin
ID: 33927511
Alternative solution

Many thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question