Solved

Programmatically sharing a folder !

Posted on 2001-07-10
19
1,090 Views
Last Modified: 2013-12-03
Hi,

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

Thanks for answer.
Mike
0
Comment
Question by:mike_marquet
  • 10
  • 5
  • 2
  • +1
19 Comments
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6268226
You need to use Distributed File System (Dfs) Functions:
NetDfsAdd()
NetDfsRemove()
0
 

Author Comment

by:mike_marquet
ID: 6268264
Thanks, but how about Windows 95/98 & Me !
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6268316
NetShareAdd();
NetShareDel();
0
 

Author Comment

by:mike_marquet
ID: 6268480
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
 
LVL 32

Expert Comment

by:jhance
ID: 6268672
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
 

Author Comment

by:mike_marquet
ID: 6268703
OK, but I have this problem under Windows NT 4.0.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6269219
There is ALSO a code sample for NT4 on the NetShareAdd() SDK page.  Did you even look at it?
0
 

Author Comment

by:mike_marquet
ID: 6269358
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
 

Author Comment

by:mike_marquet
ID: 6269359
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mike_marquet
ID: 6269573
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
 
LVL 4

Expert Comment

by:aponcealbuerne
ID: 6269624
try changing
stShareInfo2.shi2_path = TEXT("C:\\TEMP");
with
stShareInfo2.shi2_path = TEXT("C:\\TEMP\\");
adding "\\"

hope helps
0
 

Author Comment

by:mike_marquet
ID: 6272050
Already tested. Doesn't works.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6272848
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
 

Author Comment

by:mike_marquet
ID: 6272873
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
 
LVL 32

Accepted Solution

by:
jhance earned 25 total points
ID: 6272929
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
 
LVL 4

Expert Comment

by:aponcealbuerne
ID: 6273168

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
 

Author Comment

by:mike_marquet
ID: 6275886
To aponcealbuerne :

Already tested, don't works.
0
 

Author Comment

by:mike_marquet
ID: 6277130
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
 
LVL 32

Expert Comment

by:jhance
ID: 6278121
In essence, what I suggested that you do.....i.e. pass UNICODE to the function rather than ASCII...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Technet Professional promotion code - Looking for a current promotion code 13 75
Need example 5 107
Visual Studio Debugging 3 121
Trouble with References... 5 43
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now