Can anyone help me with this? When I run this function, on the first recursive call, WNetOpenEnum returns error 1204: "The specified network provider name is invalid."
The first call to Search is Search(null);
--------------------
Code: public struct NetResource { public int dwScope; public int dwType; public int dwDisplayType; public int dwUsage; [MarshalAs(UnmanagedType.LPTStr)] public string lpLocalName; [MarshalAs(UnmanagedType.LPTStr)] public string lpRemoteName; [MarshalAs(UnmanagedType.LPTStr)] public string lpComment; [MarshalAs(UnmanagedType.LPTStr)] public string lpProvider; } // end public struct NetResource
const int NO_ERROR = 0; const int ERROR_NOT_CONTAINER = 1207; const int ERROR_INVALID_PARAMETER = 87; const int ERROR_NO_NETWORK = 1222; const int ERROR_EXTENDED_ERROR = 1208; const int ERROR_MORE_DATA = 234; const int ERROR_NO_MORE_ITEMS = 259; const int ERROR_INVALID_HANDLE = 6; const int ERROR_INVALID_ADDRESS = 487; const int RESOURCE_CONNECTED = 0x00000001; const int RESOURCE_GLOBALNET = 0x00000002; const int RESOURCETYPE_ANY = 0x00000000; const int RESOURCEUSAGE_ALL = 0X00000000; const int RESOURCEUSAGE_CONNECTABLE = 0x00000001; const int RESOURCEUSAGE_CONTAINER = 0x00000002; const int RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001;
[DllImport("mpr.dll")] static extern int WNetOpenEnum( int dwScope, int dwType, int dwUsage, [MarshalAs(UnmanagedType.AsAny)][In] Object lpNetResource, //ref NetResource lpNetResource, out IntPtr lphEnum ); [DllImport("mpr.dll")] static extern int WNetEnumResource( IntPtr hEnum, ref int lpcCount, //ref NetResource[] lpBuffer, IntPtr lpBuffer, ref int lpBufferSize );
int errCode = 0; string errMsg = "";
void Search(object nr) { //TODO: Search for domains int rv; // Return value IntPtr lphEnum = new IntPtr(); rv = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_ALL, nr, out lphEnum);
if (rv == NO_ERROR) { IntPtr hEnum = lphEnum; int lpcCount = 1; NetResource lpBuffer = new NetResource(); int szNR = Marshal.SizeOf(lpBuffer); IntPtr ptr_lpBuffer = Marshal.AllocHGlobal(szNR); DoublyLinkedList domainList = new DoublyLinkedList(); do { szNR = Marshal.SizeOf(lpBuffer); rv = WNetEnumResource(hEnum, ref lpcCount, ptr_lpBuffer, ref szNR); lpBuffer = (NetResource)Marshal.PtrToStructure(ptr_lpBuffer, typeof(NetResource)); if (RESOURCEUSAGE_CONTAINER == (lpBuffer.dwDisplayType & RESOURCEUSAGE_CONTAINER)) { Search(lpBuffer); if (errCode != 0) break; } else if ((rv == ERROR_MORE_DATA || rv == NO_ERROR)) domainList.Append(lpBuffer); } while (rv == ERROR_MORE_DATA); Marshal.FreeHGlobal(ptr_lpBuffer); if (rv == NO_ERROR) { // Continue } // end if (rv == NO_ERROR) else if (errCode == 0) { errCode = 2; errMsg = processError(rv); } } // end if (rv == NO_ERROR) else if (errCode == 0) { errCode = 1; errMsg = processError(rv); } if (nr == null) { searching = false; OnSearchComplete(); } } // end void SearchThread()
|