Map a network drive:
.686
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
INCLUDE windows.inc
INCLUDE kernel32.inc
INCLUDE user32.inc
INCLUDE msvcrt.inc
INCLUDE masm32.inc
INCLUDE mpr.inc
INCLUDE c:\masm32\macros\macros.asm
INCLUDELIB kernel32.lib
INCLUDELIB user32.lib
INCLUDELIB msvcrt.lib
INCLUDELIB masm32.lib
INCLUDELIB mpr.lib
GetErrorMessage PROTO :DWORD,:PTR BYTE,:DWORD
.DATA?
szBuf BYTE 256 DUP(?)
dwError DWORD ?
pszRemoteName PBYTE ?
.DATA
szDriveLetter BYTE "N:", 0
szNetworkResource BYTE "\\NSLU2\Disk 1", 0
szPassword BYTE 0
dwLenRemoteName DWORD SIZEOF szBuf
.CODE
start:
call main
inkey SADD(13,10,"Press any key to exit ... ")
INVOKE ExitProcess, 0
main PROC
INVOKE WNetAddConnection, ADDR szNetworkResource, ADDR szPassword, ADDR szDriveLetter
.IF eax != NO_ERROR
mov dwError, eax
INVOKE GetErrorMessage, eax, ADDR szBuf, SIZEOF szBuf
INVOKE StdOut, ADDR szBuf
.ELSE
INVOKE StdOut, SADD("Success: ")
INVOKE WNetGetConnection, ADDR szDriveLetter, ADDR szBuf, ADDR dwLenRemoteName
INVOKE StdOut, SADD("drive ")
INVOKE StdOut, ADDR szDriveLetter
INVOKE StdOut, SADD(" is mapped to ")
INVOKE StdOut, ADDR szBuf
.ENDIF
ret
main ENDP
;--------------------------------------------------------------------------
GetErrorMessage PROC dwErrorNum:DWORD, pBuffer:PTR BYTE, dwBufferSize:DWORD
LOCAL hModule :DWORD
LOCAL dwDefLangID:DWORD
LOCAL dwFlags :DWORD
LOCAL retv :SDWORD
LOCAL pTmpBuffer :PVOID
mov hModule, NULL
mov pTmpBuffer, NULL
INVOKE GetUserDefaultLangID
mov dwDefLangID, eax
mov eax, FORMAT_MESSAGE_ALLOCATE_BUFFER OR FORMAT_MESSAGE_FROM_SYSTEM OR FORMAT_MESSAGE_IGNORE_INSERTS
mov dwFlags, eax
.IF dwErrorNum >= NERR_BASE && dwErrorNum <= MAX_NERR
INVOKE LoadLibraryEx, SADD("netmsg.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE
mov hModule, eax
.IF hModule != NULL
mov eax, dwFlags
or eax, FORMAT_MESSAGE_FROM_HMODULE
mov dwFlags, eax
.ENDIF
.ENDIF
INVOKE FormatMessage, dwFlags, hModule, dwErrorNum, dwDefLangID, ADDR pTmpBuffer, 0, NULL
.IF eax
dec dwBufferSize
INVOKE lstrcpyn, pBuffer, pTmpBuffer, dwBufferSize
INVOKE LocalFree, pTmpBuffer
mov retv, 0
.ELSE
INVOKE GetLastError
mov retv, eax
.ENDIF
.IF hModule != NULL
INVOKE FreeLibrary, hModule
.ENDIF
mov eax, retv
ret
GetErrorMessage ENDP
;--------------------------------------------------------------------------
END start
You can use WNetCancelConnection to disconnect the mapped drive.
DWORD WNetAddConnection(
LPTSTR lpRemoteName, // pointer to network device name
LPTSTR lpPassword, // pointer to password
LPTSTR lpLocalName // pointer to local device name
);
Parameters
lpRemoteName
Points to a null-terminated string that specifies the network resource to connect to.
lpPassword
Points to a null-terminated string that specifies the password to be used to make a connection. This parameter is usually the password associated with the current user.
If this parameter is NULL, the default password is used. If the string is empty, no password is used.
lpLocalName
Points to a null-terminated string that specifies the name of a local device to be redirected, such as F: or LPT1. The case of the characters in the string is not important. If the string is NULL, a connection to the network resource is made without redirecting the local device.
-----
you might want to add in the functionality of using a password too, you're just passing a null pointer - so effectively no password.. most people (if they're smart) are not going to map network drives without passwords :)
and the api you're using is effectively depreciated...
"This function is provided for compatibility with earlier versions of Microsoft Windows. For new applications, use the WNetAddConnection2
function. "
Quoteyou might want to add in the functionality of using a password too, you're just passing a null pointer - so effectively no password.. most people (if they're smart) are not going to map network drives without passwords :)
I wrote this for my own use on my home network and thought I would pass it along. My file server has no password. Anyone can make the change for a password easily enough.
Quoteand the api you're using is effectively depreciated...
I took a look at WNetAddConnection2 and it looked like a pain in the ass compared to WNetAddConnection for what I wanted to do. I don't think it's going to stop working anytime soon.
If I was writing this for business use I would have used WNetAddConnection2 and I would have used a password.
I'll leave it to you to modify it to your liking.
I guess I just shouldn't have posted it huh?
naw, its usable sure, i wasn't 'attacking' the coding, just suggesting changes...
Quote... i wasn't 'attacking' the coding ...
Well, that's the way it came across.
I added a password variable. After re-reading the docs I think it's better since I want no password to be used, versus the 'default password', whatever that is.
Greg,
Though I don't do much coding these days I enjoy following your source code. Years back it was common for asmers to post source code on a daily bases, don't seem to see much of it these days. I would often post source code back then. Just ignore the critics and keep posting your code buddy! :wink
Quote from: Greg on June 24, 2008, 10:03:13 PM
Quote... i wasn't 'attacking' the coding ...
Well, that's the way it came across.
I added a password variable. After re-reading the docs I think it's better since I want no password to be used, versus the 'default password', whatever that is.
maybe you're a bit sensitive... i was simply pointing out the password thing wasn't commented (before you edited it), so it didnt' make much sense what you were doing, and that the api is considered depreciated (simply again, pointing out that the code mightn't be usable in the future)..
i didn't once criticise on the coding, just the api usage... and even then it wasnt really a criticism, more of a 'hint'...
Quotemaybe you're a bit sensitive...
maybe ... :bg
Cycle Saddles,
Thanks.