Find name of all SQL Server in the LAN

Hello,
I need a simple C++ (Win32 API) code to find name of all SQL Server in the LAN and lists them for the user. The user select one of them and connect to its database (for example Northwind) and run a stored procedure on it.

Thanks
-FA
LVL 14
Farzad AkbarnejadDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Farzad AkbarnejadDeveloperAuthor Commented:
I need a code withoput using MFC. I want to use Win32 API and its DLL (NTWDBLIB.DLL) for SQL Server.

-FA
0
jkrCommented:
You can do that using 'NetServerEnum()' and checking for SV_TYPE_SQLSERVER - see http://win32.mvps.org/network/nse.c ("NetServerEnum"):

#include <stdio.h>
#include <windows.h>
#include <lm.h>
#pragma hdrstop



int main( void );
void print_si( SERVER_INFO_101 *pSI );
void build_typestr( DWORD type, char *str );



int main( void )
{
      //*********************************      change these:
      wchar_t *pPdcName = L"\\\\BABYA";      // server to run on -- I use my PDC
      wchar_t *pDomain = L"FOO";                  // domain to query
      DWORD wantedbytes = 256;                  // desired buffer size per call
      DWORD servertype = SV_TYPE_ALL;            // server types to ask for
      //*********************************

      BYTE *pbuf; // will be filled in by NSE()
      DWORD entriesread, totalentries; // this call, total
      DWORD resume; // resume handle -- leave this alone
      NET_API_STATUS rc; // result code
      SERVER_INFO_101 *pSI;
      DWORD i; // loop index for returned SERVER_INFO_101 array

      do
      {
            pbuf = NULL;
            
            rc = NetServerEnum( pPdcName, 101, &pbuf, wantedbytes, &entriesread,
                  &totalentries, servertype, pDomain, &resume );

            if ( ( rc == NERR_Success || rc == ERROR_MORE_DATA ) && pbuf != NULL )
            {
                  pSI = (SERVER_INFO_101 *) pbuf;
                  for ( i = 0; i < entriesread; i ++ )
                        print_si( &pSI[i] );
            }

            if ( pbuf != NULL )
                  NetApiBufferFree( pbuf );

      } while ( rc == ERROR_MORE_DATA );

      if ( rc != NERR_Success )
            printf( "Oops! Error %lu encountered!\n", rc );

      return 0;
}



void print_si( SERVER_INFO_101 *pSI )
{
      char typestr[256];

      build_typestr( pSI->sv101_type, typestr );

      printf( "%-16.16S  %2lu.%02lu  %-.54s\n", pSI->sv101_name,
            pSI->sv101_version_major & MAJOR_VERSION_MASK,
            pSI->sv101_version_minor, typestr );
}



void build_typestr( DWORD type, char *str )
{
      char *t;
      struct _type_t {
            DWORD flag;
            char *str;
      } *curtype, types[] = {
            { SV_TYPE_WORKSTATION, "WS" },
            { SV_TYPE_SERVER, "SRV" },
            { SV_TYPE_SQLSERVER, "SQL" },
            { SV_TYPE_DOMAIN_CTRL, "PDC" },
            { SV_TYPE_DOMAIN_BAKCTRL, "BDC" },
            { SV_TYPE_AFP, "AFP" },
            { SV_TYPE_NOVELL, "NW" },
            { SV_TYPE_DOMAIN_MEMBER, "MEMB" },
            { SV_TYPE_LOCAL_LIST_ONLY, "LOCAL" },
            { SV_TYPE_XENIX_SERVER, "XENIX" },
            { SV_TYPE_NT, "NT" },
            { SV_TYPE_WFW, "WFW" },
            { SV_TYPE_SERVER_NT, "NTS" },
            { SV_TYPE_POTENTIAL_BROWSER, "PBR" },
            { SV_TYPE_BACKUP_BROWSER, "BBR" },
            { SV_TYPE_MASTER_BROWSER, "MBR" },
            { SV_TYPE_DOMAIN_MASTER, "DMBR" },
            { SV_TYPE_DOMAIN_ENUM, "PDOM" },
            { SV_TYPE_WINDOWS, "WIN" },
            { SV_TYPE_ALL, "Sentinel" }
      };

      t = str;
      for ( curtype = &types[0]; curtype->flag != SV_TYPE_ALL; curtype ++ )
      {
            if ( ( type & curtype->flag ) == curtype->flag ) // match?
            {
                  if ( t != str )
                        *( t ++ ) = ' ';
                  strcpy( t, curtype->str );
                  t += strlen( t );
            }
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
BTW, see also the notes at http://win32.mvps.org/network/nse.html
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Farzad AkbarnejadDeveloperAuthor Commented:
Hello,
Thanks for yor help. I paste your code in a Win32 Console Application in Visual C++ 6.0. I couldn't compile it.
Also I need to connect to SQL Server's Northwind database and run a stored procedure.

Thanks
-FA

Compiling...
FindSQLServer.cpp
Linking...
FindSQLServer.obj : error LNK2001: unresolved external symbol _NetApiBufferFree@4
FindSQLServer.obj : error LNK2001: unresolved external symbol _NetServerEnum@36
Debug/FindSQLServer.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

FindSQLServer.exe - 3 error(s), 0 warning(s)
0
jkrCommented:
You need to link with netapi32.lib - just place

#pragma comment(lib,"netapi32.lib")

in your code or add that .lib to your project.
0
Farzad AkbarnejadDeveloperAuthor Commented:
Hello,
I compiled it but I get the following error message in a dialog box when I execute program:

    Error Starting Program
    ----------------------

    !  The FINDSQLSERVER.EXE file is linked to missing
       export NETAPI32.DLL:NETServerEnum

                            | OK |

I am using:

Windows 98
SQL Server 2000 (8.0) Personal Edition
Visual Studio 6.0 (Visual C++ 6.0)

Thanks
-FA

0
jkrCommented:
Oh, on Win98, that's totally different. let me dig that up...
0
Farzad AkbarnejadDeveloperAuthor Commented:
Hello,
So I will test the above code tomorrow in the office on WinXP (& Winodws 2000 Server) PCs.

Thanks for your help,
-FA
0
jkrCommented:
Actually, it will definitely work on XP/W2k/NT. However, Win98 is not an option, see e.g. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp

Client: Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation.
Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header: Declared in Lmserver.h; include Lm.h.
Library: Use Netapi32.lib.


0
Farzad AkbarnejadDeveloperAuthor Commented:
CAN I test it with WinXPs? My SQL Server is Personal Edition on WinXP. Also I have other Enterprise Edition on Windows 2000 Server.

-FA
0
jkrCommented:
Yes, you definitely can test that on XP, NT and W2k. Just Win9x is not an option.
0
Farzad AkbarnejadDeveloperAuthor Commented:
Thanks. I works. My question had another part:
"connect to its database (for example Northwind) and run a stored procedure on it." (I think that I must use NTWDBLIB.DLL)
Do you want to copy it as a new question and new points?

Thanks
-FA

0
Farzad AkbarnejadDeveloperAuthor Commented:
Hello,
Thanks for your help. I will ask other part of my question.

Thanks
-FA
0
Farzad AkbarnejadDeveloperAuthor Commented:
Sorry for delay in accepting answer.

-FA
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.