Page 1 of 4

mapping network drive inside application -is it possible?

Posted: Tue May 09, 2017 1:32 pm
by mol
Does anybody know if exists harbour function to map network drive inside application?
I need to hide net use... command

Re: mapping network drive inside application -is it possible?

Posted: Tue May 09, 2017 2:01 pm
by andyglezl
Maybe this...

ShellExecute( 0, "Open", "Net Use", "Parameters. . . . .", NULL, 0 )

or

File.vbs
----------------------------------------------------
set objshell = createobject("wscript.shell")
objshell.run "Net Use . . . . . . . . .",vbhide
----------------------------------------------------

Re: mapping network drive inside application -is it possible?

Posted: Tue May 09, 2017 2:17 pm
by edk
Marek, why do you want to map network drives?
Instead of the drive letter, you can refer directly to the network resource by the UNC path, for example:
Instead of USE F:\test.dbf,
where F: is the mapped resource \\server\DriveD
you can:
USE \\server\DriveD\test.dbf

Re: mapping network drive inside application -is it possible?

Posted: Tue May 09, 2017 4:30 pm
by mol
You are right, Edward, but win7 need user and password for shared folder. Using UNC like in your test, I don't have possibility to enter user and pass

Re: mapping network drive inside application -is it possible?

Posted: Tue May 09, 2017 4:32 pm
by mol
andyglezl wrote: Tue May 09, 2017 2:01 pm Maybe this...

ShellExecute( 0, "Open", "Net Use", "Parameters. . . . .", NULL, 0 )

or

File.vbs
----------------------------------------------------
set objshell = createobject("wscript.shell")
objshell.run "Net Use . . . . . . . . .",vbhide
----------------------------------------------------
I thought about such a method, thanks!

Re: mapping network drive inside application -is it possible?

Posted: Tue May 09, 2017 6:20 pm
by edk
mol wrote: Tue May 09, 2017 4:30 pm You are right, Edward, but win7 need user and password for shared folder. Using UNC like in your test, I don't have possibility to enter user and pass
Ale Marek, jak użytkownik jest zalogowany na stacji z uprawnieniami dostępu do serwera na win7 to nie potrzebujesz już podawać poświadczeń.
Poza tym można ustawić Win7/8/10 tak aby można było mieć dostęp do współdzielonego zasobu jako gość bez hasła. Otwierasz Centrum sieci, Zmień zaawansowane ustawienia udostępniania, rozwijasz Wszystkie sieci, wybierasz Wyłącz udostępnianie chronione hasłem.

Re: mapping network drive inside application -is it possible?

Posted: Tue May 09, 2017 7:31 pm
by mol
Dzięki za podpowiedź!

Re: mapping network drive inside application -is it possible?

Posted: Tue May 09, 2017 7:38 pm
by KDJ
Marek

Here is my package of functions in C for mapping network drive (with example):

Code: Select all

#include "hmg.ch"

MEMVAR _HMG_SYSDATA

FUNCTION Main()
  LOCAL cDrive    := "Z:"
  LOCAL cResource := "\\www.ajaxfilebrowser.com/Userc5aebf4"
  LOCAL cUser     := ""
  LOCAL cPassword := ""

  SET FONT TO 'MS Shell Dlg', 8

  DEFINE WINDOW Main;
    MAIN;
    ROW     100;
    COL     100;
    WIDTH   350;
    HEIGHT  170;
    TITLE   "Map network drive";
    ON INIT MainUpdate(cDrive)

    DEFINE LABEL Drive
      ROW    10
      COL    10
      WIDTH  300
      HEIGHT 13
      VALUE  "Drive: " + cDrive
    END LABEL

    DEFINE LABEL Resource
      ROW    30
      COL    10
      WIDTH  300
      HEIGHT 13
      VALUE  "Resource: " + cResource
    END LABEL

    DEFINE LABEL User
      ROW    50
      COL    10
      WIDTH  300
      HEIGHT 13
      VALUE  "User: " + cUser
    END LABEL

    DEFINE LABEL Password
      ROW    70
      COL    10
      WIDTH  300
      HEIGHT 13
      VALUE  "Password: " + cPassword
    END LABEL

    DEFINE BUTTON Connect
      ROW     100
      COL     10
      WIDTH   100
      HEIGHT  25
      CAPTION "Connect (Map)"
      ACTION  Connect(cDrive, cResource, cUser, cPassword)
    END BUTTON

    DEFINE BUTTON Disconnect
      ROW     100
      COL     120
      WIDTH   100
      HEIGHT  25
      CAPTION "Disconnect"
      ACTION  Disconnect(cDrive)
    END BUTTON

    DEFINE BUTTON Browse
      ROW     100
      COL     230
      WIDTH   100
      HEIGHT  25
      CAPTION "Browse"
      ACTION  Browse(cDrive)
    END BUTTON

    ON KEY ESCAPE ACTION Main.RELEASE
  END WINDOW

  Main.ACTIVATE

RETURN NIL


FUNCTION MainUpdate(cDrive)
  LOCAL nType := GetDriveType(cDrive + '\')

  Main.Drive.VALUE := "Drive: " + cDrive + If(nType == 1, "", " (connected)")

  Main.Disconnect.ENABLED := (nType != 1 /*DRIVE_NO_ROOT_DIR*/)
  Main.Browse.ENABLED     := (nType != 1 /*DRIVE_NO_ROOT_DIR*/)

  Main.Connect.SETFOCUS

RETURN NIL


FUNCTION Connect(cDrive, cRes, cUser, cPass)
  LOCAL nType := GetDriveType(cDrive + '\')
  LOCAL nError

  IF (nType == 1 /*DRIVE_NO_ROOT_DIR*/)
    nError := NetAddConnection(cDrive, cRes, cUser, cPass)

    IF nError != 0
      MsgBox("Can not connect." + CRLF + "Error code: " + HB_NtoS(nError) + CRLF + GetSystemErrorMessage(nError))
    ENDIF
  ELSE
    MsgBox("Can not connect, drive is already connected!")
  ENDIF

  MainUpdate(cDrive)

RETURN NIL


FUNCTION Disconnect(cDrive)

  NetCancelConnection(cDrive, .T. /*force disconnect*/)
  MainUpdate(cDrive)

RETURN NIL


FUNCTION Browse(cDrive)

  EXECUTE FILE 'explorer.exe' PARAMETERS '/e, ' + cDrive

RETURN NIL


#pragma BEGINDUMP

#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"

#include <windows.h>
#include "hbapi.h"


      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364939(v=vs.85).aspx
      // GetDriveType(cRootPathName)
HB_FUNC( GETDRIVETYPE )
{
  hb_retni(GetDriveType(HMG_parc(1)));
}


      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa385413(v=vs.85).aspx
      // NetAddConnection(cDrive, cResource, cUser, cPass)
HB_FUNC( NETADDCONNECTION )
{
  NETRESOURCE NetRes;

  NetRes.dwType       = RESOURCETYPE_DISK;
  NetRes.lpLocalName  = HMG_parc(1);
  NetRes.lpRemoteName = HMG_parc(2);
  NetRes.lpProvider   = NULL;

  hb_retni(WNetAddConnection2(&NetRes, HMG_parc(4), HMG_parc(3), CONNECT_UPDATE_PROFILE));
}


      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa385427(v=vs.85).aspx
      // NetCancelConnection(cDrive, lForce)
HB_FUNC( NETCANCELCONNECTION )
{
  hb_retni(WNetCancelConnection2(HMG_parc(1), CONNECT_UPDATE_PROFILE, (BOOL) hb_parl(2)));
}


      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa385453(v=vs.85).aspx
      // NetGetConnection(cDrive)
HB_FUNC( NETGETCONNECTION )
{
  TCHAR lpName[MAX_PATH];
  DWORD nBufLen = MAX_PATH;

  if (WNetGetConnection(HMG_parc(1), lpName, &nBufLen) == NO_ERROR)
    HMG_retc(lpName);
  else
    HMG_retc(NULL);
}


      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa385476(v=vs.85).aspx
      // NetGetUser(cDrive)
HB_FUNC( NETGETUSER )
{
  TCHAR lpName[MAX_PATH];
  DWORD nBufLen = MAX_PATH;

  if (WNetGetUser(HMG_parc(1), lpName, &nBufLen) == NO_ERROR)
    HMG_retc(lpName);
  else
    HMG_retc(NULL);
}


      // https://msdn.microsoft.com/en-us/library/windows/desktop/ms679351(v=vs.85).aspx
      // GetSystemErrorMessage(nError)
HB_FUNC( GETSYSTEMERRORMESSAGE )
{
  DWORD  dwError = hb_parni(1);
  LPVOID lpMsgBuf;

  FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | 72,
    NULL,
    dwError,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    (LPTSTR) &lpMsgBuf,
    0,
    NULL);

  HMG_retc(lpMsgBuf);

  //HeapFree(GetProcessHeap(), 0, lpMsgBuf);
  LocalFree(lpMsgBuf);
}

#pragma ENDDUMP

Re: mapping network drive inside application -is it possible?

Posted: Wed May 10, 2017 6:13 am
by mol
Thank you, Krzysztof!

Re: mapping network drive inside application -is it possible?

Posted: Wed May 10, 2017 8:59 am
by mol
It really works great! Thanks again!