Solved

Programmatically sharing a folder !

Posted on 2001-07-10
19
1,077 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
Comment Utility
You need to use Distributed File System (Dfs) Functions:
NetDfsAdd()
NetDfsRemove()
0
 

Author Comment

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

Expert Comment

by:MadYugoslav
Comment Utility
NetShareAdd();
NetShareDel();
0
 

Author Comment

by:mike_marquet
Comment Utility
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
Comment Utility
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
Comment Utility
OK, but I have this problem under Windows NT 4.0.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:mike_marquet
Comment Utility
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
Comment Utility
try changing
stShareInfo2.shi2_path = TEXT("C:\\TEMP");
with
stShareInfo2.shi2_path = TEXT("C:\\TEMP\\");
adding "\\"

hope helps
0
 

Author Comment

by:mike_marquet
Comment Utility
Already tested. Doesn't works.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
To aponcealbuerne :

Already tested, don't works.
0
 

Author Comment

by:mike_marquet
Comment Utility
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
Comment Utility
In essence, what I suggested that you do.....i.e. pass UNICODE to the function rather than ASCII...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

11 Experts available now in Live!

Get 1:1 Help Now