I have inherited an application that requires access to a network share, which mean the PC has to have a mapped drive before access can be gained (user and password required). Is it possible to gain access programmatically?
What I need to be able to do is check for existance of files, change their names and open them in notepad

Dani AI

Generated

A few practical routes that avoid forcing users to create a persistent mapped drive (ties to 's mapping idea and 's server suggestion).

Impersonation inside your process is usually the cleanest: obtain a logon token for the needed account, impersonate that token for the thread, do your UNC file checks/renames/opens, then revert and close the token. See the LogonUser and ImpersonateLoggedOnUser documentation for the required sequence and logon types (use the "new credentials" option when you need outbound network access). LogonUser docs. (learn.microsoft.com)

If you only need a session rather than in-process impersonation, a deviceless network session (no drive letter) using the system command is a simple alternative — the net use \\server\share /user:DOMAIN\user style connection creates a usable UNC session without assigning a drive letter. net use. (learn.microsoft.com)

To open Notepad under specific credentials so it can access the UNC path directly, spawn the process with alternate credentials (CreateProcessWithLogonW). CreateProcessWithLogonW. (learn.microsoft.com)

Important cautions: Windows allows only one set of credentials per remote server per session — you will hit the "multiple connections" error if a different credential is already in use; disconnect existing sessions or use an IP/DNS alias to avoid that. Also do not store plaintext passwords in config files; use the Windows Credential Manager APIs (CredRead/CredWrite) or an OS-level store. Troubleshoot multiple-credential error. (learn.microsoft.com)

Practical checklist: clear current connections (net use / net use * /delete) before testing, try impersonation first for a "virtual" connection that leaves no drive letter, and fall back to a small server/proxy only if centralizing credentials is preferred.

Recommended Answers

All 8 Replies

Quite Possibly by using VBScript, Embeded into a VB6 application, you can simply map the drive.... this page gives you a pretty good idea as to use VBScript to map the drive:

Let me know what you come up with...

Thanks for the suggestion but I have already done simular using NetShareAdd to map a drive. What I am really looking for is something like a 'virtual' connection that does not create a physical mapping but enables me to see and change file names on a server within the application. At the moment the user has to map a drive using user/password before the application works and I want the application to have a configuration option to enable changing connection, this will avoid logging in every morning. At present the application is hard coded to get details from the server using UNC (\\server\path)

Any ideas would help

well, the only thing I can think that would be helpful would be to create a server app that sits on the computer.... then, when you connect, have the server send the client app any information that you would like, and process it accordingly....

Yes I had thought of that but it seemed a bit elaborate, I was hoping for an easier way. It looks like I will just have to continue mapping a drive, possibly programmatically.

Why not code a server? It's not too difficult to do, I have even posted in a previous post a skeleton server that you can use, and it will simply creation of sockets and what not a whole lot. All you have to do is build the transactions between the server and client....

i've never done this under VB but did write a routine that could map drives under delphi.

the following code is how it works under delphi, you could see if can change to work with VB.

function ConnectDrive(_drvLetter: String; _netPath: String; _password: String; _username: String; _showError: Boolean; _reconnect: Boolean; _interactive: Boolean): DWORD;
var
 nRes            :TNetResource;
 dwFlags         :DWORD;
 errCode         :DWORD;
begin
 FillChar(NRes,SizeOf(NRes),#0);
 nRes.dwType:=RESOURCETYPE_DISK;
 nRes.lpLocalName:=PChar(_drvLetter);
 nRes.lpRemoteName:=PChar(_netPath);
 nRes.dwScope:=RESOURCE_GLOBALNET;
 dwFlags:=0;
 If _reconnect then dwFlags:=CONNECT_UPDATE_PROFILE;
 If _reconnect and _interactive then dwFlags:=CONNECT_UPDATE_PROFILE and CONNECT_INTERACTIVE;
 Result:=WNetAddConnection3(form1.Handle,nRes,PChar(_password),PChar(_username),dwFlags);
 end;

function DisConnectDrive(_drvLetter: String; _showError: Boolean; _force: Boolean; _save: Boolean): DWORD;
var
 nRes           :TNetResource;
 dwFlags        :DWORD;
 errCode        :DWORD;
begin
if _save then
 dwFlags:=CONNECT_UPDATE_PROFILE
else
 dwFlags:=0;
 errCode:=WNetCancelConnection2(PChar(_drvLetter),dwFlags,_force);
if (errCode<>NO_ERROR) and (_showError) then
 begin
  Application.MessageBox(PChar(SysErrorMessage(GetLastError)),
  'Error Disconnecting Drive',
  MB_OK);
 end;
 Result:=errCode; {NO_ERROR}
end;

You could have a couple of text inputs on the form, with a command button. When clicking on this after filling the info in, you could connect the drive. When the program closes you could get it to call the disconnect feature to ensure properly disconnected.

If need further help, I'll try to get it converted to a VB app, but due to workload can be sometime before I get it done.

Good Luck

Mike

appreciate all the help we can get on this mrmike, but let's try to keep delphi in the delphi forum. Thanx.

Sorry comatose,

thought if shown the delphi code, may have been able to convert it to vb code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.