Solved

Error 52 when trying to write to file on server

Posted on 2002-03-07
10
299 Views
Last Modified: 2010-05-18
Using VB5. When I open program (on a Win98 2nd edition system) and attempt to open a file on our server (also Win98 2nd edition), to write to it:

Open filename for Output as #1

I get "error 52 Bad file name or number"

The server is shared, but password protected. We run on a LAN, peer-to-peer environment.  If I go to network neighborhood and go to the server, I am prompted for the password. After I do so, I can then execute the above code without error.

The question is, How can I through code, bring up the prompt, without getting the error 52?
0
Comment
Question by:quicdoc
10 Comments
 
LVL 6

Expert Comment

by:anthony_glenwright
ID: 6848710
This is just a guess, but you could try WNetUseConnection ()
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6848747
Why don't you map a network drive and check reconnect at logon?
Doing so, server should not ask you for password every time (it depends on how is configured your groups-accounts).
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6848950
<<I get "error 52 Bad file name or number">>

BAD FILE NAME:
What is the value of "filename" when you attempt this open?
BAD FILE NUMBER:
Are you certain that "#1" is available? (Not already used?)
To insure this use FreeFile:

Dim nFileNumber As Integer
nFileNumber = FreeFile
Open filename for Output as #nFileNumber



0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6850347
trkcorp is right but only sometimes hardcoded filenumber does this error.
The problem is when quicdoc try toi open the file at network, resource ask it for password and since open statement doesn't provides it, vb fails.
Maybe antony's solution would works.
0
 

Author Comment

by:quicdoc
ID: 6850537
Anthony, please explain more about use of WNetUseConnection()- I know the file number is not in use and the file I am looking, I first test to see if the File exists.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6850633
maybe some customization could be required:
type NETRESOURCE
    dwScope as long
    dwType as long
    dwDisplayType as long
    dwUsage as long
    lpLocalName as string
    lpRemoteName as string
    pComment as string
    lpProvider as string
end type
 
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6850643
(continue...)
Members
dwScope
Specifies the scope of the enumeration. Currently, this member can be one of the following values: Value Meaning
RESOURCE_CONNECTED Currently connected resources (the dwUsage member is undefined).
RESOURCE_GLOBALNET Resources on the network.
RESOURCE_REMEMBERED Remembered (persistent) connections (dwUsage is undefined).


dwType
Specifies a bitmask that gives the resource type. Currently, this member can be one of the following values: Value Meaning
RESOURCETYPE_ANY All resources
RESOURCETYPE_DISK Disk resources
RESOURCETYPE_PRINT Print resources


dwDisplayType
Specifies how the network object should be displayed in a network browsing user interface. Currently, this member can be the following values: Value Meaning
RESOURCEDISPLAYTYPE_DOMAIN
 The object should be displayed as a domain.
RESOURCEDISPLAYTYPE_GENERIC
 The method used to display the object does not matter.
RESOURCEDISPLAYTYPE_SERVER
 The object should be displayed as a server.
RESOURCEDISPLAYTYPE_SHARE
 The object should be displayed as a sharepoint.


dwUsage
Specifies a bitmask that gives the resource usage. This member is defined only if dwScope is RESOURCE_GLOBALNET. Currently, this member can be one of the following values: Value Meaning
RESOURCEUSAGE_CONNECTABLE This is a connectable resource; the name pointed to by the lpRemoteName member can be passed to the WNetAddConnection function to make a network connection.
RESOURCEUSAGE_CONTAINER This is a container resource; the name pointed to by the lpRemoteName member can be passed to the WNetOpenEnum function to enumerate the resources in the container.


lpLocalName
Points to the name of a local device if the dwScope member is RESOURCE_CONNECTED or RESOURCE_REMEMBERED. This member is NULL if the connection does not use a device. Otherwise, it is undefined.
lpRemoteName
Points to a remote network name if the entry is a network resource.
If the entry is a current or persistent connection, lpRemoteName points to the network name associated with the name pointed to by the lpLocalName member.

lpComment
Points to a provider-supplied comment.
lpProvider
Points to the name of the provider owning this resource. This member can be NULL if the provider name is unknown.
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 200 total points
ID: 6850673
DWORD WNetUseConnection(
  HWND hwndOwner,              
  LPNETRESOURCE lpNetResource,  
  LPTSTR lpUserName,            
  LPTSTR lpPassword,            
  DWORD dwFlags,                
  LPTSTR lpAccessName,          
  LPDWORD lpBufferSize,        
  LPDWORD lpResult              
);
 
declare function WNetUseConnection lib "mpr.dll" alias "WNetUseConnectionA" (byval hwndOwner as long, lpNetResource as NETRESOURCE, byval lpUserName as string, lpPassword as string, byval dwFlags as long, byval lpAccessName as string,  byref lpBufferSize as long, byval lpResult as long) as long

remember: maybe some changes could be required

Parameters
hwndOwner
Specifies a window that the provider of network resources can use as an owner window for dialog boxes. Use this parameter if CONNECT_INTERACTIVE is set.
lpNetResource
Pointer to a NETRESOURCE structure that specifies details of the proposed connection. The structure contains information about the network resource, the local device, and the network resource provider.
You must specify the following members of the NETRESOURCE structure: Member Description  
dwType Specifies the type of resource to connect to. It is most efficient to use a specific resource type (such as RESOURCETYPE_DISK or RESOURCETYPE_PRINT). However, if lpLocalName is NULL or points to an empty string and CONNECT_REDIRECT is not set, dwType can be RESOURCETYPE_ANY. This method works only if the function does not automatically choose a device to redirect to the network resource.
lpLocalName Points to a null-terminated string that specifies the name of a local device to be redirected, such as "F:" or "LPT1." The string is treated in a case-insensitive manner. If the string is empty or if lpLocalName is NULL, a connection to the network occurs without redirection. If CONNECT_REDIRECT is set or the network requires a redirected local device, the function chooses a local device to redirect and returns the name of the device in lpAccessName.  
lpRemoteName Points to a null-terminated string that specifies the network resource to connect to. The string can be up to MAX_PATH characters in length. The string must follow the network provider's naming conventions.
lpProvider Points to a null-terminated string that specifies the network provider to connect to. If lpProvider is NULL or points to an empty string, the operating system attempts to determine the correct provider by parsing the string pointed to by lpRemoteName.
 You should set this member only if you know for certain which network provider you want to use. Otherwise, let the operating system determine which network provider the network name maps to.
 If this member is not NULL, the operating system attempts to make a connection only to the named network provider.



The WNetUseConnection function ignores the other members of the NETRESOURCE structure.

lpPassword
Pointer to a null-terminated string that specifies a password to be used in making the network connection.
If lpPassword is NULL, the function uses the current default password associated with the user specified by lpUserName.

If lpPassword points to an empty string, the function does not use a password.

If the connection fails because of an invalid password and CONNECT_INTERACTIVE is set, the function displays a dialog box asking the user to type the password.

lpUserName
Pointer to a null-terminated string that specifies a user name to be used in making the connection.
If lpUserName is NULL, the function uses the default user name. The user context for the process provides the default user name.

The lpUserName parameter is specified when users want to connect to a network resource for which they have been assigned a user name or account other than the default user name or account.

The user-name string represents a security context. It may be specific to a network provider.

dwFlags
Specifies any combination of the following values: Value Meaning
CONNECT_INTERACTIVE If this flag is set, the operating system may interact with the user for authentication purposes.
CONNECT_PROMPT This flag instructs the system not to use any default settings for user names or passwords without offering the user the chance to supply an alternative. This flag is ignored unless CONNECT_INTERACTIVE is set.
CONNECT_REDIRECT This flag forces the redirection of a local device when making the connection. If lpLocalName specifies a local device to redirect, this flag has no effect, because the operating system attempts to redirect the specified device anyway. Otherwise, if the operating system automatically chooses a local device, lpAccessName must point to a return buffer and dwType must not be RESOURCETYPE_ANY.
If this flag is not set, a local device is automatically chosen for redirection only if the network requires a local device to be redirected.
 
CONNECT_UPDATE_PROFILE This flag instructs the operating system to store the network resource connection.
If this bit flag is set, the operating system automatically attempts to restore the connection when the user logs on. The system remembers only successful connections that redirect local devices. It does not remember unsuccessful connections and deviceless connections. A deviceless connection occurs when lpLocalName is NULL or points to an empty string.

If this bit flag is clear, the operating system will not automatically restore the connection at logon.
 


lpAccessName
Specifies the name of a buffer that receives system requests on the connection.
If lpLocalName specifies a local device, this buffer is optional, and will have the local device name copied into it. If lpLocalName does not specify a device and the network requires a local device redirection, or if CONNECT_REDIRECT is set, this buffer is required and the name of the redirected local device is returned here. Otherwise, the name copied into the buffer is that of a remote resource. If specified, this buffer must be at least as large as the string pointed to by the lpRemoteName parameter.

lpBufferSize
Specifies the size, in characters, of the lpAccessName buffer passed in. If the call fails because the buffer is not large enough, the function uses this location to return the required buffer size.
lpResult
Pointer to a DWORD that returns additional information about the connection. This parameter can be a combination of the following values: Value Meaning
CONNECT_LOCALDRIVE If this flag is set, the connection was made using a local device redirection. If the lpAccessName parameter points to a buffer, the local device name is copied to the buffer.


Return Values
If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is one of the following error codes.

Value Meaning
ERROR_ACCESS_DENIED Access to the network resource was denied.
ERROR_ALREADY_ASSIGNED The local device specified by lpLocalName is already connected to a network resource.
ERROR_BAD_DEVICE The value specified by lpLocalName is invalid.
ERROR_BAD_NET_NAME The value specified by lpRemoteName is not acceptable to any network resource provider. The resource name is invalid, or the named resource cannot be located.
ERROR_BAD_PROVIDER The value specified by lpProvider does not match any provider.
ERROR_CANCELLED The attempt to make the connection was canceled by the user through a dialog box from one of the network resource providers or by a called resource.
ERROR_EXTENDED_ERROR A network-specific error occurred. To get a description of the error, call the WNetGetLastError function.
ERROR_INVALID_ADDRESS The caller passed in a pointer to a buffer that could not be accessed.
ERROR_INVALID_PARAMETER This error is a result of one of the following conditions:
The lpRemoteName member is NULL; lpAccessName is not NULL, but lpBufferSize is either NULL or points to zero.
The dwType member is neither RESOURCETYPE_DISK nor RESOURCETYPE_PRINT. In addition, CONNECT_REDIRECT is set in dwFlags, lpLocalName is NULL, or the connection is to a network that requires the redirecting of a local device.
 
ERROR_INVALID_PASSWORD The specified password is invalid and CONNECT_INTERACTIVE is not set.
ERROR_MORE_DATA The lpAccessName buffer is too small. If a local device is redirected, the buffer needs to be large enough to contain the local device name. Otherwise, the buffer needs to be large enough to contain either the string pointed to by lpRemoteName or the name of the connectable resource whose alias is pointed to by lpRemoteName. If this status is returned, then no connection has been made.
ERROR_NO_MORE_ITEMS The operating system cannot automatically choose a local redirection because all the valid local devices are in use.
ERROR_NO_NET_OR_BAD_PATH A network component has not started, or the specified name could not be handled.
ERROR_NO_NETWORK There is no network present.

0
 

Author Comment

by:quicdoc
ID: 6850870
Thanks Richie. Your comments led me to some source code from codehound.com which establishes a connection.

Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUsername As String, ByVal dwFlags As Long) As Long

'CONSTANTS
Private Const WN_SUCCESS = 0 ' The function was successful.
Private Const WN_NET_ERROR = 2 ' An error occurred on the network.
Private Const WN_BAD_PASSWORD = 6 ' The password was invalid.
Private Const NO_ERROR = 0
Private Const CONNECT_LOCALDRIVE = 256
Private Const CONNECT_REDIRECT = 128
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
Private Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const CONNECT_UPDATE_PROFILE = 1
Private Const CONNECT_UPDATE_RECENT = &H2

'TYPES
Private Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    lpLocalName As String
    lpRemoteName As String
    lpComment As String
    lpProvider As String
End Type

Function ConnectionAdd3(ByVal sSharePath As String,_ Optional ByRef sDriveLetter As String, Optional sPassword_ As String = "", Optional sUserName As String = "",_ Optional sComment As String) As Long
    Dim tNetR As NETRESOURCE
    Dim lRetVal As Long
   
    tNetR.dwScope = RESOURCE_GLOBALNET
    tNetR.dwType = RESOURCETYPE_DISK
    tNetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
    tNetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
    tNetR.lpLocalName = sDriveLetter
    tNetR.lpRemoteName = sSharePath
    tNetR.lpComment = sComment
   
    ConnectionAdd3 = WNetAddConnection2(tNetR, sPassword,_ sUserName, CONNECT_UPDATE_PROFILE)
End Function
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6850909
But the idea of use API was from Antony in first place, i just was helping a little...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now