Mobinex
asked on
How to get list of path of printers in LAN network?
Hi Experts,
I want to view list of printers in Local Network to let users can show to open and print but I don't know how to get list of printers that are available on the LAN network by using C++ (MFC) or JAVA.
Can you help to give some example in C++ (if you have no ideas in C++ then JAVA is OK).
Thank in advance!
I want to view list of printers in Local Network to let users can show to open and print but I don't know how to get list of printers that are available on the LAN network by using C++ (MFC) or JAVA.
Can you help to give some example in C++ (if you have no ideas in C++ then JAVA is OK).
Thank in advance!
You can do that using 'WNetEnumResource()' (http://msdn2.microsoft.com/en-us/library/aa385449.aspx) and filtering for 'RESOURCETYPE_PRINT', e.g.
#include <stdio.h>
#include <windows.h>
#pragma hdrstop
// compile and link with
// cl wneprinter.c mpr.lib
int main( void );
int doEnum( int level, NETRESOURCE *pnr );
void exterr( void );
void exterr( void )
{
char errbuf[2048], namebuf[2048];
DWORD err;
errbuf[0] = namebuf[0] = '\0';
if ( WNetGetLastError( &err, errbuf, sizeof errbuf, namebuf, sizeof namebuf ) == NO_ERROR )
printf( "Error %lu (\"%s\") reported by \"%s\".\n",
err, errbuf, namebuf );
return;
}
int doEnum( int level, NETRESOURCE *pnr )
{
DWORD rc, rc2;
HANDLE hEnum;
DWORD count, bufsize, ui;
NETRESOURCE buf[200];
const char *type, *cont;
rc = WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pnr, &hEnum );
if ( rc == ERROR_ACCESS_DENIED )
{
printf( "%-6.6s %-4.4s%*s Error 5 -- access denied\n", "", "", level * 2, "" );
return 1;
}
if ( rc )
{
printf( "WNOE(): rc = %lu, gle = %lu\n", rc, rc2 = GetLastError() );
if ( rc2 == ERROR_EXTENDED_ERROR )
exterr();
return 0;
}
while ( 1 )
{
count = (DWORD) -1L;
bufsize = sizeof buf;
rc = WNetEnumResource( hEnum, &count, buf, &bufsize );
if ( rc != NO_ERROR )
break;
for ( ui = 0; ui < count; ++ ui )
{
if ( buf[ui].dwType != RESOURCETYPE_PRINT ) continue; // skip anything but printers
switch ( buf[ui].dwDisplayType )
{
case RESOURCEDISPLAYTYPE_DOMAIN:
type = "domain"; break;
case RESOURCEDISPLAYTYPE_GENERIC:
type = "generic"; break;
case RESOURCEDISPLAYTYPE_SERVER:
type = "server"; break;
case RESOURCEDISPLAYTYPE_SHARE:
type = "share"; break;
default:
type = "unknown"; break;
}
cont = ( buf[ui].dwUsage & RESOURCEUSAGE_CONTAINER )? "container": "";
printf( "%-6.6s %-4.4s%*s %s (%s)\n", type, cont, level * 2, "",
buf[ui].lpRemoteName, buf[ui].lpProvider );
// now we recurse if it's a container
if ( buf[ui].dwUsage & RESOURCEUSAGE_CONTAINER )
doEnum( level + 1, &buf[ui] );
}
}
if ( rc != ERROR_NO_MORE_ITEMS ) // bad things
{
printf( "WNER(): rc = %lu, gle = %lu\n", rc, rc2 = GetLastError() );
if ( rc2 == ERROR_EXTENDED_ERROR )
exterr();
}
WNetCloseEnum( hEnum );
return 1;
}
int main( void )
{
doEnum( 0, NULL );
return 0;
}
ASKER
Hi jkr,
I have tried this but I can not find the printers resource.
If I commented out the code
//if ( buf[ui].dwType != RESOURCETYPE_PRINT ) continue; // skip anything but printers
Then I see it listed all shared folder and PCs with the path like this:
share \\13VTM-DSK0010\Documents (Microsoft Windows Network)
share \\13VTM-DSK0010\Installer (Microsoft Windows Network)
server cont \\13VTM-DSK0011 (Microsoft Windows Network)
Can you give more ideas?
Many thanks!
I have tried this but I can not find the printers resource.
If I commented out the code
//if ( buf[ui].dwType != RESOURCETYPE_PRINT ) continue; // skip anything but printers
Then I see it listed all shared folder and PCs with the path like this:
share \\13VTM-DSK0010\Documents (Microsoft Windows Network)
share \\13VTM-DSK0010\Installer (Microsoft Windows Network)
server cont \\13VTM-DSK0011 (Microsoft Windows Network)
Can you give more ideas?
Many thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://cc.codegear.com/ProdCat.aspx?prodid=2&catid=13
http://cc.codegear.com/Item/19577