• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

How to tell if DLL on Desktop or Server?

I have a DLL that is going to be deployed both on Servers and Desktops.  Instead of creating two seperate DLLs for desktop and server we want to have one dll, but the functionality is different based on if the dll is being ran on a desktop vs a server.  So how can we do a programmatic check to see if the DLL is on a desktop or server?

Thanks,

Leo
0
Leo Eikelman
Asked:
Leo Eikelman
  • 5
  • 5
1 Solution
 
vinnyd79Commented:
There is a registry value you can check. Check out this link:

http://support.microsoft.com/kb/q152078/
0
 
Leo EikelmanAuthor Commented:
So this checks if a Windows NT computer is a workstation, server, etc...

would I be able to check this value on Windows 2000 server and Windows XP desktop?

Could you provide a code example?

Thanks,

Leo
0
 
vinnyd79Commented:
Try adding this to a module:


Option Explicit
Public Const REG_SZ As Long = 1
Public Const REG_DWORD As Long = 4
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const ERROR_NONE = 0
Public Const ERROR_BADDB = 1
Public Const ERROR_BADKEY = 2
Public Const ERROR_CANTOPEN = 3
Public Const ERROR_CANTREAD = 4
Public Const ERROR_CANTWRITE = 5
Public Const ERROR_OUTOFMEMORY = 6
Public Const ERROR_ARENA_TRASHED = 7
Public Const ERROR_ACCESS_DENIED = 8
Public Const ERROR_INVALID_PARAMETERS = 87
Public Const ERROR_NO_MORE_ITEMS = 259
Public Const KEY_QUERY_VALUE = &H1

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias _
   "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
   ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _
   As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _
   As Long, phkResult As Long, lpdwDisposition As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
   "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
   ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
   As String, lpcbData As Long) As Long
   Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, lpData As _
   Long, lpcbData As Long) As Long
   Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
   "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
   String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
   As Long, lpcbData As Long) As Long

Public ProdType As String

Public Function QueryValue(sKeyName As String, sValueName As String)
       Dim lRetVal As Long         'result of the API functions
       Dim hKey As Long         'handle of opened key
       Dim vValue As Variant      'setting of queried value

       lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKeyName, 0, _
   KEY_QUERY_VALUE, hKey)
       lRetVal = QueryValueEx(hKey, sValueName, vValue)
       ProdType = vValue
       RegCloseKey (hKey)
   End Function

Public Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _
   String, vValue As Variant) As Long
Dim cch As Long, lrc As Long, lType As Long, lValue As Long
Dim sValue As String

On Error GoTo QueryValueExError

       ' Determine the size and type of data to be read
       lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
       If lrc <> ERROR_NONE Then Error 5

       Select Case lType
           ' For strings
           Case REG_SZ:
               sValue = String(cch, 0)

   lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
   sValue, cch)
               If lrc = ERROR_NONE Then
                   vValue = Left$(sValue, cch - 1)
               Else
                   vValue = Empty
               End If
           ' For DWORDS
           Case REG_DWORD:
   lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _
   lValue, cch)
               If lrc = ERROR_NONE Then vValue = lValue
           Case Else
               'all other data types not supported
               lrc = -1
       End Select

QueryValueExExit:
       QueryValueEx = lrc
       Exit Function

QueryValueExError:
       Resume QueryValueExExit
   End Function

Public Function IsServer() As Boolean
QueryValue "SYSTEM\CurrentControlSet\Control\ProductOptions", "ProductType"
Select Case ProdType
    Case "WinNT"
        IsServer = False
    Case "LanmanNT", "ServerNT"
        IsServer = True
End Select
End Function




Then try calling it like this:

Private Sub Command1_Click()
MsgBox IsServer
End Sub

If it is a server it will return true otherwise it should return false.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Leo EikelmanAuthor Commented:
I will give that a shot and get back to you

Leo
0
 
Leo EikelmanAuthor Commented:
Is there any way of doing this without using the registry?

I know of the function GetVersionExA in kernel32 but that only returns Major Version, Minor Version, Build Number, etc... is there one that returns if it's a server or desktop?

Just some simple api


Leo
0
 
vinnyd79Commented:
The only other way I can think of at the moment is to check the running services to see if the server service is running.

http://vbnet.mvps.org/index.html?code/network/enumservices.htm

0
 
Leo EikelmanAuthor Commented:
that would not work because all of the computers are on a network so the server service is running on all of them.

I thought there would be some simple API from windows to figure this out.

any more suggestions?

Leo
0
 
vinnyd79Commented:
0
 
vinnyd79Commented:
Actually it's the osversioninfoex structure you need to use with GetVersionExA API.
0
 
Leo EikelmanAuthor Commented:
Thank you vinny, I got it working using the osversioninfoex structure and the GetVersionExA API.

: )
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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