?
Solved

Programmatically sharing a folder !

Posted on 2001-07-10
19
Medium Priority
?
1,145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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
 

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 75 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

777 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