detecting windows SMB feature + 1 more

Windows specific questions.
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

detecting windows SMB feature + 1 more

Post by hooshnik »

I am trying to detect windows 10 AND smb 3.0 in freebasic. I understand that windows 8 also uses smb 3.0 HOWEVER I have seen reports that the file locking problem from smb 2.1 still affects windows 8.1.

Basically in order to overcome this silly smb 2.1 and lower bug I have to wait 10 seconds before reading the metadata (date) of a file. If I don't wait long enough the date is wrong (old date before writing to file I guess).

I have also read on this forum that detecting windows version is a mess so I am trying to avoid it. So somehow I have to detect windows 10 and smb 3.0 AND 1 other feature that only windows 10 has. What would be the best one? I don't want users to be bothered with this 10 second delay on windows 10.
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

Re: detecting windows SMB feature + 1 more

Post by hooshnik »

I've just decided this is overkill for my open source program. I'll just look at an environmental variable. Call it something like smblockdelay. I'll put a warning message in block letters on the console.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: detecting windows SMB feature + 1 more

Post by MrSwiss »

hi,

getting the Windows Version String isn't any magic ... :

Code: Select all

' GetWindowsVersion.bas -- 2017-06-02, by MrSwiss

Dim As Long     fn = FreeFile
Dim As String   cmd = "ver", ret

If Open Pipe(cmd For Input As #fn) Then
    Var e = Err : Print e   ' just in case ...
Else
    Line Input #fn, ret ' if first line is 'empty', try second (WIN 10 is empty!)
    If Len(ret) = 0 Then Line Input #fn, ret ' get windows version string
    If Len(ret) > 0 Then
        Print ret : Print   ' to screen
        Print "'" + cmd + "' successful!"
    Else
        Print "Couldn't get Windows Version String!"
    EndIf
End If

Close fn    ' always close open devices/files etc.
Locate CsrLin + 1 : Print "Any key press --> EXIT! ";

Sleep : End
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

Re: detecting windows SMB feature + 1 more

Post by hooshnik »

Thanks MrSwiss that was educational! It even returns the pipe (/current line?) name as "ver" which I wasn't expecting. Except I just coded the environmental variable into my program. If I would have used your method I should ultimately have to also detect that the program is being run from a windows network drive.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: detecting windows SMB feature + 1 more

Post by MrSwiss »

hooshnik wrote:It even returns the pipe (/current line?) name as "ver" which I wasn't expecting.
Nope, this is "hard coded", the value of string 'cmd' (see declarations).
hooshnik wrote:If I would have used your method I should ultimately have to also detect that the program is being run from a windows network drive.
Well, you could use ExePath statement, to determine DRIVE:/<Path> ...
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

Re: detecting windows SMB feature + 1 more

Post by hooshnik »

Woops I missed that it was hard-coded! I thought you were running the windows "cmd" command lol. It's a bog standard named pipe. I always wondered how to do it in freebasic now I know. :)

Well looking at drive letters is hackish. On my w7 computer drive e is local but on another computer it could be remote. Is that what you mean?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: detecting windows SMB feature + 1 more

Post by MrSwiss »

hooshnik wrote:Well looking at drive letters is hackish. On my w7 computer drive e is local but on another computer it could be remote. Is that what you mean?
Yes, it's one of those things in WIN, that doesn't make live too easy.
I'd check on MSDN, if there is a method to establish, if a drive is local or not.
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: detecting windows SMB feature + 1 more

Post by Josep Roca »

> I'd check on MSDN, if there is a method to establish, if a drive is local or not.

GetDriveType
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

Re: detecting windows SMB feature + 1 more

Post by hooshnik »

In Windows 8 and Windows Server 2012, this function is supported by the following technologies.

Server Message Block (SMB) 3.0 protocol: No
Crap. Now I can't use it I think.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: detecting windows SMB feature + 1 more

Post by MrSwiss »

hooshnik wrote:Crap. Now I can't use it I think.
Nope, nothing to do with SMB, you can very well use it. In order to do so:

Code: Select all

#Include "windows.bi"
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

Re: detecting windows SMB feature + 1 more

Post by hooshnik »

MrSwiss wrote:Nope, nothing to do with SMB
Are you sure? I think Microsoft is implying that if this is run on a network drive which is using SMB 3.0 with windows 8 it will surely report something like "unknown" making it useless to me.

EDIT: I suppose I could trigger on "unknown" + windows 8. Not so fun however.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: detecting windows SMB feature + 1 more

Post by MrSwiss »

hooshnik wrote:Are you sure? I think Microsoft is implying that if this is run on a network drive which is using SMB 3.0 with windows 8 it will surely report something like "unknown" making it useless to me.
Microsoft wrote:SMB does not support volume management functions.
This also is not of any interest, within the context.

Since GetDriveType is a "kernel32" call (local DLL), it only makes Drive-Discovery, probably,
without even attempting any communication (SMB).
Even in case it does, it'll most likely, use another mechanism (named pipes or similar).

I don't like it, when people say "no", before they've even tried "something new" out.
Pierre Bellisle
Posts: 56
Joined: Dec 11, 2016 17:22

Re: detecting windows SMB feature + 1 more

Post by Pierre Bellisle »

The SMB version used will be determined by the lowest Windows OS Client versus Server version.
One way to find it could be by using NetWkstaGetInfo() to retreive those both Client and Server version and then deduct the SMB version that will be used.

SMB version by Jose Barreto

Yours to try :-)

Pierre

Code: Select all

#Define JumpCompiler "<D:\Free\64\fbc.exe>"
#Define JumpCompilerCmd "<-s gui >"

#Lang "fb"
#Define Unicode
#Include Once "windows.bi"
#Include Once "win\winbase.bi"
#Include Once "win\lmcons.bi"

#define NERR_SUCCESS 0

Type WKSTA_INFO_100
  wki100_platform_id  As DWORD
  wki100_computername As WString Pointer
  wki100_langroup     As WString Pointer
  wki100_ver_major    As DWORD
  wki100_ver_minor    As DWORD
End Type

Declare Function NetWkstaGetInfo Lib "NETAPI32.DLL" Alias "NetWkstaGetInfo" _
(ByVal servername As WString Pointer, ByVal level As DWORD, ByRef bufptr As LPBYTE) As NET_API_STATUS

Declare Function NetApiBufferFree Lib "NetApi32.dll" Alias "NetApiBufferFree" _
(ByVal Buffer As LPVOID ) As NET_API_STATUS
'_____________________________________________________________________________

 Dim pInfo100 As WKSTA_INFO_100 Pointer
 Dim ComputerNameLen    AS DWORD
 Dim wsComputerName     AS wSTRING * MAX_COMPUTERNAME_LENGTH + 1 '15
 Dim wsServerName       AS wSTRING * MAX_COMPUTERNAME_LENGTH + 1 '15
 Dim ComputerWindowsVer As Double
 Dim ServerWindowsVer   As Double
 Dim WindowLowerVersion As Double

 'Get current Windows version
 If NetWkstaGetInfo(ByVal NULL, 100, Cast(LPBYTE, pInfo100)) = NERR_Success Then 'Windows 2000 and newer
   'MessageBox(HWND_DESKTOP, _
   '           !"platform_id:  \t" & pInfo100->wki100_platform_id    & !"\n" & _
   '           !"computername: \t" & pInfo100->*wki100_computername  & !"\n" & _
   '           !"langroup:     \t" & pInfo100->*wki100_langroup      & !"\n" & _
   '           !"ver_major:    \t" & pInfo100->wki100_ver_major      & !"\n" & _
   '           !"ver_minor:    \t" & pInfo100->wki100_ver_minor      & !"\n",  _
   '           !"NetWkstaGetInfo Computer", MB_OK Or MB_TOPMOST)
   NetApiBufferFree(pInfo100)
   ComputerWindowsVer = pInfo100->wki100_ver_major + pInfo100->wki100_ver_minor / 10

   'Get current computer name
   ComputerNameLen = MAX_COMPUTERNAME_LENGTH 'Size in characters
   GetComputerName(@wsComputerName, Cast(LPDWORD, @ComputerNameLen))

   'Set ServerName
   wsServerName = "" '<<<<<< Set a valid server name here
   IF LEN(wsServerName) = 0 THEN
     MessageBox(HWND_DESKTOP, "Please set ""wsServerName"" to a valid server name!", "NetWkstaGetInfo", MB_OK Or MB_TOPMOST)
     wsServerName = "MyServerName"
   END IF
   'wsServerName = wsComputerName 'Use this line for test witj local computer name if no server is available

   'Get Windows Server version
   If NetWkstaGetInfo(@wsServerName, 100, Cast(LPBYTE, pInfo100)) = NERR_Success Then 'Windows 2000 and newer
     'MessageBox(HWND_DESKTOP, _
     '           !"platform_id:  \t" & pInfo100->wki100_platform_id    & !"\n" & _
     '           !"computername: \t" & pInfo100->*wki100_computername  & !"\n" & _
     '           !"langroup:     \t" & pInfo100->*wki100_langroup      & !"\n" & _
     '           !"ver_major:    \t" & pInfo100->wki100_ver_major      & !"\n" & _
     '           !"ver_minor:    \t" & pInfo100->wki100_ver_minor      & !"\n",  _
     '           !"NetWkstaGetInfo Server" _
     '           , MB_OK Or MB_TOPMOST)
     NetApiBufferFree(pInfo100)
     ServerWindowsVer = pInfo100->wki100_ver_major + pInfo100->wki100_ver_minor / 10

     'Get negociation resulting from OS comparaison
     IF ServerWindowsVer > ComputerWindowsVer THEN
       WindowLowerVersion = ComputerWindowsVer
     ELSE
       WindowLowerVersion = ServerWindowsVer
     End if

     'Choose SMB version according to oldest Windows capabitlities
     Dim SmbVer As wString * 10
     SELECT CASE WindowLowerVersion
       Case 10   : SmbVer = "3.1.1" 'Windows 10
       Case 6.3  : SmbVer = "3.0.2" 'Windows 8.1
       Case 6.2  : SmbVer = "3.0.0" 'Windows 8
       Case 6.1  : SmbVer = "2.1.0" 'Windows 7
       Case 6.0  : SmbVer = "2.1.0" 'Windows Vista
       Case Else : SmbVer = "1.0.0" 'Windows pre-Vista
     End Select
     MessageBox(HWND_DESKTOP, _
                !"WinServer: \t" & wsServerName   & " ver. " & ComputerWindowsVer & !"\n" &  _
                !"Computer:  \t" & wsComputerName & " ver. " & ServerWindowsVer   & !"\n\n" & _
                !"SMB negociated: \t" & SmbVer    & !"\n", !"SmbVer", MB_OK Or MB_TOPMOST)
   else
     MessageBox(HWND_DESKTOP, !"Server:  \t" & wsServerName & !" not found! \n", _
                !"NetWkstaGetInfo Server", MB_OK Or MB_TOPMOST)
   End If
 Else
   MessageBox(HWND_DESKTOP, "NetWkstaGetInfo failed!", "NetWkstaGetInfo", MB_OK Or MB_TOPMOST)
 End If

 'To follow Josep suggestion
 Dim zDrive  AS ZSTRING * 4
 Dim sLog    AS STRING
 Dim lresult AS LONG
 Dim Drv     AS LONG

 sLog = ""
 FOR Drv = 65 TO 90 'A ro Z
   zDrive  =  CHR$(Drv) & ":\"
   lResult = GetDriveType(zDrive)
   SELECT CASE lResult
     CASE DRIVE_CDROM
       sLog &= zDrive & !" is a cd-rom\n"
     CASE DRIVE_FIXED
       sLog &= zDrive & !" is a fixed disk\n"
     CASE DRIVE_RAMDISK
       sLog &= zDrive & !" is a ram disk\n"
     CASE DRIVE_REMOTE
       sLog &= zDrive & !" iis a remote drive\n"
     CASE DRIVE_REMOVABLE
       sLog &= zDrive & !" is a removable drive\n"
     CASE 0
       sLog &= zDrive & !" is an unknown drive type\n"
   END SELECT
 NEXT
 MessageBox(HWND_DESKTOP, sLog, "Remote disk", MB_OK Or MB_TOPMOST)
'_____________________________________________________________________________
'
hooshnik
Posts: 28
Joined: Sep 21, 2016 19:10

Re: detecting windows SMB feature + 1 more

Post by hooshnik »

MrSwiss wrote: I don't like it, when people say "no", before they've even tried "something new" out.
I am sorry Pierre Bellisle and MrSwiss. I thank you all of the suggestions and help but I feel I have to discontinue for now. This thing blew up into much more work than I expected. I wish I could make it perfect but this world isn't.
Pierre Bellisle
Posts: 56
Joined: Dec 11, 2016 17:22

Re: detecting windows SMB feature + 1 more

Post by Pierre Bellisle »

Hey,

No need to be sorry. ;-)
Easy does it...

Pierre
Post Reply