Need Help - NetServerEnum compile error 
Author Message
 Need Help - NetServerEnum compile error

Ihad found the article which can find sql server
connections but there are some compile error:

1) Unsafe code may only appear if compiling with /unsafe
2) No overload for method 'StructLayoutAttribute'
takes '0' arguments
3) 'System.Runtime.InteropServices.LayoutKind' does not
contain a defintion for 'sequential'
4) 'System.Runtime.InteropServices.CharSet' does not
contain a definition for 'auto'

What does all those errors mean? Thank you.

The C# code which i copy from Visual Studio .Net EA Everett
-----------------------------------------------------------
using System;
using System.Runtime.InteropServices;

namespace Test_Out_SQL_Conn
{
        /// <summary>
        /// Summary description for sqlservers.
        /// </summary>
        public class sqlservers
        {
                public sqlservers()
                {
                        //
                        // TODO: Add constructor logic here
                        //
                }

                [DllImport("kernel32.dll")]
                        private static extern uint
CopyMemory( [MarshalAs(UnmanagedType.AsAny)] object
Destination,
                        [MarshalAs(UnmanagedType.AsAny)]
object Source,
                        int length);

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

        [System.Runtime.InteropServices.StructLayoutAttribu
te (LayoutKind.sequential, CharSet=CharSet.auto)]
                        private struct SERVER_INFO_101
                {
                        public int dwPlatformID;
                        public System.IntPtr
lpszServerName;
                        public int dwVersionMajor;
                        public int dwVersionMinor;
                        public int dwType;
                        public int lpszComment;
                }

                private static ServerTypeEnum _serverType;

                public static ServerTypeEnum ServerType
                {
                        get
                        {
                                return _serverType;
                        }
                        set
                        {
                                _serverType = value;
                        }
                }

                public static string[] GetList(string
domain)
                {
                        string[] servers;
                        string serverName = null;
                        string domainName = domain;
                        uint level = 101, prefmaxlen =
0xFFFFFFFF, entriesread = 0, totalentries = 0;
                        uint resume_handle = 0;

                        try
                        {
                                unsafe
                                {
                                        // get a pointer
to the server info structure
                                        SERVER_INFO_101*
si = null;
                                        SERVER_INFO_101*
pTmp;   // temp pointer for use when loopingthrough
returned (pointer) array
                                        // this api
requires a pointer to a byte array..which is actually a
pointer to an array
                                        uint nRes =
NetServerEnum(serverName, level, (uint *) &si, prefmaxlen,
ref entriesread, ref totalentries, (uint)_serverType,
domainName, resume_handle);
                                        servers = new
string[entriesread];
                                        if (nRes == 0)
                                        {
                                                if ((pTmp
= si) != null)
                                                {

        for (int i = 0; i < entriesread; i++)
                                                        {

        try

        {

                servers[i] = Marshal.PtrToStringAuto(pTmp-

Quote:
>lpszServerName);

        }

        catch(Exception e)

        {

                string error = e.Message;

        }

        pTmp++;
                                                        }
                                                }
                                        }
                                }
                        }
                        catch(Exception /*e*/)
                        {
                                return null;
                        }
                        return servers;
                }

        }

        public enum ServerTypeEnum : uint
        {
                None = 0,
                ALL = 0xFFFFFFFF,
                WORKSTATION = 0x00000001,
                SERVER = 0x00000002,
                SQLSERVER = 0x00000004,
                DOMAIN_CTRL = 0x00000008,
                DOMAIN_BAKCTRL = 0x00000010,
                TIME_SOURCE = 0x00000020,
                AFP = 0x00000040,
                NOVELL = 0x00000080,
                DOMAIN_MEMBER = 0x00000100,
                PRINTQ_SERVER = 0x00000200,
                DIALIN_SERVER = 0x00000400,
                XENIX_SERVER = 0x00000800,
                NT = 0x00001000,
                WFW = 0x00002000,
                SERVER_MFPN = 0x00004000,
                SERVER_NT = 0x00008000,
                POTENTIAL_BROWSER = 0x00010000,
                BACKUP_BROWSER = 0x00020000,
                MASTER_BROWSER = 0x00040000,
                DOMAIN_MASTER = 0x00080000,
                SERVER_OSF = 0x00100000,
                SERVER_VMS = 0x00200000,
                WINDOWS = 0x00400000,   /* Windows 95 and
above */
                DFS = 0x00800000,               /* Root of
a DFS tree */
                CLUSTER_NT = 0x01000000,        /* NT
Clusters */
                DCE = 0x10000000,       /* IBM DSS
(Directory and Security Services) or equivalent */
                ALTERNATE_XPORT = 0x20000000,   /* Return
list of alternate transport */
                LOCAL_LIST_ONLY = 0x40000000,   /* Return
local list only */
                DOMAIN_ENUM = 0x80000000
        }

Quote:
}

Regards,
Chua Wen Ching


Tue, 17 May 2005 11:00:38 GMT  
 Need Help - NetServerEnum compile error
Chua Wen Ching,

Quote:
>1) Unsafe code may only appear if compiling with /unsafe

Compile with /unsafe option.

Quote:
>2) No overload for method 'StructLayoutAttribute'
>takes '0' arguments
>3) 'System.Runtime.InteropServices.LayoutKind' does not
>contain a defintion for 'sequential'
>4) 'System.Runtime.InteropServices.CharSet' does not
>contain a definition for 'auto'

C# is case sensitive. Make sure you get the case right for all
members. It should be Sequential and Auto.

You should also free the returned buffer with NetApiBufferFree().

Mattias

===

http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Tue, 17 May 2005 14:43:47 GMT  
 Need Help - NetServerEnum compile error
to compile unsafe code in visual studio:
1) goto project properties
2) Goto configuration properties
3) under build, set Allow Unsafe Code Blocks to true
that will let ya build with unsafe blocks


Quote:
> How can i compile in Visual Studio .Net with the
> option /unsafe? Do you ask me to compile with command
> prompt or???

> Thank you.

> Regards,
> Chua Wen Ching :D



Tue, 17 May 2005 18:03:49 GMT  
 Need Help - NetServerEnum compile error

Quote:
> I try to compile with visual studio .net 2003 command
> prompt by typing:

> csc /unsafe sqlservers.cs

> but it end up with an error:
> error CS5001: Program 'C:/.../sqlservers.exe' does not
> have an entry point defined.

> What does this error mean?

It means that you don't have an appropriate Main() method defined.

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Tue, 17 May 2005 18:23:18 GMT  
 Need Help - NetServerEnum compile error
Chua Wen Ching,

Quote:
>Oh i see. But i only want to create that sqlserver class
>and use it into my windows forms. So i need to put a
>temporary main in it?

No, you can compile it to a class library DLL that you can then
reference from your Winforms project.

Regarding NetApiBufferFree, you can declare and call it like this

[DllImport("netapi32.dll")]
unsafe static extern uint NetApiBufferFree(void* Buffer);

--

NetApiBufferFree(si);

Mattias

===

http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Tue, 17 May 2005 19:50:25 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Compile Error=> Need help from expert

2. Need help w/ compile error

3. Need help with compile error

4. compiling error, need help

5. VC 5.0 Compile Errors (compiles fine with 4.2)

6. need help compiling vc++6 project in vC++.Net

7. TIME FUNCTION -- NEED help compiling

8. I need help on this compile warning message..

9. HELP: Need Help on Error (Undefined Symbol...)

10. HELP: Need Help on Error (Undefined Symbol...)

11. Need Help compiling M

12. Need Help compiling Microsoft C with Borland C

 

 
Powered by phpBB® Forum Software