rewrite of gettype.  Beginnings of support for remote computers.  Big thanks to Alex and KJK.
Modified: trunk/reactos/subsys/system/gettype/gettype.c
Modified: trunk/reactos/subsys/system/gettype/gettype.xml

Modified: trunk/reactos/subsys/system/gettype/gettype.c
--- trunk/reactos/subsys/system/gettype/gettype.c	2005-11-03 01:50:13 UTC (rev 18970)
+++ trunk/reactos/subsys/system/gettype/gettype.c	2005-11-03 02:36:09 UTC (rev 18971)
@@ -15,6 +15,8 @@
 #include <windows.h>
 #include <tchar.h>
 #include <stdio.h>
+#include <lm.h>
+#include <shellapi.h>
 
 enum
 {
@@ -28,33 +30,164 @@
 	GETTYPE_BUILD	= 0x080
 };
 
-void GetBasicInfo(OSVERSIONINFOEX osvi, char * HostName, char * OSName, char * Version, char * Role, char * Components)
+INT 
+GetVersionNumber(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
 {
+	INT VersionNumber = 255;
+	if(pBuf102 != NULL && !bLocal)
+	{
+		VersionNumber = pBuf102->sv102_version_major * 1000;
+		VersionNumber += (pBuf102->sv102_version_minor * 100);
+	}
+	else if(bLocal)
+	{		
+		VersionNumber = osvi->dwMajorVersion * 1000;
+		VersionNumber += (osvi->dwMinorVersion * 100);
+	}
+
+	return VersionNumber;
+}
+
+INT 
+GetMajValue(BOOL Major, BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
+{
+	INT VersionNumber = 255;
+	if(pBuf102 != NULL && !bLocal)
+	{
+		if(Major)
+			VersionNumber = pBuf102->sv102_version_major * 1000;
+		else
+			VersionNumber = pBuf102->sv102_version_minor * 100;
+	}
+	else
+	{
+		if(Major)
+			VersionNumber = osvi->dwMajorVersion * 1000;
+		else
+			VersionNumber = osvi->dwMinorVersion * 100;
+	}
+	return VersionNumber;
+}
+
+INT 
+GetSystemRole(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
+{
+
+	if(pBuf102 != NULL && !bLocal)
+	{
+		if ((pBuf102->sv102_type & SV_TYPE_DOMAIN_CTRL) ||
+			(pBuf102->sv102_type & SV_TYPE_DOMAIN_BAKCTRL))
+			return 1;
+		else if(pBuf102->sv102_type & SV_TYPE_SERVER_NT)
+			return 2;
+		else
+			return 3;
+	}
+	else
+	{
+		if(osvi->wProductType == VER_NT_DOMAIN_CONTROLLER)
+			return 1;
+		else if(osvi->wProductType == VER_NT_SERVER)
+			return 2;
+		else if(osvi->wProductType == VER_NT_WORKSTATION)
+			return 3;
+	}
+	return 255;
+
+}
+
+INT 
+GetServicePack(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102, TCHAR * Server)
+{
+	INT SPNumber = 255;
+	if(!bLocal)
+	{
+		/* FIXME: Use Registry to get value */
+	}
+	else
+	{
+		SPNumber = osvi->wServicePackMajor;
+	}
+	return SPNumber;
+}
+
+INT 
+GetBuildNumber(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
+{
+	INT BuildNum = 255;
+	if(!bLocal)
+	{
+		/* FIXME: Use Registry to get value */
+	}
+	else
+	{
+		BuildNum = osvi->dwBuildNumber;
+	}
+	return BuildNum;
+}
+
+INT GetType(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
+{
+	if(bLocal)
+	{
+		if(osvi->dwMajorVersion == 5)
+		{
+			if(osvi->dwMinorVersion == 1)
+			{
+				if(osvi->wSuiteMask & VER_SUITE_PERSONAL)
+					return 1;
+				else
+					return 2;
+			}
+			else if(osvi->dwMinorVersion == 2)
+			{
+				if(osvi->wSuiteMask & VER_SUITE_BLADE)
+					return 6;
+				else if(osvi->wSuiteMask & VER_SUITE_DATACENTER)
+					return 5;
+				else if(osvi->wSuiteMask & VER_SUITE_ENTERPRISE)
+					return 4;
+				else 
+					return 3;
+			}
+		}
+	}
+	else
+	{
+		/* FIXME: Get this value from registry */
+	}
+	return 255;
+}
+
+VOID 
+GetBasicInfo(LPOSVERSIONINFOEX osvi, TCHAR * HostName, TCHAR * OSName, TCHAR * Version, TCHAR * Role, TCHAR * Components)
+{
 	/* Host Name - COMPUTERNAME*/
 	DWORD  bufCharCount = 1024;
 	GetComputerName(HostName, &bufCharCount);
 
+
 	/* OSName - Windows XP Home Editition */
-	if(osvi.dwMajorVersion == 4)
+	if(osvi->dwMajorVersion == 4)
 	{
 		_tcscpy(OSName, _T("Microsoft Windows NT 4.0 "));
 	}
-	else if(osvi.dwMajorVersion == 5)
+	else if(osvi->dwMajorVersion == 5)
 	{
-		if(osvi.dwMajorVersion == 0)
+		if(osvi->dwMajorVersion == 0)
 		{
 			_tcscpy(OSName, _T("Microsoft Windows 2000 "));
 		}
-		else if(osvi.dwMinorVersion == 1)
+		else if(osvi->dwMinorVersion == 1)
 		{
 			_tcscpy(OSName, _T("Microsoft Windows XP "));
 		}
-		else if(osvi.dwMinorVersion == 2)
+		else if(osvi->dwMinorVersion == 2)
 		{
 			_tcscpy(OSName, _T("Microsoft Windows Server 2003 "));
 		}
 	}
-	else if(osvi.dwMajorVersion == 6)
+	else if(osvi->dwMajorVersion == 6)
 	{
 		_tcscpy(OSName, _T("Microsoft Windows Vista "));
 	}
@@ -63,169 +196,199 @@
 		_tcscpy(OSName, _T("Microsoft Windows "));
 	}
 
-	if(osvi.wSuiteMask & VER_SUITE_BLADE)
+	if(osvi->wSuiteMask & VER_SUITE_BLADE)
 		_tcscat(OSName, _T("Web Edition"));
-	if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
+	if(osvi->wSuiteMask & VER_SUITE_DATACENTER)
 		_tcscat(OSName, _T("Datacenter"));
-	if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
+	if(osvi->wSuiteMask & VER_SUITE_ENTERPRISE)
 		_tcscat(OSName, _T("Enterprise"));
-	if(osvi.wSuiteMask & VER_SUITE_EMBEDDEDNT)
+	if(osvi->wSuiteMask & VER_SUITE_EMBEDDEDNT)
 		_tcscat(OSName, _T("Embedded"));
-	if(osvi.wSuiteMask & VER_SUITE_PERSONAL)
+	if(osvi->wSuiteMask & VER_SUITE_PERSONAL)
 		_tcscat(OSName, _T("Home Edition"));
-	if(osvi.wSuiteMask & VER_SUITE_SMALLBUSINESS_RESTRICTED && osvi.wSuiteMask & VER_SUITE_SMALLBUSINESS)
+	if(osvi->wSuiteMask & VER_SUITE_SMALLBUSINESS_RESTRICTED && osvi->wSuiteMask & VER_SUITE_SMALLBUSINESS)
 		_tcscat(OSName, _T("Small Bussiness Edition"));
 
 	/* Version - 5.1 Build 2600 Serivce Pack 2 */
-	_stprintf(Version, _T("%d.%d Build %d %s"),(int)osvi.dwMajorVersion,(int)osvi.dwMinorVersion,(int)osvi.dwBuildNumber, osvi.szCSDVersion);
+	_stprintf(Version, _T("%d.%d Build %d %s"),(int)osvi->dwMajorVersion,(int)osvi->dwMinorVersion,(int)osvi->dwBuildNumber, osvi->szCSDVersion);
 
 	/* Role - Workgroup / Server / Domain Controller */
-	if(osvi.wProductType == VER_NT_DOMAIN_CONTROLLER)
+	if(osvi->wProductType == VER_NT_DOMAIN_CONTROLLER)
 		_tcscpy(Role, _T("Domain Controller"));
-	else if(osvi.wProductType == VER_NT_SERVER)
+	else if(osvi->wProductType == VER_NT_SERVER)
 		_tcscpy(Role, _T("Server"));
-	else if(osvi.wProductType == VER_NT_WORKSTATION)
+	else if(osvi->wProductType == VER_NT_WORKSTATION)
 		_tcscpy(Role, _T("Workgroup"));
 
-	/* Components - N/A */
-	BOOL bCompInstalled = FALSE;
-	_tcscpy(Components, "");
-	if(osvi.wSuiteMask & VER_SUITE_BACKOFFICE)
-	{
-		_tcscat(Components, _T("Microsoft BackOffice"));
-		bCompInstalled = TRUE;
-	}
-	if(osvi.wSuiteMask & VER_SUITE_TERMINAL)
-	{
-		if(bCompInstalled)
-			_tcscat(OSName, ";");
-		_tcscat(Components, _T("Terminal Services"));
-		bCompInstalled = TRUE;
-	}
-	if(!bCompInstalled)
-		_tcscat(Components, _T("Not Installed"));
+	/* Components - FIXME: what is something that might be installed? */
+	_tcscat(Components, _T("Not Installed"));
 
 }
-int main (int argc, char *argv[])
+
+INT
+main (VOID)
 {
 	DWORD Operations = 0;
+	INT ret = 255;
 	INT i = 0;
-	OSVERSIONINFOEX osvi;
-	
-	/* get the struct we will pull all the info from */
-	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
-	if(!GetVersionEx((OSVERSIONINFO*)&osvi))
-		return 255;
+	INT argc = 0;
+	/* True if the target is local host */
+	BOOL bLocal = TRUE;
+	DWORD nStatus = 0;
+	TCHAR ServerName[32];
+	TCHAR RemoteResource[32];
+	TCHAR UserName[32] = _T("");
+	TCHAR Password[32] = _T("");
+	LPOSVERSIONINFOEX osvi = NULL;
+	LPSERVER_INFO_102 pBuf102 = NULL;
+	LPTSTR * argv;
+	osvi = (LPOSVERSIONINFOEX)malloc(sizeof(LPOSVERSIONINFOEX));
+	pBuf102 = (LPSERVER_INFO_102)malloc(sizeof(LPSERVER_INFO_102));
 
-	/* no params, just print some info */
-	if(argc == 1)
+	/* Get the command line correctly since it is unicode */
+	argv = CommandLineToArgvW(GetCommandLineW(), &argc);
+
+
+	/* Process flags */
+	if(argc)
 	{
-		TCHAR HostName[1024] = _T("");
-		TCHAR OSName[1024] = _T("");
-		TCHAR VersionInfo[1024] = _T("");
-		TCHAR Role[1024] = _T("");
-		TCHAR Components[1024] = _T("");
-		GetBasicInfo(osvi, HostName, OSName, VersionInfo, Role, Components);
-		_tprintf(_T("\nHostname: %s\nName: %s\nVersion: %s\nRole: %s\nComponent: %s\n"),HostName, OSName, VersionInfo, Role, Components);
-		return 0;
+		for (i = 1; i < argc; i++)
+		{
+			if(!_tcsicmp(argv[i], _T("/ROLE")) && !Operations)
+				Operations |= GETTYPE_ROLE;
+			else if(!_tcsicmp(argv[i], _T("/VER")) && !Operations)
+				Operations |= GETTYPE_VER;
+			else if(!_tcsicmp(argv[i], _T("/MAJV")) && !Operations)
+				Operations |= GETTYPE_MAJV;
+			else if(!_tcsicmp(argv[i], _T("/MINV")) && !Operations)
+				Operations |= GETTYPE_MINV;
+			else if(!_tcsicmp(argv[i], _T("/SP")) && !Operations)
+				Operations |= GETTYPE_SP;
+			else if(!_tcsicmp(argv[i], _T("/BUILD")) && !Operations)
+				Operations |= GETTYPE_BUILD;
+			else if(!_tcsicmp(argv[i], _T("/TYPE")) && !Operations)
+				Operations |= GETTYPE_TYPE;
+			else if(!_tcsicmp(argv[i], _T("/?")) && !Operations)
+				Operations |= GETTYPE_HELP;
+			else if(!_tcsicmp(argv[i], _T("/S")) && i + 1 < argc)
+			{
+				_tcscpy(ServerName,argv[++i]);
+				bLocal = FALSE;
+			}
+			else if(!wcsicmp(argv[i], L"/U") && i + 1 < argc)
+				_tcscpy(UserName,argv[++i]);
+			else if(!wcsicmp(argv[i], L"/P") && i + 1 < argc)
+				_tcscpy(Password,argv[++i]);
+			else
+			{
+				wprintf(L"Error in paramters, please see usage\n");
+				return 255;
+			}
+		}
 	}
 
-	/* read the commands */
-	for (i = 1; i < argc; i++)
+	/* Some debug info */
+	//_tprintf(_T("%s - %s - %s - %d"), ServerName, UserName, Password, (int)Operations);
+
+	if(!bLocal)
 	{
-		if(!_tcsicmp(argv[i], _T("/ROLE")))
-			Operations |= GETTYPE_ROLE;
-		else if(!_tcsicmp(argv[i], _T("/VER")))
-			Operations |= GETTYPE_VER;
-		else if(!_tcsicmp(argv[i], _T("/MAJV")))
-			Operations |= GETTYPE_MAJV;
-		else if(!_tcsicmp(argv[i], _T("/MINV")))
-			Operations |= GETTYPE_MINV;
-		else if(!_tcsicmp(argv[i], _T("/SP")))
-			Operations |= GETTYPE_SP;
-		else if(!_tcsicmp(argv[i], _T("/BUILD")))
-			Operations |= GETTYPE_BUILD;
-		else if(!_tcsicmp(argv[i], _T("/TYPE")))
-			Operations |= GETTYPE_TYPE;
-		else if(!_tcsicmp(argv[i], _T("/?")))
-			Operations |= GETTYPE_HELP;
-		else
+		NETRESOURCE nr;
+
+
+		/* \\*IP or Computer Name*\*Share* */
+		_stprintf(RemoteResource, _T("\\\\%s\\IPC$"), ServerName);
+
+		nr.dwType = RESOURCETYPE_ANY;
+		nr.lpLocalName = NULL;
+		nr.lpProvider= NULL;
+		nr.lpRemoteName	= RemoteResource;
+
+		/* open a connection to the server with difference user/pass. */
+		nStatus = WNetAddConnection2(&nr, UserName[0]?UserName:NULL,Password[0]?Password:NULL, CONNECT_INTERACTIVE | CONNECT_COMMANDLINE);
+
+		if(nStatus != NO_ERROR)
 		{
-			_tprintf(_T("Unsupported parameter"));
+			_tprintf(_T("Error:%d-%d\n"),(int)nStatus,GetLastError());
 			return 255;
 		}
 	}
 
-	/* preform the operations */
+	/* Use GetVersionEx for anything that we are looking for locally */
+	if(bLocal)
+	{
+		osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
+		if(!GetVersionEx((LPOSVERSIONINFO)osvi))
+		{
+			_tprintf(_T("Failed to get local information\n"));
+			return 255;
+		}
+	}
+	else
+	{
+		nStatus = NetServerGetInfo(NULL,102,(LPBYTE *)&pBuf102);
+		if (nStatus != NERR_Success)
+		{
+			_tprintf(_T("Failed to connection to remote machine\n"));
+			return 255;
+		}
+
+	}
+
 	if(Operations & GETTYPE_VER)
 	{
-		INT VersionNumber = 0;
-		VersionNumber = osvi.dwMajorVersion * 1000;
-		VersionNumber += (osvi.dwMinorVersion * 100);
-		return VersionNumber;
+		ret = GetVersionNumber(bLocal, osvi, pBuf102);
 	}
 	else if(Operations & GETTYPE_MAJV)
 	{
-		INT VersionNumber = 0;
-		VersionNumber = osvi.dwMajorVersion;
-		return VersionNumber;
+		ret = GetMajValue(TRUE, bLocal, osvi, pBuf102);
 	}
 	else if(Operations & GETTYPE_MINV)
 	{
-		INT VersionNumber = 0;
-		VersionNumber = osvi.dwMinorVersion;
-		return VersionNumber;
+		ret = GetMajValue(FALSE, bLocal, osvi, pBuf102);
 	}
 	else if(Operations & GETTYPE_ROLE)
 	{
-		if(osvi.wProductType == VER_NT_DOMAIN_CONTROLLER)
-			return 1;
-		else if(osvi.wProductType == VER_NT_SERVER)
-			return 2;
-		else if(osvi.wProductType == VER_NT_WORKSTATION)
-			return 3;
+		ret = GetSystemRole(bLocal, osvi, pBuf102);
 	}
 	else if(Operations & GETTYPE_SP)
 	{
-		INT SPNumber = 0;
-		SPNumber = osvi.wServicePackMajor;
-		return SPNumber;
+		ret = GetServicePack(bLocal, osvi, pBuf102, ServerName);
 	}
 	else if(Operations & GETTYPE_BUILD)
 	{
-		INT BuildNumber = 0;
-		BuildNumber = osvi.dwBuildNumber;
-		return BuildNumber;
+		ret = GetBuildNumber(bLocal, osvi, pBuf102);
 	}
 	else if(Operations & GETTYPE_TYPE)
 	{
-		if(osvi.dwMajorVersion == 5)
-		{
-			if(osvi.dwMinorVersion == 1)
-			{
-				if(osvi.wSuiteMask & VER_SUITE_PERSONAL)
-					return 1;
-				else
-					return 2;
-			}
-			else if(osvi.dwMinorVersion == 2)
-			{
-				if(osvi.wSuiteMask & VER_SUITE_BLADE)
-					return 6;
-				else if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
-					return 5;
-				else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
-					return 4;
-				else 
-					return 3;
-			}
-		}
+		ret = GetType(bLocal, osvi, pBuf102);
 	}
 	else if(Operations & GETTYPE_HELP)
 	{
-		_tprintf(_T("GETTYPE  [/ROLE | /SP | /VER | /MAJV | /MINV | /TYPE | /BUILD]"));
-		return 0;
+		wprintf(L"GETTYPE  [/ROLE | /SP | /VER | /MAJV | /MINV | /TYPE | /BUILD]");
+		ret = 0;
 	}
+	else if(!Operations && bLocal)
+	{
+		/* FIXME: what happens when no flags except remote machine, does it
+		it print this info for the remote server? */
+		TCHAR HostName[1024] = _T("");
+		TCHAR OSName[1024] = _T("");
+		TCHAR VersionInfo[1024] = _T("");
+		TCHAR Role[1024] = _T("");
+		TCHAR Components[1024] = _T("");
+		GetBasicInfo(osvi, HostName, OSName, VersionInfo, Role, Components);
+		_tprintf(_T("\nHostname: %s\nName: %s\nVersion:%s\n") ,HostName, OSName, VersionInfo);
+		_tprintf(_T("Role: %s\nComponent: %s\n"), Role, Components);
+		ret = 0;
+	}
 
-	return 255;
+	/* Clean up some stuff that that was opened */
+	if(pBuf102)
+		NetApiBufferFree(pBuf102);
+	LocalFree(argv);
+	if(!bLocal)
+	{
+		WNetCancelConnection2(RemoteResource,0,TRUE);
+	}
+	return ret;
 }

Modified: trunk/reactos/subsys/system/gettype/gettype.xml
--- trunk/reactos/subsys/system/gettype/gettype.xml	2005-11-03 01:50:13 UTC (rev 18970)
+++ trunk/reactos/subsys/system/gettype/gettype.xml	2005-11-03 02:36:09 UTC (rev 18971)
@@ -2,6 +2,10 @@
 	<include base="gettype">.</include>
 	<define name="_WIN32_WINNT">0x0501</define>
 	<define name="__USE_W32API" />
+	<define name="UNICODE" />
+	<define name="_UNICODE" />
 	<library>kernel32</library>
+	<library>mpr</library>
+	<library>netapi32</library>
 	<file>gettype.c</file>
 </module>