Sample Windows API network calls

forum.sourcecode (2000-2005) & forum.td.sourcecode (2005-2010)
James McCall

Sample Windows API network calls

Post by James McCall » 03 Nov 2007, 03:18

 Posted by:  James McCall 

Sample attached, saved as text in CTD2000, has these grown functions, by way
of interfaces to Windows network API calls (answering the questions we
really need answered) ...

- MyPathSkipRoot( ) (interface to PathSkipRootA( ) in shlwapi.dll)... strip
the server or network share part from a path
- MyDirectoryExists( ) ... if the path is on an not-yet-connected server it
has the option to pop up the login dialog and if that option isn't taken
doesn't take 30-90 seconds to figure it out
- MyConnectToNetworkShare( ) (interface to WNetUseConnectionA( ) in
mpr.dll )
- MyGetIPFromHostName( )
- MyPingHost( )
- MyIsConnectedToNetworkShare( ) ... attempt to figure out if we are
connected to a server, useful to call this before trying to access something
on that server
- MyUNCPathToUNCNetworkShare( ) ... strip a path to just the network share
part (eg. \\dbsrv1\data\vss stripped to \\dbsrv1\data)
- MyNetworkPathToServerName( )
- MyMappedDriveToServerName( ) ... find out which server is behind v:\data
- MyNetworkPathToServerIP( )

I have posted it in response to a question posted in the
forum.td.development newsgroup.

NOTE: if you examine the code closely you may find some strange
interdependencies, this is an attempt to get a good balance between
efficiency, maintainability and usefulness. If you make use of this code
and improve it in the process please have the decency to post it back in
this forum.

James McCall

You do not have the required permissions to view the files attached to this post.

Mirko
Italy
Posts: 1299
Joined: 04 Apr 2017, 08:56
Location: Geneva

Re: Sample Windows API network calls

Post by Mirko » 05 Jun 2008, 09:10

 Posted by:  Mirko BONANNO 

Hi James,

very nice set of functions !!!

Unfortunately the sample applications does not work properly... maybe I'm
doing something wrong ?

It only works with local drives !

Please find (in the next message) a picture showing the same network
directory in your application and in windows explorer.

Mirko

PS GTD42 & WXP (of course I changed strci20 and vti20 to strci42 and vti42)

Mirko
Italy
Posts: 1299
Joined: 04 Apr 2017, 08:56
Location: Geneva

Re: Sample Windows API network calls

Post by Mirko » 05 Jun 2008, 09:13

 Posted by:  Mirko BONANNO 

Here is the picture showing how the network directory is not recognized !

You do not have the required permissions to view the files attached to this post.

James McCall

Re: Sample Windows API network calls

Post by James McCall » 07 Jun 2008, 07:23

 Posted by:  James McCall 

MyDirectoryExists( ) in clsNetworkFunctions will try to PING \\bip400\. If
it fails then the function does not continue.

Normally I'd guess that PING response is turned off on \\bip400\ . However
in this case I see that MyGetIPFromHostName( ) failes so MyPingHost( ) won't
even get as far as sending the PING. I suggest you step into
MyGetIPFromHostName( ) and see where it fails.

James

Mirko
Italy
Posts: 1299
Joined: 04 Apr 2017, 08:56
Location: Geneva

Re: Sample Windows API network calls

Post by Mirko » 09 Jun 2008, 11:20

 Posted by:  Mirko BONANNO 

Hi James,

it looks like gethostbyname( "BIP400" ) return 0

:-(

James McCall

Re: Sample Windows API network calls

Post by James McCall » 09 Jun 2008, 18:36

 Posted by:  James McCall 

Is this an IP network? The function MyDirectoryExists( ) assumes that it is
an IP network and checks for the existence of the server by calling
MyPingHost( ). If you can't make that assumption then edit
MyDirectoryExists( ) to remove the calls to MyPingHost.

Find this ...
If ( NOT MyPingHost( sHostName ) )
Set nRcvHostExists_P = NUM_FALSE
Set nRet = NUM_FALSE
Break
Set nRcvHostExists_P = NUM_TRUE
... and change it to this ...
! If ( NOT MyPingHost( sHostName ) )
Set nRcvHostExists_P = NUM_FALSE
Set nRet = NUM_FALSE
Break
! Set nRcvHostExists_P = NUM_TRUE

And find this ...
If ( NOT MyPingHost( lsDirToCheck_P ) )
Set nRcvHostExists_P = NUM_FALSE
Set nRet = NUM_FALSE
Break
Set nRcvHostExists_P = NUM_TRUE
... and change it to this ...
! If ( NOT MyPingHost( lsDirToCheck_P ) )
Set nRcvHostExists_P = NUM_FALSE
Set nRet = NUM_FALSE
Break
! Set nRcvHostExists_P = NUM_TRUE

Mike

Re: Sample Windows API network calls

Post by Mike » 10 Jun 2008, 12:13

 Posted by:  Mike 

I use my own method which is very similar.

!!CB!! 81
Library name: mpr.dll
ThreadSafe: No
Function: WNetCancelConnection2A
Description: Drop a network connection
Export Ordinal: 0
Returns
Number: DWORD
Parameters
String: LPSTR
Number: DWORD
Boolean: BOOL
Function: WNetAddConnection2A
Description: Make a network connection
Export Ordinal: 0
Returns
Number: DWORD
Parameters
structPointer
Receive Number: DWORD
Receive Number: DWORD
Receive Number: DWORD
Receive Number: DWORD
Receive String: LPSTR
Receive String: LPSTR
Receive String: LPSTR
Receive String: LPSTR
String: LPSTR
String: LPSTR
Number: DWORD
Function: WNetGetLastErrorA
Description:
Export Ordinal: 0
Returns
Number: DWORD
Parameters
Number: LPVOID
String: LPSTR
Number: DWORD
String: LPSTR
Number: DWORD
Function: WNetGetConnectionA
Description: Get connection of a drive letter
Export Ordinal: 0
Returns
Number: DWORD
Parameters
String: LPCSTR
Receive String: LPSTR
Receive Number: LPDWORD
Function: WNetGetUserA
Description:
Export Ordinal: 0
Returns
Number: DWORD
Parameters
String: LPSTR
Receive String: LPSTR
Receive Number: LPDWORD
! Function: WNetGetUserDeviceA
Description: Get the user from a device or resource
Export Ordinal: 61
Returns
Number: DWORD
Parameters
String: LPSTR
Receive String: LPSTR
Receive Number: LPDWORD
Function: WNetUseConnectionA
Description: WNetUSeConnection BE AWARE CHECK MSDN LIBRARY is different for win9x/ME and win NT/2K/XP
Export Ordinal: 0
Returns
Number: DWORD
Parameters
Window Handle: HWND
structPointer
Number: DWORD
Number: DWORD
Number: DWORD
Number: DWORD
String: LPSTR
String: LPSTR
String: LPSTR
String: LPSTR
String: LPSTR
String: LPSTR
Number: DWORD
Receive String: LPSTR
Receive Number: LPDWORD
Receive Number: LPDWORD
Function: WNetAddConnectionA
Description:
Export Ordinal: 0
Returns
Number: DWORD
Parameters
String: LPCSTR
String: LPCSTR
String: LPCSTR

!!CB!! 134
Function: WNetUseConnection
Description: OS independant (Uses WinNT alike) WNetUseConnection
Returns
Number:
Parameters
String: lpLocalName
String: lpRemoteName
String: lpUserID
String: lpPassword
Receive String: sDriveLetter
Static Variables
Local variables
Number: dwScope
Number: dwDisplayType
Number: dwUseage
String: lpComment
Number: OSVersionInfoSize
Number: dwMajorVersion
Number: dwMinorVersion
Number: dwBuildNumber
Number: dwPlatformID
String: szCSDVersion
Number: wServicePackMajor
Number: wServicePackMinor
Number: wSuiteMask
Number: wProductType
Number: wReserved
String: lpAccessName
Number: lpBufferSize
Number: lpResult
Number: nTemp
Actions
Set sDriveLetter=''
If not GetVersionExA(156,dwMajorVersion,dwMinorVersion,dwBuildNumber,dwPlatformID,szCSDVersion,wServicePackMajor,wServicePackMinor,wSuiteMask,wProductType,wReserved)
Return GetLastError()
If dwPlatformID=0
Call SalMessageBox('This program can not run under win32S','Melding',0)
Call SalQuit()
Call SalStrSetBufferLength(lpAccessName,128)
Set lpBufferSize=127
If dwPlatformID=1
Set nTemp=WNetUseConnectionA(hWndNULL,2,1,3,1,lpLocalName,lpRemoteName,STRING_Null,STRING_Null,lpUserID,lpPassword,0x80,lpAccessName,lpBufferSize,lpResult)
If nTemp!=0
Return nTemp
Set sDriveLetter=lpAccessName
Return nTemp
If dwPlatformID=2
Set nTemp=WNetUseConnectionA(hWndNULL,2,1,3,1,lpLocalName,lpRemoteName,STRING_Null,STRING_Null,lpPassword,lpUserID,0x80,lpAccessName,lpBufferSize,lpResult)
If nTemp!=0
Return nTemp
Set sDriveLetter=lpAccessName
Return nTemp
Return -1

!!CB!! 134
Function: DriveMap
Description: Check if any drive is mapped under network,unc,user if so return drive letter else map any drive
Returns
Number:
Parameters
String: sMapname
String: sUser
String: sPassword
Receive String: sDrive
Receive Boolean: bNewMap
Static Variables
Local variables
String: sDrives[*]
String: sMap
Number: n
Number: nBound
String: sMappedUser
Number: nTemp
Actions
Call VisDosEnumDrives(sDrives)
Set n=1
! First check all network drives if it already is mapped if so return driveletter
Call SalArrayGetUpperBound(sDrives,1,nBound)
While n<=nBound
If VisDosGetDriveType(sDrives[n])=DRIVE_Network
Set nTemp=1023
Call SalStrSetBufferLength(sMap,1024)
If WNetGetConnectionA(sDrives[n],sMap,nTemp)=0
If SalStrUpperX(sMap)=SalStrUpperX(sMapname)
Set nTemp=80
Call SalStrSetBufferLength(sMappedUser,81)
If WNetGetUserA(sDrives[n],sMappedUser,nTemp)=0
! It is also possible that mappeduser comes with a domain or workgroup.
! So find the last \ and take it from there
Set nTemp=VisStrScanReverse(sMappedUser,SalStrLength(sMappedUser)-1,'\\')
If nTemp!=-1
Set sMappedUser=SalStrMidX(sMappedUser,nTemp+1,80)
If sUser=sMappedUser
Set sDrive=sDrives[n]
Set bNewMap=FALSE
Return 0
Set n=n+1
! apparently no drive is mapped here so try to map it
Set nTemp=WNetUseConnection(sDrive,sMapname,sUser,sPassword,sDrive)
Set bNewMap=TRUE
Return nTemp

And in case you need it:
!!CB!! 134
Function: fWindowsError
Description: Give the windows error back
Returns
String:
Parameters
Number: nErr
Static Variables
Local variables
String: sErr
Actions
Call SalStrSetBufferLength(sErr,1024)
If nErr<0
Set nErr=-nErr
Call FormatMessageA(0x12FF,0,nErr,0,sErr,1024,0)
Return sErr

See also: http://msdn.microsoft.com/en-us/library/aa385482(VS.85).aspx

It works flawless with connections over here.

FRBhote
India
Posts: 2198
Joined: 09 Mar 2017, 05:32
Location: Hyderabad, India

Re: Sample Windows API network calls

Post by FRBhote » 11 Jun 2008, 07:39

 Posted by:  F R Bhote 

Can we have it as an APT please?

Mirko
Italy
Posts: 1299
Joined: 04 Apr 2017, 08:56
Location: Geneva

Re: Sample Windows API network calls

Post by Mirko » 11 Jun 2008, 17:59

 Posted by:  Mirko BONANNO 

Good suggestion !!

Mike

Re: Sample Windows API network calls

Post by Mike » 06 Oct 2008, 15:26

 Posted by:  Mike 

You do not have the required permissions to view the files attached to this post.

Mike

Re: Sample Windows API network calls

Post by Mike » 06 Oct 2008, 15:26

 Posted by:  Mike 

Just a quick extract from some of my code then.

Return to “td.sourcecode”

Who is online

Users browsing this forum: [Ccbot] and 0 guests