Quick Search for:  in language:    
API,DOS,function,enumerate,network,resources,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
.Net Stats

 Code: 140,189. lines
 Jobs: 376. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for .Net.
Click here to see a screenshot of this code!Ray Picking for 3d Objects
By Jay1_z on 11/23

(Screen Shot)

Embed Images into Xml Files
By Chris Richards on 11/23


Encryption and Alternate Data Stream
By Philip Pierce on 11/23


Click here to see a screenshot of this code!AddressBook
By Ekong George Ekong on 11/23

(Screen Shot)

Click here to see a screenshot of this code!Command Line Redirection
By kaze on 11/23

(Screen Shot)

Fader
By Ahmad Hammad on 11/23


Click here to see a screenshot of this code!Get content of a web page (simple)
By Tin Trung Dang on 11/22

(Screen Shot)

Retrieve the long path name for a short path name.
By Özgür Aytekin on 11/22


Easy Randomizer
By Christian Müller on 11/22


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

WNetEnumResource

Print
Email
 

Submitted on: 11/23/2002 4:35:38 PM
By: Eugene Zhukovsky  
Level: Advanced
User Rating: By 2 Users
Compatibility:C#

Users have accessed this article 7155 times.
 

 
     This API function can enumerate all network resources, including servers, workstations, printers, shares, remote directories etc. Unlike NetServerEnum function, this will enumerate DOS, Win3.1, Windows 95 and above, as well as NT 3.51 and higher, Win2k amd XP workstations. The function is recursive, it works from top down. You can use this code in any way you like, hust keep the credit. Tested on XP and NT 4.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
//declare the DLL import functions [DllImport("netapi32

//Coded by Eugene E. Zhukovsky, 8.21.2002

//declare the DLL import functions

[DllImport("mpr.dll", CharSet=CharSet.Auto)]

public static extern int WNetEnumResource(

IntPtr hEnum,

ref int lpcCount,

IntPtr lpBuffer,

ref int lpBufferSize );

[DllImport("mpr.dll", CharSet=CharSet.Auto)]

public static extern int WNetOpenEnum(

RESOURCE_SCOPE dwScope,

RESOURCE_TYPE dwType,

RESOURCE_USAGE dwUsage,

[MarshalAs(UnmanagedType.AsAny)][In] Object lpNetResource,

out IntPtr lphEnum);

[DllImport("mpr.dll", CharSet=CharSet.Auto)]

public static extern int WNetCloseEnum( IntPtr hEnum );

//declare the structures to hold info

public enum RESOURCE_SCOPE

{

RESOURCE_CONNECTED = 0x00000001,

RESOURCE_GLOBALNET = 0x00000002,

RESOURCE_REMEMBERED = 0x00000003,

RESOURCE_RECENT= 0x00000004,

RESOURCE_CONTEXT= 0x00000005

}

public enum RESOURCE_TYPE

{

RESOURCETYPE_ANY= 0x00000000,

RESOURCETYPE_DISK= 0x00000001,

RESOURCETYPE_PRINT = 0x00000002,

RESOURCETYPE_RESERVED = 0x00000008,

}

public enum RESOURCE_USAGE

{

RESOURCEUSAGE_CONNECTABLE =0x00000001,

RESOURCEUSAGE_CONTAINER=0x00000002,

RESOURCEUSAGE_NOLOCALDEVICE =0x00000004,

RESOURCEUSAGE_SIBLING=0x00000008,

RESOURCEUSAGE_ATTACHED=0x00000010,

RESOURCEUSAGE_ALL =(RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),

}

public enum RESOURCE_DISPLAYTYPE

{

RESOURCEDISPLAYTYPE_GENERIC= 0x00000000,

RESOURCEDISPLAYTYPE_DOMAIN= 0x00000001,

RESOURCEDISPLAYTYPE_SERVER= 0x00000002,

RESOURCEDISPLAYTYPE_SHARE= 0x00000003,

RESOURCEDISPLAYTYPE_FILE = 0x00000004,

RESOURCEDISPLAYTYPE_GROUP= 0x00000005,

RESOURCEDISPLAYTYPE_NETWORK= 0x00000006,

RESOURCEDISPLAYTYPE_ROOT = 0x00000007,

RESOURCEDISPLAYTYPE_SHAREADMIN = 0x00000008,

RESOURCEDISPLAYTYPE_DIRECTORY = 0x00000009,

RESOURCEDISPLAYTYPE_TREE = 0x0000000A,

RESOURCEDISPLAYTYPE_NDSCONTAINER = 0x0000000B

}

public struct NETRESOURCE

{

public RESOURCE_SCOPE dwScope;

public RESOURCE_TYPE dwType;

public RESOURCE_DISPLAYTYPE dwDisplayType;

public RESOURCE_USAGE dwUsage;

[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpLocalName;

[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpRemoteName;

[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpComment;

[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpProvider;

}

//the function we'll be calling

public static void WNETOE(Object o)

{

int iRet;

IntPtr ptrHandle = new IntPtr();

try

{

iRet =WNetOpenEnum(

RESOURCE_SCOPE.RESOURCE_GLOBALNET,

RESOURCE_TYPE.RESOURCETYPE_ANY,

RESOURCE_USAGE.RESOURCEUSAGE_ALL,

o,

out ptrHandle );

if( iRet != 0 )

{

return;

}

int entries;

int buffer = 16384;

IntPtr ptrBuffer = Marshal.AllocHGlobal( buffer );

NETRESOURCE nr;

for(;;)

{

entries = -1;

buffer = 16384;

iRet =WNetEnumResource( ptrHandle, ref entries, ptrBuffer, ref buffer );

if( (iRet != 0) || (entries < 1) )

{

break;

}

Int32 ptr = ptrBuffer.ToInt32();

for( int i = 0; i < entries; i++ )

{

nr = (NETRESOURCE)Marshal.PtrToStructure( new IntPtr(ptr), typeof(NETRESOURCE) );

if(RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER == (nr.dwUsage

& RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER))

{

//call recursively to get all entries in a container

WNETOE(nr);

}

ptr += Marshal.SizeOf( nr );

Console.WriteLine( " {0} : LocalName='{1}' RemoteName='{2}'",

nr.dwDisplayType.ToString(), nr.lpLocalName, nr.lpRemoteName );

}

}

Marshal.FreeHGlobal( ptrBuffer );

iRet =WNetCloseEnum( ptrHandle );

}

catch(Exception e)

{

}

}

//now call the function passing a null

WNETOE(null);

//here's some possible error codes

public enum NERR

{

NERR_Success = 0,/* Success */

ERROR_MORE_DATA = 234, // dderror

ERROR_NO_BROWSER_SERVERS_FOUND = 6118,

ERROR_INVALID_LEVEL = 124,

ERROR_ACCESS_DENIED = 5,

ERROR_INVALID_PARAMETER = 87,

ERROR_NOT_ENOUGH_MEMORY = 8,

ERROR_NETWORK_BUSY = 54,

ERROR_BAD_NETPATH = 53,

ERROR_NO_NETWORK = 1222,

ERROR_INVALID_HANDLE_STATE = 1609,

ERROR_EXTENDED_ERROR= 1208

}


Other 8 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
11/27/2002 8:09:36 AM:Jorge Branco
why do u want it to work in win95 if the framework won't install in win95? LOL
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/27/2002 9:08:26 AM:Eugene Zhukovsky
What I mean is that it will ENUMERATE win95 PCs and higher, not just NT servers. The call will work FOR win95, not ON it. Hey, it'll even work FOR win3.1!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/12/2003 2:11:22 PM:
This is exactly what I'm looking for, but I'm a VB programmer. I wish you also had a VB .Net sample. I tried to past this code into a C# project, in Visual Studio, but I'm not sure how to properly do this.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | .Net Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.