Programmatically sharing a folder !

Hi,

How can I programmatically share a folder in read access only under Windows 95/98 & NT/2000 ?

Thanks for answer.
Mike
mike_marquetAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jhanceConnect With a Mentor Commented:
Check the header file where SHARE_INFO_2 is declared, lmshare.h.  You'll see it has:

typedef struct _SHARE_INFO_2 {
    LPTSTR  shi2_netname;
    DWORD   shi2_type;
    LPTSTR  shi2_remark;
    DWORD   shi2_permissions;
    DWORD   shi2_max_uses;
    DWORD   shi2_current_uses;
    LPTSTR  shi2_path;
    LPTSTR  shi2_passwd;
} SHARE_INFO_2, *PSHARE_INFO_2, *LPSHARE_INFO_2;



What is the type of these strings?  The header files for the NetXXX API are defective (in my opinion) since they use LPTSTR instead of what really should be there, LPWSTR.  There is NOT an ASCII version of NetShareAdd or SHARE_INFO_2 available.  You MUST pass it UNICODE data.

Since you are building an ASCII app, you have to fiddle with this junk.  Since I don't like to directly edit the SDK .h files (since they get overwritten when you update) I'd suggest you just redeclare these in your own app like:

typedef struct _MY_SHARE_INFO_2 {
    LPWSTR  shi2_netname;
    DWORD   shi2_type;
    LPWSTR  shi2_remark;
    DWORD   shi2_permissions;
    DWORD   shi2_max_uses;
    DWORD   shi2_current_uses;
    LPWSTR  shi2_path;
    LPWSTR  shi2_passwd;
} MY_SHARE_INFO_2, *PMY_SHARE_INFO_2, *LPMY_SHARE_INFO_2;

and

NET_API_STATUS NET_API_FUNCTION
NetShareAdd (
    IN  LPWSTR  servername,
    IN  DWORD   level,
    IN  LPBYTE  buf,
    OUT LPDWORD parm_err
    );

As well as any other NetXXX Api functions, structs, or constants out of LMSHARE.H you use.  Then use your "private" versions in your app:

MY_SHARE_INFO_2 stShareInfo2;

etc...

If you are building a strictly UNICODE application this is not an issue.  It's only when you mix ASCII with UNICODE you get this mess.  Microsoft SHOULD have fixed this long ago but at this point I doubt it will ever happen...
0
 
MadYugoslavCommented:
You need to use Distributed File System (Dfs) Functions:
NetDfsAdd()
NetDfsRemove()
0
 
mike_marquetAuthor Commented:
Thanks, but how about Windows 95/98 & Me !
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MadYugoslavCommented:
NetShareAdd();
NetShareDel();
0
 
mike_marquetAuthor Commented:
I try to use NetShareAdd but without success.

here's the sample code :

  SHARE_INFO_2 stShareInfo2;
  DWORD        dwParmErr = 0;

  // Fill in the SHARE_INFO_2 structure
  stShareInfo2.shi2_netname      = TEXT("TEMP");
  stShareInfo2.shi2_type         = STYPE_DISKTREE;
  stShareInfo2.shi2_remark       = TEXT("TEMP to test NetShareAdd");
  stShareInfo2.shi2_permissions  = ACCESS_READ;
  stShareInfo2.shi2_max_uses     = -1;
  stShareInfo2.shi2_current_uses = 0;
  stShareInfo2.shi2_path         = TEXT("C:\\TEMP");
  stShareInfo2.shi2_passwd       = NULL;

  NET_API_STATUS dwRet = NetShareAdd(NULL, 2, (LPBYTE)&stShareInfo2, &dwParmErr);

  if (dwRet) TRACE("dwRet =  %u\tdwParmErr = %u\n", dwRet, dwParmErr);

  // RESULT IS => dwRet = 113    dwParmErr = 8

For testing, I have add this share manually with windows explorer than try to delete it so :

  NET_API_STATUS dwRet = NetShareDel(NULL, TEXT("TEMP"), 0);

  // RESULT IS => dwRet = 2310 (NERR_NetNameNotFound)

But when I enumerate the share names (with NetShareEnum), the share is displayed

Any idea ?

0
 
jhanceCommented:
See the SDK on the NetShareAdd() function.  

This function only supports the SHARE_INFO_50 level on Win9x.  The SDK has a link to sample code for Win9x that I've used may times.
0
 
mike_marquetAuthor Commented:
OK, but I have this problem under Windows NT 4.0.
0
 
jhanceCommented:
There is ALSO a code sample for NT4 on the NetShareAdd() SDK page.  Did you even look at it?
0
 
mike_marquetAuthor Commented:
This code comes from there !

The only difference is this (which I have also tested) :
 stShareInfo2.shi2_permissions = 0;
 stShareInfo2.shi2_max_uses    = 4;
0
 
mike_marquetAuthor Commented:
This code comes from there !

The only difference is this (which I have also tested) :
 stShareInfo2.shi2_permissions = 0;
 stShareInfo2.shi2_max_uses    = 4;
0
 
mike_marquetAuthor Commented:
This code comes from there !

The only difference is this (which I have also tested) :
 stShareInfo2.shi2_permissions = 0;
 stShareInfo2.shi2_max_uses    = 4;
0
 
aponcealbuerneCommented:
try changing
stShareInfo2.shi2_path = TEXT("C:\\TEMP");
with
stShareInfo2.shi2_path = TEXT("C:\\TEMP\\");
adding "\\"

hope helps
0
 
mike_marquetAuthor Commented:
Already tested. Doesn't works.
0
 
jhanceCommented:
Your code is no good for Windows NT.  I'm assuming you are building an ASCII application and not a UNICODE one.  If so then you must be sure that all the LPWSTR paramaters to the function and the struct are UNICODE:

 SHARE_INFO_2 stShareInfo2;
 DWORD        dwParmErr = 0;

 // Fill in the SHARE_INFO_2 structure
 stShareInfo2.shi2_netname      = L"TEMP";
 stShareInfo2.shi2_type         = STYPE_DISKTREE;
 stShareInfo2.shi2_remark       = L"TEMP to test NetShareAdd";
 stShareInfo2.shi2_permissions  = ACCESS_READ;
 stShareInfo2.shi2_max_uses     = -1;
 stShareInfo2.shi2_current_uses = 0;
 stShareInfo2.shi2_path         = L"C:\\TEMP";
 stShareInfo2.shi2_passwd       = NULL;

 NET_API_STATUS dwRet = NetShareAdd(NULL, 2, (LPBYTE)&stShareInfo2, &dwParmErr);
0
 
mike_marquetAuthor Commented:
When I do this, I have this compilation error :

error C2440: '=' : cannot convert from 'unsigned short [10]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
0
 
aponcealbuerneCommented:

This code works for me...


SHARE_INFO_2        shareInfo;
DWORD               status;
shareInfo.shi2_netname =  "TEMP";
shareInfo.shi2_type =         STYPE_DISKTREE;
shareInfo.shi2_remark =       NULL;
shareInfo.shi2_permissions =  PERM_FILE_READ | PERM_FILE_WRITE;
shareInfo.shi2_max_uses =     (DWORD)-1;
shareInfo.shi2_current_uses = 0;
shareInfo.shi2_path =         "C:\\TEMP\\";
shareInfo.shi2_passwd =       NULL;
status = NetShareAdd( NULL, 2, (PBYTE)&shareInfo, NULL );

0
 
mike_marquetAuthor Commented:
To aponcealbuerne :

Already tested, don't works.
0
 
mike_marquetAuthor Commented:
I have found how to make it to work.

Here's what I have done :

// Function to convert from ansi to wide char
void AnsiToWideChar(LPCTSTR lpszAnsi, LPWSTR lpWideChar, int nSize)
 {
  memset(lpWideChar, 0, nSize);

  for (size_t I=0; I<strlen(lpszAnsi); I++)
   {
    lpWideChar[I] = lpszAnsi[I];
   }
 }

// Function to convert from wide char to ansi
CString WideCharToAnsi(LPWSTR lpWideChar)
 {
  CString str;

  int I = 0;

  while (lpWideChar[I])
    {
     //char cLow  = (char)lpWideChar[I];
     //char cHigh = (char)(lpWideChar[I] >> 8);

     str +=  (char)lpWideChar[I];

     I++;
    }

  return str;
 }

// How to add a new share name
  USHORT nFolderName[255];
  USHORT nShareName[255];
  USHORT nComment[255];

  AnsiToWideChar("C:\\TEMP", nFolderName, sizeof(nFolderName));
  AnsiToWideChar("TEMP", nShareName, sizeof(nShareName));
  AnsiToWideChar("no comment", nComment, sizeof(nComment));
 
  SHARE_INFO_2 stShareInfo2;
  DWORD        dwParmErr = 0;

  // Fill in the SHARE_INFO_2 structure
  stShareInfo2.shi2_netname      = (char *)nShareName;
  stShareInfo2.shi2_type         = STYPE_DISKTREE;
  stShareInfo2.shi2_remark       = (char *)nComment;
  stShareInfo2.shi2_permissions  = -1;
  stShareInfo2.shi2_max_uses     = ACCESS_READ;
  stShareInfo2.shi2_current_uses = 0;
  stShareInfo2.shi2_path         = (char *)nFolderName;
  stShareInfo2.shi2_passwd       = NULL;

  NET_API_STATUS dwRet = NetShareAdd(NULL, 2, (LPBYTE)&stShareInfo2, &dwParmErr);
0
 
jhanceCommented:
In essence, what I suggested that you do.....i.e. pass UNICODE to the function rather than ASCII...
0
All Courses

From novice to tech pro — start learning today.