Thread: NetBIOS APIs

  1. #1
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18

    NetBIOS APIs

    Hi,

    I'm looking for documentation on the Windows Network APIs. In particular, I want to query for the names of all "visible" computers on the network. I've been googling away, but just keep getting Samba referrals!!! I don't know if this information is held locally on my machine, or if I have to query, for example, a WINS server, but in either case I need to know how to achive it!

    Apologies for cross-posting, but my thread on the Networking forum has returned a workable, but not particularly elegant solution! (Ping all the addresses in the current network address range. Do a port scan on those that respond to look for SMB ports open and finally do a reverse DNS lookup to get the name.....)

    Many thanks,

    Paul

    P.S. I'm using Code::Blocks and wxWidgets, if that makes any difference.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Give the following a try to see if it works for you.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #pragma comment(lib,"mpr")
    #pragma comment(lib,"wsock32")
    
    int main(void)
    {
    	NETRESOURCE *pNetworkResource = NULL;
    	HANDLE hEnumerate; 
    	DWORD dwResult;
    	DWORD dwSize = 16386;
    	DWORD dwNumber = -1;
    	char pszGroupName[255] = {0};
    	int iLength;
    	struct hostent* host = NULL;
    
    	WSADATA wsaData;
    	if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0 )
    	{
    		printf("WSAStartup failed\n");
    		return -1;
    	}
    	if(WNetOpenEnum(RESOURCE_CONTEXT,RESOURCETYPE_ANY,0,NULL,&hEnumerate) != 0)
    	{
    		printf("WNetOpenEnum failed\n");
    		WSACleanup();
    		return -1;
    	}
    	pNetworkResource=(LPNETRESOURCE)GlobalAlloc(GPTR, dwSize);
    	if( pNetworkResource == NULL)
    	{
    		printf("GlobalAlloc failed\n");
    		WNetCloseEnum(hEnumerate);
    		WSACleanup();
    		return -1;  
    	}
    	WNetEnumResource( hEnumerate, &dwNumber, pNetworkResource, &dwSize);
    	for (DWORD dwIndex = 0; dwIndex < dwNumber; dwIndex++)
    	{
    		if (pNetworkResource[dwIndex].lpRemoteName==NULL)
    			continue;
    		iLength = strlen(pNetworkResource[dwIndex].lpRemoteName);
    		memset(&pszGroupName, 0, sizeof(pszGroupName));
    		// Remove the "\\" prefix 
    		memcpy(pszGroupName, pNetworkResource[dwIndex].lpRemoteName + 2, iLength - 2);
    		host = gethostbyname(pszGroupName);
    		if(host != NULL)
    			printf("%s  %s\n", host->h_name, inet_ntoa( *( in_addr *)host->h_addr) );
    	}
    	GlobalFree(pNetworkResource);
    	WNetCloseEnum(hEnumerate);
    	WSACleanup();
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18
    Thanks for that

    I tried putting this into Code::Blocks as a console application. When I compiled I got:

    Code:
    C:\Sourcecode\computers\main.cpp     3    warning:  ignoring #pragma comment
    C:\Sourcecode\computers\main.cpp     4    warning:  ignoring #pragma comment
    C:\Sourcecode\computers\main.cpp          In function 'main':
    C:\Sourcecode\computers\main.cpp     12   warning:  converting of negative value '-0x00000001' to 'DWORD'
    C:\Sourcecode\computers\main.cpp     10   warning:  unused variable 'dwResult'
    obj\Debug.main.o                          In function 'main':
    C:\Sourcecode\computers\main.cpp     18   undefined reference to '_WSAStartup@8'
    C:\Sourcecode\computers\main.cpp     23   undefined reference to '_WNetOpenEnumA@20'
    C:\Sourcecode\computers\main.cpp     26   undefined reference to '_WSACleanup@0'
    C:\Sourcecode\computers\main.cpp     33   undefined reference to '_WNetCloseEnum@4'
    C:\Sourcecode\computers\main.cpp     34   undefined reference to '_WSACleanup@0'
    C:\Sourcecode\computers\main.cpp     37   undefined reference to '_WNetEnumResource@16'
    C:\Sourcecode\computers\main.cpp     46   undefined reference to '_gethostbyname@4'
    C:\Sourcecode\computers\main.cpp     48   undefined reference to '_inet_ntoa@4'
    C:\Sourcecode\computers\main.cpp     51   undefined reference to '_WNetCloseEnum@4'
    C:\Sourcecode\computers\main.cpp     52   undefined reference to '_WSACleanup@0'
                                              === Build Finished: 10 errors, 4  warnings ===
    I had a play and found I needed to add the wsock32 and mpr libraries to the build myself. Is that correct? Or should the #pragma statements have done it for me?

    I'm fairly new to C++ and windows programming, although have experience with other languages. I'm assuming I should have been able to just compile?

    Anyway, it worked and did exactly what I need it to do

    Thanks

    Paul
    Last edited by wierdbeard65; 06-30-2008 at 02:39 AM.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Link the needed libraries, mpr and winsock. Ensure that -lmpr and -lws2_32 get passed to the linker. (Probably under linker options, or something. RTFM for Code::Blocks, it should tell you)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18
    Thanks CH, I kind of managed to (eventually) work that out (hence my edit) although I didn't know about the command-line options being passed to the linker....

    I'll have a look at the docs, but sometimes it's difficult to know if it's a Code::Blocks issue, a compiler (MinGW) issue, a basic C++ issue or, with my main app, a wxWidgets issue!!!!

    Once I get my head around this stuff, I'm sure I'll be fine (java took a few weeks too ) but at the moment........

    Cheers,

    Paul

  6. #6
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    These are specs for the SMB Protocol:
    http://msdn.microsoft.com/en-us/library/cc246231.aspx
    http://msdn.microsoft.com/en-us/library/cc246482.aspx

    And here you can find all other Windows Communication Protocols:
    http://msdn.microsoft.com/en-us/library/cc216513.aspx

    Took ~5 minutes of searching.. if you know where you have to look

    Edit:
    These might also be helpful:
    http://msdn.microsoft.com/en-us/library/cc197979.aspx
    Last edited by mkruk; 07-01-2008 at 06:03 AM.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18
    if you know where you have to look
    That's the key though, isn't it? All I could find on Google was loads of references to SAMBA and to the existence of an API!

    Anyway, thanks for that - all useful stuff.

    Paul

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to copy binary files using Unix API's
    By rohan_ak1 in forum C Programming
    Replies: 25
    Last Post: 05-07-2008, 09:12 AM
  2. Any good windows system APIs?
    By taelmx in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2006, 12:43 AM
  3. CD Player using waveOutXXX APIs?
    By loobian in forum C++ Programming
    Replies: 6
    Last Post: 12-19-2003, 12:52 AM
  4. Hi all!, Another question about socket's & NetBios
    By Pandora in forum Windows Programming
    Replies: 9
    Last Post: 03-19-2003, 08:10 AM
  5. Those darn API's
    By Zahl in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-04-2003, 10:27 AM