473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Interop - EnumPrintProcessors

Could some kind soul illustrate how I can call the EnumPrintProcessors Win32
API from c# code?

BOOL EnumPrintProcessors(
LPTSTR pName, // print server name
LPTSTR pEnvironment, // environment name
DWORD Level, // information level
LPBYTE pPrintProcessorInfo, // processor data buffer
DWORD cbBuf, // size of data buffer
LPDWORD pcbNeeded, // bytes received or required
LPDWORD pcReturned // number of processors
);I am having particular trouble with the pPrintProcessorInfo parameter.

This is defined as an LPBYTE and will eventually end up with an array of
PRINTPROCESSOR_INFO_1 structures within it.

I cant quite translate this concept into C#

Thanks for any assistance, I have been trawling round msdn and the net to
little avail.

--
KM
Nov 17 '05 #1
3 3182
Keith,

Assuming that the definition here is correct, this is how I would
translate it:

[DllImport("dll name", CharSet=CharSet.Auto)]
static extern bool EnumPrintProcessors(
string pName,
string pEnvironment,
[MarshalAs(UnmanagedType.U4)]
int Level,
IntPtr pPrintProcessorInfo,
[MarshalAs(UnmanagedType.U4)]
int cbBuf,
[MarshalAs(UnmanagedType.U4)]
ref int pcbNeeded,
[MarshalAs(UnmanagedType.U4)]
ref int pcReturned);

The reason for the cbBuf parameter being declared as IntPtr is that the
P/Invoke layer can not marshal values back for c-style arrays (it will only
marshal back the first element). In order to make this work, you will have
to allocate unmanaged memory (through the static methods on the Marshal
class) and then pass that IntPtr through to the function. Upon return, you
can use the static PtrToStructure method on the Marshal class to get the
managed representation of the structure. Then, you have to release the
unmanaged memory.

You can also use unsafe code to do this, but it would be hard to
recommend how without seeing the structure of the PRINTPROCESSOR_INFO_1
structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Keith M" <ke***********@nospam.amsjv.com> wrote in message
news:42********@glkas0286.greenlnk.net...
Could some kind soul illustrate how I can call the EnumPrintProcessors
Win32
API from c# code?

BOOL EnumPrintProcessors(
LPTSTR pName, // print server name
LPTSTR pEnvironment, // environment name
DWORD Level, // information level
LPBYTE pPrintProcessorInfo, // processor data buffer
DWORD cbBuf, // size of data buffer
LPDWORD pcbNeeded, // bytes received or required
LPDWORD pcReturned // number of processors
);I am having particular trouble with the pPrintProcessorInfo parameter.

This is defined as an LPBYTE and will eventually end up with an array of
PRINTPROCESSOR_INFO_1 structures within it.

I cant quite translate this concept into C#

Thanks for any assistance, I have been trawling round msdn and the net to
little avail.

--
KM

Nov 17 '05 #2

"Keith M" <ke***********@nospam.amsjv.com> wrote in message
news:42********@glkas0286.greenlnk.net...
Could some kind soul illustrate how I can call the EnumPrintProcessors
Win32
API from c# code?

BOOL EnumPrintProcessors(
LPTSTR pName, // print server name
LPTSTR pEnvironment, // environment name
DWORD Level, // information level
LPBYTE pPrintProcessorInfo, // processor data buffer
DWORD cbBuf, // size of data buffer
LPDWORD pcbNeeded, // bytes received or required
LPDWORD pcReturned // number of processors
);I am having particular trouble with the pPrintProcessorInfo parameter.

This is defined as an LPBYTE and will eventually end up with an array of
PRINTPROCESSOR_INFO_1 structures within it.

I cant quite translate this concept into C#

Thanks for any assistance, I have been trawling round msdn and the net to
little avail.

--
KM


Why using PInvoke when it's that easy to do using System.Management and WMI
classes.
Herewith a small sample...

using System.Management ;
....
GetPropPrinter("somePrinterDevice"); // UNC path or local printer name

static int GetPropPrinter(string printerDevice)
{
string path = "win32_printer.DeviceId='" + printerDevice + "'";
using (ManagementObject printer = new ManagementObject(path))
{
printer.Get();
PropertyDataCollection printerProperties = printer.Properties;
foreach (PropertyData printerProperty in printerProperties ) {
if(printerProperty.Value !=null) // Show only non-null property values
Console.WriteLine("Property = {0}\t Value = {1}",
printerProperty.Name, printerProperty.Value);
}
}
}

Willy.
Nov 17 '05 #3
Thanks for that. the PtrToStructure call would seem to be appropriate.

Unfortunately I cant see how to use it with an array of structures,
allocated as an array of bytes, coming back in.

From the documentation:
"The buffer must be large enough to receive the array of structures and any
strings to which the structure members point. "

So if I got back an array with 2 structures represented within, how would I
get them into two PRINTPROCESSOR_INFO_1
structures?

PRINTPROCESSOR_INFO_1 by the way is defined thus:

typedef struct _PRINTPROCESSOR_INFO_1
{
LPTSTR pName;
}
Thanks
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u6*************@TK2MSFTNGP15.phx.gbl...
Keith,

Assuming that the definition here is correct, this is how I would
translate it:

[DllImport("dll name", CharSet=CharSet.Auto)]
static extern bool EnumPrintProcessors(
string pName,
string pEnvironment,
[MarshalAs(UnmanagedType.U4)]
int Level,
IntPtr pPrintProcessorInfo,
[MarshalAs(UnmanagedType.U4)]
int cbBuf,
[MarshalAs(UnmanagedType.U4)]
ref int pcbNeeded,
[MarshalAs(UnmanagedType.U4)]
ref int pcReturned);

The reason for the cbBuf parameter being declared as IntPtr is that the P/Invoke layer can not marshal values back for c-style arrays (it will only marshal back the first element). In order to make this work, you will have to allocate unmanaged memory (through the static methods on the Marshal
class) and then pass that IntPtr through to the function. Upon return, you can use the static PtrToStructure method on the Marshal class to get the
managed representation of the structure. Then, you have to release the
unmanaged memory.

You can also use unsafe code to do this, but it would be hard to
recommend how without seeing the structure of the PRINTPROCESSOR_INFO_1
structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Nov 17 '05 #4

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

Similar topics

0
by: roy | last post by:
I try to call com written in VB 6.0. When I use VS.net Studio to do the debuging, some time it works fine, some time I got the following message: Server Error in '/GISOnlineReservation'...
0
by: roy | last post by:
I try to call com written in VB 6.0., some time it works fine, some time I got the following message: Server Error in '/GISOnlineReservation' Application....
0
by: keefah | last post by:
Hi, I'm writing a C# web app that uses Outlook to send email. I use a reference to the Microsoft Outlook 11.0 Object Library, but it's giving me problems. I tracked down some stuff on the Net...
0
by: lacour | last post by:
I can't seem to figure out the difference between adding a COM dll reference in VS2003 and by using TLBIMP. I have a COM dll that references another COM dll, and I want the syntax of my...
8
by: Rob Edwards | last post by:
When trying to add the Microsoft CDO for Exchange Management Library (aka CDOEXM.dll) I receive the following message: "A reference to 'Microsoft CDO for Exchange Management Library' could not be...
7
by: R Reyes | last post by:
Can someone please explain to me why I can't get the MS Word Interop assembly to work in my VS2005 project? I'm trying to manipulate MS Word from my Web Form application and I can't get passed...
2
by: JC | last post by:
Anybody knows what problem has this code? I think, in the Garbage Collector? You know the Solution? The program in the test's case, whit 350 contacts, run OK before number 86. The error is a...
1
by: allbelonging | last post by:
C#.Net Outlook 2003 automation (programmatically) with Office.Interop.Outlook Problem: I have my outlook 2003 configured with multiple mailbox on my local machine. I want to specify the mailbox...
0
by: Tina | last post by:
I've gotten this before where it says there is a problem with Interop.MSDASC but I can't remember what causes this. This is a 1.1 app I'm trying to debug in vs2005. It was running yesterday just...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.