Solved

Small program to get server OS name

Posted on 2011-03-01
10
1,139 Views
Last Modified: 2012-05-11
I got this small C source code and compiled with GCC, in order to get server OS information, using data stored in ServerMessegeBlock (SMB) APIs.

When I call it with no parameters, the code correctily shows OS from current machine:
    "GetOSName.exe      ENTER"

But when a run the utility passing server name, no information is printed:
    "GetOSName.exe   \\MYSERVER    ENTER"

So, what is wrong with this code ?
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")

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

int main(int argc, wchar_t *argv[])
{
   DWORD dwLevel = 101;
   LPSERVER_INFO_101 pBuf = NULL;
   NET_API_STATUS nStatus;
   LPTSTR pszServerName = NULL;

   if (argc > 2)
   {
      fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);
      exit(1);
   }

   // The server is not the default local computer.
   //
   if (argc == 2)
      pszServerName = (LPTSTR) argv[1];

   //
   // Call the NetServerGetInfo function, specifying level 101.
   //
   nStatus = NetServerGetInfo(pszServerName,
                              dwLevel,
                              (LPBYTE *)&pBuf);
   //
   // If the call succeeds,
   //
   if (nStatus == NERR_Success)
   {
      //
      // Check for the type of server.
      //

      printf("sv101_type..........: %d\n", pBuf->sv101_type);
      wprintf(L"sv101_name..........: %s\n", pBuf->sv101_name);
      printf("sv101_platform_id...: %d\n", pBuf->sv101_platform_id);
      printf("sv101_version_major.: %d\n", pBuf->sv101_version_major);
      printf("sv101_version_minor.: %d\n", pBuf->sv101_version_minor);
      wprintf(L"sv101_comment.......: %s\n", pBuf->sv101_comment);


      if ((pBuf->sv101_type & SV_TYPE_DOMAIN_CTRL) ||
         (pBuf->sv101_type & SV_TYPE_DOMAIN_BAKCTRL) ||
         (pBuf->sv101_type & SV_TYPE_SERVER_NT))
         printf("This is a server\n");
      else
         printf("This is a workstation\n");
   }
   //
   // Otherwise, print the system error.
   //
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Free the allocated memory.
   //
   if (pBuf != NULL)
      NetApiBufferFree(pBuf);

   return 0;
}

Open in new window

0
Comment
Question by:MarcosBarroso
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:denissie
ID: 35012397
Hi MacrosBarroso,

Could you please add the output of your program when using the server name?
Do you get the ""A system error has occurred" message?

Thanks.
0
 

Author Comment

by:MarcosBarroso
ID: 35016260
If I use  
    "GetOSName.exe   \\MYSERVER    ENTER"     or
    "GetOSName.exe   MYSERVER    ENTER"

Then i get value 53 returned to variable nStatus:  
    " A system error has occurred: 53"

Thanks

Marcos

0
 

Author Comment

by:MarcosBarroso
ID: 35016433
I think my error is in line 26:
     "pszServerName = (LPTSTR) argv[1];"

Maybe wrong cast or string /pointer type, because if I don't pass any parameter, information about current machine is showed ok.

Marcos


0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 35016484
error 53 means "bad network path"

the servername passed to NetServerGetInfo must be LPWSTR ( == wchar_t *)  while LPTSTR can be either LPSTR (==char*) or LPWSTR depending on the defined character set of the project. try to pass L"MYSERVER"  to check that issue.

Sara
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35016503
I'll go out on a limb and make a fool of myself, but doesn't function main have char *argv, not wchar_t:

int main(int argc, char *argv[])

Open in new window

0
 
LVL 33

Expert Comment

by:sarabande
ID: 35016590
a wizard generated win32 console project in Visual Studio has function

   int _tmain(int argc, LPTSTR argv[]);

as main function where the LPTSTR either is char* or wchar_t*

Sara
0
 
LVL 4

Expert Comment

by:denissie
ID: 35021922
The WinAPI "NetServerGetInfo" uses LPWSTR servername as server name's input.
Therefore, you should use unicode.

As might be seen in MSDn code example (http://msdn.microsoft.com/en-us/library/aa370624%28v=vs.85%29.aspx), use "int wmain(int argc, wchar_t *argv[])".

BTW:Your code looks a lot like that code example. Might it be that you've omitted to copy the "int wmain..." line?
Could you change that and try again?

Thanks.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35022135
MarcosBarroso:  Did you try my simple change on the "main" line just to see if it works?

sara:  He's using "main", not "_tmain".  I think it makes a difference. The raw "main" is the same in C and C++:

http://en.wikipedia.org/wiki/Main_function
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35022662
I knew I'd be the fool in this discussion.   Good going sarabande.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 35024967
sjklein42, i like your humour :-)

you rightly stated that marcosborroso used wrong argument wchar_t* as type for second argument.

Sara
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
romain user profiles for windows users 22 78
SBS 2008 Standard OEL 2 28
Mysterious disks wanting to be formatted 6 35
Active Directory uninstall Windows 2008 R2 6 79
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now