Results 1 to 7 of 7

Thread: Translate some C++ to VB for me ?

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Translate some C++ to VB for me ?

    I'd do it, 'cept im not all that hot at it...

    Code:
    // === Structures and Definitions =============================================
    
    struct share_info_50 {
            char            shi50_netname[ 13 ];
            unsigned char   shi50_type;
            unsigned short  shi50_flags;
            char FAR *      shi50_remark;
            char FAR *      shi50_path;
            char            shi50_rw_pssword[ 9 ];
            char            shi50_ro_password[ 9 ];
    };
    
    typedef struct _SHARE_INFO_2 {
        LPSTR  shi2_netname;
        DWORD   shi2_type;
        LPSTR  shi2_remark;
        DWORD   shi2_permissions;
        DWORD   shi2_max_uses;
        DWORD   shi2_current_uses;
        LPSTR  shi2_path;
        LPSTR  shi2_passwd;
    } SHARE_INFO_2, *PSHARE_INFO_2, *LPSHARE_INFO_2;
    
    #define SHI50F_RDONLY           0x0001
    #define SHI50F_FULL                     0x0002
    #define SHI50F_DEPENDSON        (SHI50F_RDONLY|SHI50F_FULL)
    #define SHI50F_ACCESSMASK       (SHI50F_RDONLY|SHI50F_FULL)
    
    #define SHI50F_PERSIST          0x0100
    #define  SHI50F_SYSTEM          0x0200
    
    #define STYPE_DISKTREE          0
    
    #define SHI_USES_UNLIMITED      (DWORD)-1
    
    #define NET_API_STATUS          DWORD
    
    // === This is called by an application =======================================
    
    BOOL ShareDirectory( HWND hParent, LPSTR lpShareName, LPSTR lpPath,
                         LPSTR lpRemark, LPSTR lpPassword )
    {
       OSVERSIONINFO osvi;
    
       memset( &osvi, 0, sizeof(OSVERSIONINFO) );     //  Initialize version struct
       osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);           //  Fill in size
       GetVersionEx( &osvi );                               //  Get OS version info
    
       if( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT )        //  If it's WinNT...
          return( WNTShareDirectory( hParent, lpShareName, lpPath,      //  do this
                                     lpRemark, lpPassword ) );
       else
          return( W95ShareDirectory( hParent, lpShareName, lpPath,  //  If it's '95
                                     lpRemark, lpPassword ) );          //  do that
    }
    
    // === Windows 95 Share Function ==============================================
    
    BOOL W95ShareDirectory( HWND hParent, LPSTR lpShareName, LPSTR lpPath,
                            LPSTR lpRemark, LPSTR lpPassword )
    {
       NET_API_STATUS status;
       struct share_info_50 si50;
       HANDLE hModule;
       NET_API_STATUS ( WINAPI *pNetShareAdd )( LPSTR, USHORT, LPBYTE, USHORT );
    
    
       strupr( lpPath );                                 //  Path must be uppercase
       memset( &si50, 0, sizeof(struct share_info_50) );      //  Initialize struct
       strcpy( si50.shi50_netname, lpShareName );          //  Share name is copied
       si50.shi50_type = STYPE_DISKTREE;
       si50.shi50_remark = lpRemark;             //  Remark and path are pointed to
       si50.shi50_path = lpPath;
       strcpy( si50.shi50_rw_password, lpPassword );       //  Passwords are copied
       strcpy( si50.shi50_ro_password, lpPassword );
       si50.shi50_flags = SHI50F_PERSIST | SHI50F_FULL;
       hModule = LoadLibrary( "SVRAPI.DLL" );     //  This is NETAPI32.DLL in WinNT
    
       status = 1;                                   //  Error if module not loaded
       if( hModule )
       {                               //  If DLL loaded, look for sharing function
          (DWORD)pNetShareAdd = (DWORD)GetProcAddress( hModule, "NetShareAdd" );
          if( pNetShareAdd )                       //  And if that's found, call it
             status = pNetShareAdd( NULL, 50, (LPBYTE)&si50,
                                    sizeof(struct share_info_50) );
    
          FreeLibrary( hModule );                     //  Free library if it loaded
       }
    
       if( ! status )                    //  Zero is good...return TRUE on no error
          return( TRUE );
       return( FALSE );                               //  Error brings FALSE return
    }
    
    // === Windows NT Share Function ==============================================
    
    BOOL WNTShareDirectory( HWND hParent, LPSTR lpShareName, LPSTR lpPath,
                            LPSTR lpRemark, LPSTR lpPassword )
    {
       NET_API_STATUS status;
       SHARE_INFO_2 si2;
       HANDLE hModule;
       NET_API_STATUS ( WINAPI *pNetShareAdd )( LPSTR, DWORD, LPBYTE, LPDWORD );
       DWORD dwErr;
       BYTE *bUnicodePath, *bUnicodeShare, *bUnicodeRemark, *bUnicodePassword;
    
    
       strupr( lpPath );                                   //  Make path upper case
    
       bUnicodePath = GlobalAllocPtr( GHND,     //  Allocate space for Unicode path
                                      ( strlen( lpPath ) * 2 ) + 2 );
       if( ! bUnicodePath )
          return( FALSE );
    
       bUnicodeShare = GlobalAllocPtr( GHND,    //  Allocate space for Unicode name
                                       ( strlen( lpShareName ) * 2 ) + 2 );
       if( ! bUnicodeShare )
       {
          GlobalFreePtr( bUnicodePath );
          return( FALSE );
       }
                                  //  Convert one byte strings into Unicode strings
       MultiByteToWideChar( CP_ACP, MB_ERR_INVALID_CHARS,
                            lpPath, strlen( lpPath ),
                            bUnicodePath, ( strlen( lpPath ) * 2 ) + 2 );
    
       MultiByteToWideChar( CP_ACP, MB_ERR_INVALID_CHARS,
                            lpShareName, strlen( lpShareName ),
                            bUnicodeShare, ( strlen( lpShareName ) * 2 ) + 2 );
    
       if( strlen( lpPassword ) )            //  If the password is not zero length
       {                                                  //  Convert it to Unicode
          bUnicodePassword = GlobalAllocPtr( GHND,
                                             ( strlen( lpPassword ) * 2 ) + 2 );
          if( bUnicodePassword )
             MultiByteToWideChar( CP_ACP, MB_ERR_INVALID_CHARS,
                                  lpPassword, strlen( lpPassword ),
                                  bUnicodePassword,
                                  ( strlen( lpPassword ) * 2 ) + 2 );
          else
             bUnicodePassword = NULL;
       }
       else
          bUnicodePassword = NULL;
    
       if( strlen( lpRemark ) )            //  And if the remark is not zero length
       {                                             //  convert it to Unicode, too
          bUnicodeRemark = GlobalAllocPtr( GHND,
                                             ( strlen( lpRemark ) * 2 ) + 2 );
          if( bUnicodeRemark )
             MultiByteToWideChar( CP_ACP, MB_ERR_INVALID_CHARS,
                                  lpRemark, strlen( lpRemark ),
                                  bUnicodeRemark,
                                  ( strlen( lpRemark ) * 2 ) + 2 );
          else
             bUnicodeRemark = NULL;
       }
       else
          bUnicodeRemark = NULL;      //  If either is zero length, pointer is NULL
    
       memset( &si2, 0, sizeof(SHARE_INFO_2) );
       si2.shi2_netname = bUnicodeShare;                 //  Point to name and path
       si2.shi2_path = bUnicodePath;
       si2.shi2_type = STYPE_DISKTREE;
       si2.shi2_permissions = 0;
       si2.shi2_max_uses = SHI_USES_UNLIMITED;
    
       if( ! bUnicodeRemark )                  //  Point to valid string or nothing
          si2.shi2_remark = "\0\0";
       else
          si2.shi2_remark = bUnicodeRemark;
    
       if( ! bUnicodePassword )                //  Point to valid string or nothing
          si2.shi2_passwd = "\0\0";
       else
          si2.shi2_passwd = bUnicodePassword;
    
       hModule = LoadLibrary( "NETAPI32.DLL" );      //  Win95 should be SVRAPI.DLL
    
       status = 1;                                    //  Initialize to error state
       if( hModule )                                            //  Load the module
       {                                        //  If loaded, get function address
          (DWORD)pNetShareAdd = (DWORD)GetProcAddress( hModule, "NetShareAdd" );
          if( pNetShareAdd )                         //  And if that works, call it
             status = pNetShareAdd( "", 2, (LPBYTE)&si2, &dwErr );
    
          FreeLibrary( hModule );
       }
    
       if( bUnicodePassword )                     //  Free all used Unicode strings
          GlobalFreePtr( bUnicodePassword );
       if( bUnicodeRemark )
          GlobalFreePtr( bUnicodeRemark );
    
       GlobalFreePtr( bUnicodeShare );
       GlobalFreePtr( bUnicodePath );
    
       if( ! status ) //  Return TRUE if no error, FALSE if error
          return( TRUE );
       return( FALSE );
    }

  2. #2
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    what's is supposed to do?
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  3. #3
    jim mcnamara
    Guest
    Darre1 - it adds a network share to the PC

    jamie - Instead of me typing for two days go to VBNET
    and cut & paste the VB code that adds shares:

    http://www.mvps.org/vbnet/code/network/netconnect.htm

  4. #4
    Addicted Member Osiris's Avatar
    Join Date
    Oct 2000
    Location
    Dimension Hole
    Posts
    142
    darre, your quote or signature will give you an error message:

    "if without end if"
    ؊Ϯϊ

  5. #5

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Thats not what I want to do. I want to share a local drive, not connect to another share or map a drive...

  6. #6
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Here's code that'll share a folder on NT+ with defaulft permissions. Use NULL instead of a computer name to use the local computer.

    VB Code:
    1. 'THIS CODE IS FOR NT/2000
    2. '5-3-2001 by JoshT
    3.  
    4. Option Explicit
    5. Option Base 0
    6.  
    7. Private Const STYPE_DISKTREE As Long = 0 'disk drive
    8. Private Const STYPE_PRINTQ As Long = 1 'printer
    9. Private Const STYPE_DEVICE As Long = 2
    10. Private Const STYPE_IPC As Long = 3
    11.  
    12. Private Type SHARE_INFO_2
    13.     shi2_netname As String 'LPWSTR
    14.     shi2_type As Long 'DWORD
    15.     shi2_remark As String 'LPWSTR
    16.     shi2_permissions As Long 'DWORD
    17.     shi2_max_uses As Long 'DWORD
    18.     shi2_current_uses As Long 'DWORD
    19.     shi2_path As String 'LPWSTR
    20.     shi2_passwd As String 'LPWSTR
    21. End Type
    22.  
    23. 'NetShareAdd returns 0 if no error
    24. '
    25. 'lpwstrServerName As Any, --the computer to create the share on,
    26. '                           NULL for local computer,
    27. '                           otherwise must begin with "\\"
    28. 'ByVal dwordLevel As Long, -- specifies the type struct that contains the data
    29. '       2 for SHARE_INFO_2 Struct
    30. '       502 for SHARE_INFO_502 Struct
    31. 'ByVal lpbyteBuf As Any, -- pointer to the struct who type was specified above
    32. 'lpdwordParmErr As Long, -- if an error in the struct, the number of the struct's parameter
    33. '                            that caused the error (0 is no error)
    34.  
    35. Private Declare Function NetShareAdd Lib "netapi32.dll" _
    36.     (lpwstrServerName As Byte, _
    37.     ByVal dwordLevel As Long, _
    38.     ByVal lpbyteBuf As Long, _
    39.     lpdwordParmErr As Long) As Long
    40.    
    41.  
    42.  
    43. Private Sub TestIt()
    44.     Dim si2 As SHARE_INFO_2
    45.     Dim retval As Long
    46.     Dim parmerr As Long
    47.     Dim compname() As Byte
    48.    
    49.     parmerr = 0
    50.     'compname = vbNullString
    51.     compname() = "\\COMPNAME" & vbNullChar
    52.    
    53.     si2.shi2_netname = "Test2C" & vbNullChar 'the name of the share
    54.     si2.shi2_type = STYPE_DISKTREE 'the share is on the disk
    55.     si2.shi2_remark = "Testing Out NetShareAdd VB API" & vbNullChar 'the comment
    56.     si2.shi2_permissions = 0 'this should be ignored
    57.     si2.shi2_max_uses = -1 'unlimited connections
    58.     si2.shi2_current_uses = 0 'I don't think this is applicable
    59.     si2.shi2_path = "C:\TEMP" & vbNullChar 'the path to the share
    60.     si2.shi2_passwd = vbNullString 'NULL, the password 'this should be ignored
    61.     retval = NetShareAdd(compname(0), 2, VarPtr(si2), parmerr)
    62.    
    63. End Sub
    64.  
    65. Private Sub Form_Load()
    66.     TestIt
    67. End Sub
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  7. #7

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    heh Ive been searching the forums josh, and I knew you were going to reply with some code to do it for NT/2000
    I need it for 9x/Me though... and the above code in C would appear to do that ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width