Win AP question

G

Guest

Hi al

I have an interesting question.... I am working witha Win API this is the Function
Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Lon

Which I got from the API viewer that comes with VB 6. I have tried to convert it to the following

[DllImport("winspool.drv")
public static extern long EnumJobs(long hPrinter,long FirstJob,long NoJobs,long Level,ref JOB_INFO_1 jInfo,long cdBuf,long pcbNeeded,long pcReturned )

however i think I may have gotten a few things wrong, which I am hoping someone will be able to correct me

1. In the Declare statement there is a parameter that is : pJob As Byte and I've even seen it as : pJob As An
Now my question is that can I pass my struct(JOB_INFO_1) in there? even though the datatype it wants is byte, can I still pass the struct in there? How do i get my struct popultaed from this call, when it accepts a byte? do i need to do anything with how I created my strcut, this is my sample code of the strcut declaration
[StructLayout(LayoutKind.Sequential)
public struct JOB_INFO_1

public long JobId
public string pPrinterName
public string pMachineName
public string pUserName;
public string pDocument;
public string pDatatype;
public string pStatus;
public long Status;
public long Priority
public long Position;
public long TotalPages;
public long PagesPrinted;
public SYSTEMTIME Submitted


[StructLayout(LayoutKind.Sequential)
public struct SYSTEMTIM

public int wYear
public int wMonth
public int wDayOfWeek
public int wDay
public int wHour
public int wMinute
public int wSecond
public int wMilliseconds


2. If ever I see another winAPI that accepts a datatype of "Any" what do I know what is "supposed to get there"

Thanks for any hel
Kevi
 
T

Tom Shelton

Hi all

I have an interesting question.... I am working witha Win API this is the Function:
Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long

Which I got from the API viewer that comes with VB 6. I have tried to convert it to the following:

[DllImport("winspool.drv")]
public static extern long EnumJobs(long hPrinter,long FirstJob,long NoJobs,long Level,ref JOB_INFO_1 jInfo,long cdBuf,long pcbNeeded,long pcReturned );

however i think I may have gotten a few things wrong, which I am hoping someone will be able to correct me.

1. In the Declare statement there is a parameter that is : pJob As Byte and I've even seen it as : pJob As Any
Now my question is that can I pass my struct(JOB_INFO_1) in there? even though the datatype it wants is byte, can I still pass the struct in there? How do i get my struct popultaed from this call, when it accepts a byte? do i need to do anything with how I created my strcut, this is my sample code of the strcut declaration :
[StructLayout(LayoutKind.Sequential)]
public struct JOB_INFO_1
{
public long JobId;
public string pPrinterName;
public string pMachineName;
public string pUserName;
public string pDocument;
public string pDatatype;
public string pStatus;
public long Status;
public long Priority;
public long Position;
public long TotalPages;
public long PagesPrinted;
public SYSTEMTIME Submitted;
}

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public int wYear;
public int wMonth;
public int wDayOfWeek;
public int wDay;
public int wHour;
public int wMinute;
public int wSecond;
public int wMilliseconds;
}

2. If ever I see another winAPI that accepts a datatype of "Any" what do I know what is "supposed to get there"?

Thanks for any help
Kevin

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct JOB_INFO_1
{
public int JobId;
public string pPrinterName;
public string pMachineName;
public string pUserName;
public string pDocument;
public string pDatatype;
public string pStatus;
public int Status;
public int Priority;
public int Position;
public int TotalPages;
public int PagesPrinted;
public SYSTEMTIME Submitted;
}

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
public static extern int EnumJobs(
System.IntPtr hPrinter,
int FirstJob,
int NoJobs,
int Level,
ref JOB_INFO_1[] jInfo,
int cdBuf,
out int pcbNeeded,
out int pcReturned);

The above is untested, but would be my first cut based on the msdn
documentation. Some things you need to remember... In VB6, a Long is a
32-bit integer. In C#, a long is a 64-bit integer. That means, you
should translate VB6 into C# int's. VB6 Integer is 16-bit that's
equivalent to C#'s short.

You may want to read the documentation on P/Invoke.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
I've never been canoeing before, but I imagine there must be just a few
simple heuristics you have to remember...

Yes, don't fall out, and don't hit rocks.
 
E

Einar Høst

I have a follow-up question:

What to do when the type of struct depends on some other input parameter?
Specifically, I need to call a Win32 API function called GetTapeParameters.

I written this:

[DllImport("kernel32"), SetLastError=true]
static extern int GetTapeParameters(
IntPtr hTape,
int dwOperation,
??? lpTapeInformation
)

The problem is that lpTapeInformation should point to different kinds of
structs depending on the value for dwOperation (dwOperation
GET_TAPE_MEDIA_INFORMATION implies a TAPE_GET_MEDIA_PARAMETERS struct,
whereas dwOperation GET_TAPE_DRIVE_INFORMATION implies a
TAPE_GET_DRIVE_PARAMETERS struct). Therefore, I can't simply put ref
<struct-type> lpTapeInformation...

Any help would be much appreciated!

- Einar
 
T

Tom Shelton

I have a follow-up question:

What to do when the type of struct depends on some other input parameter?
Specifically, I need to call a Win32 API function called GetTapeParameters.

I written this:

[DllImport("kernel32"), SetLastError=true]
static extern int GetTapeParameters(
IntPtr hTape,
int dwOperation,
??? lpTapeInformation
)

The problem is that lpTapeInformation should point to different kinds of
structs depending on the value for dwOperation (dwOperation
GET_TAPE_MEDIA_INFORMATION implies a TAPE_GET_MEDIA_PARAMETERS struct,
whereas dwOperation GET_TAPE_DRIVE_INFORMATION implies a
TAPE_GET_DRIVE_PARAMETERS struct). Therefore, I can't simply put ref
<struct-type> lpTapeInformation...

Any help would be much appreciated!

- Einar

You can declare the function once for each struct type (basically you can overload it)...
Or, you can use unsafe code, and pass the value as you would in C.
 
E

Einar Høst

Tom Shelton said:
I have a follow-up question:

What to do when the type of struct depends on some other input parameter?
Specifically, I need to call a Win32 API function called GetTapeParameters.

I written this:

[DllImport("kernel32"), SetLastError=true]
static extern int GetTapeParameters(
IntPtr hTape,
int dwOperation,
??? lpTapeInformation
)

The problem is that lpTapeInformation should point to different kinds of
structs depending on the value for dwOperation (dwOperation
GET_TAPE_MEDIA_INFORMATION implies a TAPE_GET_MEDIA_PARAMETERS struct,
whereas dwOperation GET_TAPE_DRIVE_INFORMATION implies a
TAPE_GET_DRIVE_PARAMETERS struct). Therefore, I can't simply put ref
<struct-type> lpTapeInformation...

Any help would be much appreciated!

- Einar

You can declare the function once for each struct type (basically you can overload it)...
Or, you can use unsafe code, and pass the value as you would in C.

Hi Tom, thanks for your reply.

I just discovered that, like you say, you can overload the function. My C
skills are rather poor (I've never done Petzold-style Windows
programming...), so I really wouldn't know how to pass the value using
unsafe code. If you got time on your hands, would you mind typing in a small
sample?

Thanks!
 
G

Guest

Hi To

Thanks for the help, I really appreciate it, Will check it out today to see if I get the results I want :

Regard
Kevin
 
G

Guest

OK Tom this is my code so fa

using System
using System.Drawing
using System.Collections
using System.ComponentModel
using System.Windows.Forms
using System.Data
using System.Runtime.InteropServices

namespace SlyPrintMo

/// <summary
/// Summary description for Form1
/// </summary
///

public class Form1 : System.Windows.Forms.For

//Declare all Constants her
public const short CCHDEVICENAME = 32
public const short CCHFORMNAME = 32

//end of constant declaration

//Declare all structs her
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)
public struct PRINTER_DEFAULT

string pDatatype
DEVMODE pDevMode
int DesiredAccess

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)
public struct DEVMOD

string dmDeviceName; // * CCHDEVICENAME
short dmSpecVersion;
short dmDriverVersion
short dmSize;
short dmDriverExtra
int dmFields
short dmOrientation
short dmPaperSize
short dmPaperLength
short dmPaperWidth
short dmScale
short dmCopies
short dmDefaultSource
short dmPrintQuality;
short dmColor;
short dmDuplex;
short dmYResolution
short dmTTOption;
short dmCollate
string dmFormName; // * CCHFORMNAME
short dmUnusedPadding
int dmBitsPerPel
int dmPelsWidth
int dmPelsHeight;
int dmDisplayFlags;
int dmDisplayFrequency


[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)
public struct JOB_INFO_1

public int JobId
public string pPrinterName
public string pMachineName
public string pUserName;
public string pDocument;
public string pDatatype;
public string pStatus;
public int Status;
public int Priority
public int Position;
public int TotalPages;
public int PagesPrinted;
public SYSTEMTIME Submitted


[StructLayout(LayoutKind.Sequential)
public struct SYSTEMTIM

public short wYear
public short wMonth
public short wDayOfWeek
public short wDay
public short wHour
public short wMinute
public short wSecond
public short wMilliseconds


//end of struct declaration

private System.ComponentModel.IContainer components
[DllImport("winspool.drv",CharSet=CharSet.Auto)
public static extern int OpenPrinter(string pPrinterName, out System.IntPtr phPrinter, PRINTER_DEFAULTS pd)
[DllImport("winspool.drv")
public static extern int ClosePrinter(int hPrinter)
[DllImport("winspool.drv",CharSet=CharSet.Auto)
public static extern int EnumJobs(int hPrinter,int FirstJob,int NoJobs,int Level,ref JOB_INFO_1 jInfo,int cdBuf,out int pcbNeeded,out int pcReturned )

public Form1(

/
// Required for Windows Form Designer suppor
/
InitializeComponent()
System.IntPtr hPrinter
int lNeeded =0
int lReturned = 0


PRINTER_DEFAULTS pDef = new PRINTER_DEFAULTS()
OpenPrinter("OPTRAE",out hPrinter,pDef)
JOB_INFO_1 jInfo = new JOB_INFO_1()
EnumJobs((int)hPrinter,0,99,1,ref jInfo,0,out lNeeded, out lReturned)
if (lNeeded>0)

EnumJobs((int)hPrinter,0,99,1,ref jInfo, lNeeded, out lNeeded,out lReturned)
Console.WriteLine(jInfo.pUserName + " : " + jInfo.TotalPages)




/
// TODO: Add any constructor code after InitializeComponent cal
/


/// <summary
/// Clean up any resources being used
/// </summary
protected override void Dispose( bool disposing

if( disposing

if (components != null)

components.Dispose()


base.Dispose( disposing )


#region Windows Form Designer generated cod
/// <summary
/// Required method for Designer support - do not modif
/// the contents of this method with the code editor
/// </summary
private void InitializeComponent(

this.components = new System.ComponentModel.Container()

//
// Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13)
this.ClientSize = new System.Drawing.Size(488, 357)
this.Name = "Form1"
this.Text = "Form1"


#endregio

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

}
}


Now when I run it I can't even get it to give me a handle to the printer:( the hPrinter variable just stays at 0

Any suggestions?

Kevin
 
B

BMermuys

Hi,

First of all lets look at the c sign :

BOOL EnumJobs(
HANDLE hPrinter, // handle to printer object
DWORD FirstJob, // index of first job
DWORD NoJobs, // number of jobs to enumerate
DWORD Level, // information level
LPBYTE pJob, // job information buffer
DWORD cbBuf, // size of job information buffer
LPDWORD pcbNeeded, // bytes received or required
LPDWORD pcReturned // number of jobs received
);

After reading the information you'll see that pJob should be a buffer large
enough to hold all structs *plus* the data the struct points to (string
data). (There are other api's which uses this "strategy" too)

This means that you can't just pass an array of structs. You must supply
memory which is large enough for the structs and the string data. In memory
it would be something like :
struct1
struct2
struct3
all other data where the structs point to (eg. string data)

So, we declare it in c# as follow :

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
public static extern int EnumJobs(
IntPtr hPrinter,
int FirstJob,
int NoJobs,
int Level,
IntPtr pInfo,
int cdBuf,
out int pcbNeeded,
out int pcReturned);

pInfo is an IntPtr which we can point to allocated memory. One of the
problems of the used "strategy" is that you can't easily know who many bytes
you will need, you can't do NoJobs*sizeof(job_info_1) because there is
additional memory needed as explained.

You can guess the memory needed and if it fails you can try with more
memory. There is a better way : call the function first with cdBuf=0 then
it will return the bytes required in pcbNeeded.

Once you know the bytes needed, you can allocate memory with
(Marshal.AllocHGlobal), then you can call the function again with cdBuf set
to the last return pcbNeeded. Now the memory will be filled with structs
and extra data. You can then use Marshal.PtrToStruct to marshal these
struct into managed memory (it will also marshal the extra data because the
struct members point to it). After this you clean up unmanaged memory
(Marshal.FreeHGlobal).

Example : (!!You need to use the corrected structs by Tom)

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
public static extern bool OpenPrinter( string pPrinterName, out IntPtr
phPrinter, IntPtr pDefault );

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
public static extern bool ClosePrinter( IntPtr hPrinter // handle to printer
object );

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
public static extern int EnumJobs( IntPtr hPrinter, int FirstJob, int
NoJobs, int Level, IntPtr pInfo, int cdBuf, out int pcbNeeded, out int
pcReturned);

public void test()
{
IntPtr handle;
int FirstJob = 1;
int NumJobs = 2;
int pcbNeeded;
int pcReturned;

// open printer
OpenPrinter ( @\\server1\psc750, out handle, IntPtr.Zero );

// get num bytes required
EnumJobs ( handle, FirstJob, NumJobs, 1, IntPtr.Zero, 0, out pcbNeeded,
out pcReturned);

// allocate unmanaged memory
IntPtr pData = Marshal.AllocHGlobal ( pcbNeeded );

// get structs
EnumJobs ( handle, FirstJob, NumJobs, 1, pData, pcbNeeded, out
pcbNeeded, out pcReturned);

// create array of managed job structs
JOB_INFO_1 [] jobs = new JOB_INFO_1[pcReturned];

// marshal struct to managed
int pTemp = pData.ToInt32(); //start pointer
for (int i=0; i<pcReturned; ++i)
{
jobs = (JOB_INFO_1) Marshal.PtrToStruct( new IntPtr(pTemp),
typof(JOB_INFO_1) );
pTemp+= Marshal.SizeOf( typeof (JOB_INFO_1 );
}

// cleanup unmanaged memory
Marshal.FreeHGlobal ( pData );

// close printer
ClosePrinter ( handle );

// printer jobs are in the jobs array
}

HTH,
greetings



Kevin said:
Hi all

I have an interesting question.... I am working witha Win API this is the Function:
Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA"
(ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal
Level As Long, pJob As Byte, ByVal cdBuf As Long, pcbNeeded As Long,
pcReturned As Long) As Long
Which I got from the API viewer that comes with VB 6. I have tried to convert it to the following:

[DllImport("winspool.drv")]
public static extern long EnumJobs(long hPrinter,long FirstJob,long
NoJobs,long Level,ref JOB_INFO_1 jInfo,long cdBuf,long pcbNeeded,long
pcReturned );
however i think I may have gotten a few things wrong, which I am hoping
someone will be able to correct me.
1. In the Declare statement there is a parameter that is : pJob As Byte
and I've even seen it as : pJob As Any
Now my question is that can I pass my struct(JOB_INFO_1) in there? even
though the datatype it wants is byte, can I still pass the struct in there?
How do i get my struct popultaed from this call, when it accepts a byte? do
i need to do anything with how I created my strcut, this is my sample code
of the strcut declaration :
[StructLayout(LayoutKind.Sequential)]
public struct JOB_INFO_1
{
public long JobId;
public string pPrinterName;
public string pMachineName;
public string pUserName;
public string pDocument;
public string pDatatype;
public string pStatus;
public long Status;
public long Priority;
public long Position;
public long TotalPages;
public long PagesPrinted;
public SYSTEMTIME Submitted;
}

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public int wYear;
public int wMonth;
public int wDayOfWeek;
public int wDay;
public int wHour;
public int wMinute;
public int wSecond;
public int wMilliseconds;
}

2. If ever I see another winAPI that accepts a datatype of "Any" what do I
know what is "supposed to get there"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top