WNet API doesn't work in Windows 8

399 views Asked by At

We have a .NET application that uses the WNet API through P/Invoke to enumerate network resources in a Windows network. The application works perfectly on Vista and Windows 7, but in Windows 8 it doesn't discover anything.

I have tried to debug it and there are no errors, it actually finds a few network containers at the first level, but then within them it finds nothing, it always returns zero entries and "no more entries" code.

Are there any known problems with WNet API on Windows 8?

Thanks, Mihai

1

There are 1 answers

0
Fernando Diaz Toledano On

SOLUTION

I change my code accord to http://www.pinvoke.net/default.aspx/netapi32/netserverenum.html and all works well

  • ยก using SERVER_INFO_101 !

I have the same isue with this code

int ret = NetServerEnum(null, 100, ref buffer,
                    MAX_PREFERRED_LENGTH,
                    out entriesRead,
                    out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER | SV_TYPE_WINDOWS | SV_TYPE_NT, null, out 
                resHandle);
                //if the returned with a NERR_Success 
                //(C++ term), =0 for C#
                if (ret == 0)
                {
                    //loop through all SV_TYPE_WORKSTATION 
                    //and SV_TYPE_SERVER PC's
                    for (int i = 0; i < totalEntries; i++)
                    {
                        //get pointer to, Pointer to the 
                        //buffer that received the data from
                        //the call to NetServerEnum. 
                        //Must ensure to use correct size of 
                        //STRUCTURE to ensure correct 
                        //location in memory is pointed to
                        tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
                        //Have now got a pointer to the list 
                        //of SV_TYPE_WORKSTATION and 
                        //SV_TYPE_SERVER PC's, which is unmanaged memory
                        //Needs to Marshal data from an 
                        //unmanaged block of memory to a 
                        //managed object, again using 
                        //STRUCTURE to ensure the correct data
                        //is marshalled 
                        _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
                        //add the PC names to the ArrayList
                        networkComputers.Add(svrInfo.sv100_name);
                    }
                }

in line

                        tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));

the method crash....