[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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
0
Farzad Akbarnejad
Asked:
Farzad Akbarnejad
  • 8
  • 6
1 Solution
 
Farzad AkbarnejadAuthor 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
 
jkrCommented:
BTW, see also the notes at http://win32.mvps.org/network/nse.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Farzad AkbarnejadAuthor 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 AkbarnejadAuthor 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 AkbarnejadAuthor 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 AkbarnejadAuthor 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 AkbarnejadAuthor 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 AkbarnejadAuthor Commented:
Hello,
Thanks for your help. I will ask other part of my question.

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

-FA
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now