473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Win AP question

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
Nov 16 '05 #1
7 5511
On 2004-04-16, Kevin <an*******@discussions.microsoft.com> wrote:
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.
Nov 16 '05 #2
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
Nov 16 '05 #3
On 2004-04-16, Einar Høst <_e*******@hotmail.com> wrote:
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.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
"Ah, " said Arthur, "this is obviously some strange usage
of the word safe that I wasn't previously aware of. "
Nov 16 '05 #4

"Tom Shelton" <to*@mtogden.com> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
On 2004-04-16, Einar Høst <_e*******@hotmail.com> wrote:
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!
Nov 16 '05 #5
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
Nov 16 '05 #6
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
Nov 16 '05 #7
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[i] = (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" <an*******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
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

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.