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
Solved

Programmatically sharing a folder !

Posted on 2001-07-10
19
1,115 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Alternative to visual sourcesafe 2 131
Problem to packaging 1 98
What are new names for Microsoft test frameworks once called Moles and Pex? 2 61
What is MicroStrategy.NET? 2 70
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…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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