WordBasic: Calling the Windows API Function WNetGetConnection

ID: Q130221

The information in this article applies to:

SYMPTOMS

The Windows API function WNetGetConnection generates a WordBasic error number 133 ("Wrong number or type of arguments for DLL call") when called from WordBasic.

CAUSE

This problem occurs if you use the following declare statement:

   Declare Function WNetGetConnection Lib "User" (lpszLocalName As String,
   lpszRemoteName As String, cbRemoteName As Integer) As Integer

RESOLUTION

The following code can be used to correctly call the WNetGetConnection function from WordBasic.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Declare Function WNetGetConnection Lib "User"(vLocalName$,
vReturn$,vReturnSize$) As Integer

Sub MAIN

'DEFINE VARIABLES:
vLocalName$ = "o:"          'The drive you want to check
vReturnSize$ = "z"          'Do not change
vTitle$ = "WNetGetConnection"

'DEFINE CONSTANTS:
WN_SUCCESS = 0 WN_NOT_SUPPORTED = 1 WN_OUT_OF_MEMORY = 11 WN_NET_ERROR = 2 WN_BAD_POINTER = 4 WN_BAD_VALUE = 5 WN_NOT_CONNECTED = 48 WN_MORE_DATA = 3

n = WNetGetConnection(vLocalName$, vReturn$, vReturnSize$)

'MsgBox Str$(n)
Select Case n
     Case WN_SUCCESS
          MsgBox "Local device " + UCase$(vLocalName$) + " is connected to
network resource " + UCase$(vReturn$) + ".", vTitle$, 64
     Case WN_NOT_CONNECTED
          MsgBox "Local device " + UCase$(vLocalName$) + " is not
connected to a network resource.", vTitle$, 64
     Case WN_NOT_SUPPORTED
          Beep
          MsgBox "The WNetGetConnection function is not supported on "
+ "this network.", vTitle$, 48
     Case WN_OUT_OF_MEMORY
          Beep
          MsgBox "There is not enough memory to determine the network "
+ "connection.", vTitle$, 48
     Case WN_NET_ERROR
          Beep
          MsgBox "The network connection could not be determined "
+ "because a network error occurred.", vTitle$, 48
     Case WN_BAD_POINTER
          Beep
          MsgBox "WNetGetConnection was passed an argument that was "
+ "an invalid pointer.", vTitle$, 48
     Case WN_BAD_VALUE
          Beep
          MsgBox "This is not a valid local device name. (" + vLocalName$
+ ").", vTitle$, 48
     Case WN_MORE_DATA
          Beep
          MsgBox "The WNetGetConnection returned a string longer than "
+ "the available return buffer size.", vTitle$, 48
     Case Else
          MsgBox "The network connection could not be determined " +
"because an unknown error occurred.", vTitle$, 48 End Select
End Sub

MORE INFORMATION

An API call that takes an integer type should be declared with simply an integer parameter in Word. But if the parameter type is a pointer to an integer, you need to declare it as a string.

The last parameter to WNetGetConnection (cbRemoteName) should be a FAR pointer to an integer rather than an integer. In WordBasic, a string should be used instead, since a string is passed by WordBasic as a FAR pointer (in this case, to a string of characters). Declaring cbRemoteName as an Integer makes the parameter too small (2 bytes versus 4 bytes), which is why the call fails.

REFERENCES

Microsoft Windows SDK "Programmer's Reference, Volume 2: Functions," pages 992-993

KBCategory: kbmacro KBSubcategory: Additional reference words: 6.0 6.0a 6.0c winword 2.0 2.0a 2.0a-CD word6 winapi 2.0b 2.0c

Keywords          : kbmacro
Version           : 1.0 1.10 1.10a 2.0 2.0a 2.0a
Platform          : WINDOWS

Last Reviewed: August 5, 1997