Help Converting C# NetServerEnum code 
Author Message
 Help Converting C# NetServerEnum code

Hi
The following code allows one to list the PC's within a domain, the
domains themselves and also SQL server instances on a network.
Unfortunately I can't get it to work or convert it to Vb.net. I get as
far as the NetServerEnum, but am lost on the
"[marshal(UnmanagedType.LPVoid)]uint* bufptr" as the mashal type doesn't
seem to exist.

I can use a VB6 COM dll, which I know works but surely the all singing
and dancing .Net platform can do something as fundemental as listing the
PC's and domains???

Can anyone help.

public enum ServerTypeEnum
{
    steNone = 0,
    steWorkstation = 0x00000001,
    steAll = 0x00000002,
    steSQLServer = 0x00000004,
    steDomainController = 0x00000008

Quote:
}

[sysimport(dll="netapi32.dll")]
private static extern void
NetApiBufferFree([marshal(UnmanagedType.U4)]uint bufptr);

[sysimport(dll="netapi32.dll")]
unsafe private static extern uint
NetServerEnum([marshal(UnmanagedType.LPWStr)] string ServerName,
        uint level,
        [marshal(UnmanagedType.LPVoid)]uint* bufptr,
        uint prefmaxlen,
        ref uint entriesread,
        ref uint totalentries,
        uint servertype,
        [marshal(UnmanagedType.LPWStr)] string domain,
        uint resume_handle);

[System.Runtime.InteropServices.StructLayoutAttribute
(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct SERVER_INFO_101
{
    public int dwPlatformID;
    public int lpszServerName;
    public int dwVersionMajor;
    public int dwVersionMinor;
    public int dwType;
    public int lpszComment;

Quote:
}

protected void GetServers()
{
    string servername = null;
    string domain = "YourDomainName";
    uint level = 101, prefmaxlen = 0xFFFFFFFF, entriesread = 0,
         totalentries = 0, resume_handle = 0;

    cboServers.Items.Clear();

    unsafe
    {    
        //get a pointer to the server info structure
        SERVER_INFO_101* si = null;
        SERVER_INFO_101* pTmp;    //temp pointer for use when looping
                                  //through returned (pointer) array

        //this api requires a pointer to a byte array...
        //which is actually a pointer to an array of SERVER_INFO_101
structures
        //If the domain parameter is NULL, the primary domain is
implied.
        uint nRes = NetServerEnum(servername, level,
                (uint *) &si, prefmaxlen, ref entriesread, ref
totalentries,
                (uint)_ServerType, domain, resume_handle);

        if (nRes == 0)
        {
            if ((pTmp = si) != null)    //assign the temp pointer
            {
                for (int i = 0; i < entriesread; i++)    //loop through
the entries
                {
                    try
                    {
                        //the lpszServerName member of the structure
contains a pointer to
                        //the server name string...marshal the pointer
from unmanaged
                        //memory to a managed string

cboServers.Items.Add(Marshal.PtrToStringAuto(pTmp->lpszServerName));
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message) ;
                    }
                    pTmp++;        // increment the
pointer...essentially move to the next
                                   // structure in the array
                }
            }
        }
        NetApiBufferFree((uint)si);
    }            

Quote:
}

*** Sent via Developersdex http://www.*-*-*.com/ ***
Don't just participate in USENET...get rewarded for it!


Sat, 05 Feb 2005 00:56:06 GMT  
 Help Converting C# NetServerEnum code
Paul,

Quote:
>I get as
>far as the NetServerEnum, but am lost on the
>"[marshal(UnmanagedType.LPVoid)]uint* bufptr" as the mashal type doesn't
>seem to exist.

That should be the MarshalAs attribute, located in the
System.Runtime.InteropServices namespace. But the attribute doesn't
make any different here anyway, so you can just ignore it. When moving
the code to VB.NET, you would use IntPtr to replaec the pointer types.

Mattias

===
Mattias Sj?gren (VB MVP)

http://www.msjogren.net/dotnet/



Sat, 05 Feb 2005 05:03:45 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Convert VB.Net to C#, convert C# to VB.Net

2. Help ?? Convert C# code into VB

3. Help converting C# code into VB

4. Convert the following C# code to VB please

5. Code Convert from C# to VB.Net

6. Convert VB.NET code to C#?

7. Tell me you experience in convert code from VB 6 to VB.NET or C#

8. Need help converting a C# function to VB

9. Is it possible to distinguish VB code from C# code once compiled

10. Drill Down support from HTML code to C# and VB object code in Beta 2

11. NetServerEnum API Source code required

12. NetServerEnum code example

 

 
Powered by phpBB® Forum Software