API NetShareAdd not really working 
Author Message
 API NetShareAdd not really working

Hi,

I'm trying to share a folder programmatically, but with no luck till now.

I used code from
http://www.*-*-*.com/

The problem is that it creates the network share, you can see the shared
folder from other PCs in the LAN, but when trying to access it you get
access denied.

I also tried with this sample:
http://www.*-*-*.com/
with the same result.

I've been searching this problem and found that other people had the
same issue, but they didn't get any good answer, for example:

http://www.*-*-*.com/

http://www.*-*-*.com/

http://www.*-*-*.com/

Any idea?

PS: if you want, I post my full code.



Sun, 12 Feb 2012 19:50:14 GMT  
 API NetShareAdd not really working
Eduardo escribi:

Quote:
> Hi,

> I'm trying to share a folder programmatically, but with no luck till now.

> I used code from
> http://vbnet.mvps.org/index.html?code/network/netshareadd.htm

> The problem is that it creates the network share, you can see the shared
> folder from other PCs in the LAN, but when trying to access it you get
> access denied.

> I also tried with this sample:
> http://allapi.mentalis.org/apilist/30C35D643B20E5268C18104CC64EC775.html
> with the same result.

> I've been searching this problem and found that other people had the
> same issue, but they didn't get any good answer, for example:

> http://www.codeguru.com/forum/showthread.php?t=312194

> http://www.xtremevbtalk.com/showthread.php?t=99336

> http://social.msdn.microsoft.com/Forums/en-US/windowssdk/thread/51065...

> Any idea?

> PS: if you want, I post my full code.

More info:

I'm testing it with two computers that are connected, one with XP and
the other one with Vista.

If I share a local folder by code from any of them, from the other one I
can see the folder but get access denied when trying to access it.

When I manually share a folder in te one with XP, I can see and also
access it from the one with Vista with no problem.

But when I manually share a folder in the one with Vista, I also get
access denied when trying to access it from the one with XP.

Here:
http://social.msdn.microsoft.com/Forums/en-US/windowssdk/thread/51065...
one person suggest to use SetAcl (from http://setacl.sourceforge.net/ )
to set access permissions on all the files and folders contained on the
shared folder.

I thought that sharing a folder was a trivial operation, but it seems
that I was wrong...



Sun, 12 Feb 2012 20:37:26 GMT  
 API NetShareAdd not really working

Quote:

> Eduardo escribi:

> Here:

http://social.msdn.microsoft.com/Forums/en-US/windowssdk/thread/51065...

Quote:
> one person suggest to use SetAcl (from http://setacl.sourceforge.net/ )
> to set access permissions on all the files and folders contained on the
> shared folder.

> I thought that sharing a folder was a trivial operation, but it seems
> that I was wrong...

Well, if you think about it for a bit, you'll soon realize that allowing a
program to willy-nilly 'share' a folder or any other resource to the
'outside' represents a rather large security hole.

When presented with this need we have on occasion been able to force the
issue using multiple steps (as suggested by the SetAcl example) within a
controlled context. But such methods tend to be unreliable and doomed for
failure in the 'wild'.

This is an unsatisfactory answer, but IMHO such operations are best
facilitated at a higher-level using Administrator scripts (WMI) or
interactively with managment utilities.

-ralph



Sun, 12 Feb 2012 21:07:33 GMT  
 API NetShareAdd not really working
Ralph escribi:

Quote:
> Well, if you think about it for a bit, you'll soon realize that allowing a
> program to willy-nilly 'share' a folder or any other resource to the
> 'outside' represents a rather large security hole.

Yes, it's clear that allowing this is a security problem, but (I think)
that making it harder is not a solution though. You only get rid of the
lammers, but not the real hackers.

 > When presented with this need...

That what I had in mind when I said "I thought that sharing a folder was
a trivial operation...", that it was something that many programs needed
to do.

For me it's the first time that I need to make a program work in a net
environment, and I want to help the user to configure it as much as
possible, because I want to avoid support calls or e-mails.
And I'm not a a net specialist, sometimes I have hard times to make my
own net to work, so I would like to avoid troubles.

Quote:
> When presented with this need we have on occasion been able to force the
> issue using multiple steps (as suggested by the SetAcl example) within a
> controlled context. But such methods tend to be unreliable and doomed for
> failure in the 'wild'.

> This is an unsatisfactory answer, but IMHO such operations are best
> facilitated at a higher-level using Administrator scripts (WMI) or
> interactively with managment utilities.

OK, thank you Ralph.
May be I'll open a new question asking: "How is the usual way for
working with an Access database in a net environment?"


Sun, 12 Feb 2012 23:55:42 GMT  
 API NetShareAdd not really working
As Ralph suggested, this is normal. Sharing is one thing, and setting the
permissions is another. However, if this is intended for general use,
downloadable product, then leave both the sharing and permissions to the end
user's network administrators. If you do it yourself, they will hate it
because it seems like someone changed their network security without their
knowledge.

If this is for a single user, then it can be done with a batch file using
"NET SHARE" and CACLS or SetACL. In VB, you can set the permissions using
the routines in the following article:

How to use low-level access control APIs from Visual Basic
http://support.microsoft.com/kb/316440

Also see Q240176: "HOWTO: Set Security on a NTFS Folder Programmatically" in
MSDN Library Oct 2001, but the routines in the article above are easier to
use.



Mon, 13 Feb 2012 00:35:18 GMT  
 API NetShareAdd not really working
Nobody escribi:

Quote:
> As Ralph suggested, this is normal. Sharing is one thing, and setting the
> permissions is another. However, if this is intended for general use,
> downloadable product, then leave both the sharing and permissions to the end
> user's network administrators. If you do it yourself, they will hate it
> because it seems like someone changed their network security without their
> knowledge.

Well, I made an step by step assistant to configure client/server/etc.
and if it's configured as being server, in a step it points that they
need to share the "x" folder with the "y" name, and it has a button to
share it from there (but only if they want).

It also has a timer checking all the time if the folder is shared with
the correct name and showing in a label whether it's already shared or not.

 > If this is for a single user

Usually they are network administrators

Quote:

> If this is for a single user, then it can be done with a batch file using
> "NET SHARE" and CACLS or SetACL. In VB, you can set the permissions using
> the routines in the following article:

> How to use low-level access control APIs from Visual Basic
> http://support.microsoft.com/kb/316440

I'll have to study this.

Quote:

> Also see Q240176: "HOWTO: Set Security on a NTFS Folder Programmatically" in
> MSDN Library Oct 2001, but the routines in the article above are easier to
> use.

I'm also thinking about the option of just opening the folder's property
dialog and let them to do it.

What would you suggest?

Thanks.



Mon, 13 Feb 2012 00:55:53 GMT  
 API NetShareAdd not really working

Quote:

> I'm also thinking about the option of just opening the folder's property
> dialog and let them to do it.

> What would you suggest?

Besides what you are trying to do, I would put the path in an INI entry and
give them instructions on how to check it for troubleshooting.

If you want to open the properties dialog, you could use
SHObjectProperties(). You specify "Security" as the default tab name to
show. I am not sure if the name need to be changed for non-English versions
of the OS. Also, there is a warning that it maybe changed or altered in
future versions.

SHObjectProperties Function:
http://msdn.microsoft.com/en-us/library/bb762231(VS.85).aspx

Sample:

Option Explicit

Const SHOP_PRINTERNAME = 1
Const SHOP_FILEPATH = 2
Const SHOP_VOLUMEGUID = 4
Private Declare Function SHObjectProperties Lib "shell32.dll" ( _
    ByVal hwnd As Long, ByVal shopObjectType As Long, _
    ByVal pszObjectName As Long, ByVal pszPropertyPage As Long) As Long

Private Sub Form_Load()
    Debug.Print ShowSecurityDialog("C:\")
End Sub

Private Function ShowSecurityDialog(sPath As String) As Long
    ShowSecurityDialog = SHObjectProperties(0, SHOP_FILEPATH, _
        StrPtr(sPath), StrPtr("Security"))
End Function



Mon, 13 Feb 2012 01:28:38 GMT  
 API NetShareAdd not really working

Quote:
> SHObjectProperties()

There is also ShowShareFolderUIW() without warnings for future use, but it's
for XP+. It shows the Sharing rather than the Security tab. Declaration and
usage:

Private Declare Function ShowShareFolderUIW Lib "ntshrui.dll" ( _
    ByVal hwnd As Long, ByVal pszPath As Long) As Long

Debug.Print ShowShareFolderUIW(0, StrPtr("C:\"))



Mon, 13 Feb 2012 01:39:39 GMT  
 API NetShareAdd not really working
Nobody escribi:

Quote:


>> SHObjectProperties()

> There is also ShowShareFolderUIW() without warnings for future use, but it's
> for XP+. It shows the Sharing rather than the Security tab. Declaration and
> usage:

> Private Declare Function ShowShareFolderUIW Lib "ntshrui.dll" ( _
>     ByVal hwnd As Long, ByVal pszPath As Long) As Long

> Debug.Print ShowShareFolderUIW(0, StrPtr("C:\"))

Strangely, I'm testing it on Vista Ultimate, and I get error 453 "Can't
find DLL entry point ShowShareFolderUIW in ntshrui.dll"


Mon, 13 Feb 2012 02:03:29 GMT  
 API NetShareAdd not really working
Eduardo escribi:

Quote:
> Nobody escribi:


>>> SHObjectProperties()

>> There is also ShowShareFolderUIW() without warnings for future use,
>> but it's for XP+. It shows the Sharing rather than the Security tab.
>> Declaration and usage:

>> Private Declare Function ShowShareFolderUIW Lib "ntshrui.dll" ( _
>>     ByVal hwnd As Long, ByVal pszPath As Long) As Long

>> Debug.Print ShowShareFolderUIW(0, StrPtr("C:\"))

> Strangely, I'm testing it on Vista Ultimate, and I get error 453 "Can't
> find DLL entry point ShowShareFolderUIW in ntshrui.dll"

On Vista it's ShowShareFolderUI (not ShowShareFolderUIW)


Mon, 13 Feb 2012 02:06:30 GMT  
 API NetShareAdd not really working

Quote:

> Eduardo escribi:
>> Nobody escribi:


>>>> SHObjectProperties()

>>> There is also ShowShareFolderUIW() without warnings for future use, but
>>> it's for XP+. It shows the Sharing rather than the Security tab.
>>> Declaration and usage:

>>> Private Declare Function ShowShareFolderUIW Lib "ntshrui.dll" ( _
>>>     ByVal hwnd As Long, ByVal pszPath As Long) As Long

>>> Debug.Print ShowShareFolderUIW(0, StrPtr("C:\"))

>> Strangely, I'm testing it on Vista Ultimate, and I get error 453 "Can't
>> find DLL entry point ShowShareFolderUIW in ntshrui.dll"

> On Vista it's ShowShareFolderUI (not ShowShareFolderUIW)

On XP+SP2 it's the opposite. I must add W to the function name, otherwise I
get error 453.


Mon, 13 Feb 2012 02:14:29 GMT  
 API NetShareAdd not really working
Nobody escribi:

Quote:

>> I'm also thinking about the option of just opening the folder's property
>> dialog and let them to do it.

>> What would you suggest?

> Besides what you are trying to do, I would put the path in an INI entry and
> give them instructions on how to check it for troubleshooting.

> If you want to open the properties dialog, you could use
> SHObjectProperties(). You specify "Security" as the default tab name to
> show. I am not sure if the name need to be changed for non-English versions
> of the OS. Also, there is a warning that it maybe changed or altered in
> future versions.

> SHObjectProperties Function:
> http://msdn.microsoft.com/en-us/library/bb762231(VS.85).aspx

> Sample:

> Option Explicit

> Const SHOP_PRINTERNAME = 1
> Const SHOP_FILEPATH = 2
> Const SHOP_VOLUMEGUID = 4
> Private Declare Function SHObjectProperties Lib "shell32.dll" ( _
>     ByVal hwnd As Long, ByVal shopObjectType As Long, _
>     ByVal pszObjectName As Long, ByVal pszPropertyPage As Long) As Long

> Private Sub Form_Load()
>     Debug.Print ShowSecurityDialog("C:\")
> End Sub

> Private Function ShowSecurityDialog(sPath As String) As Long
>     ShowSecurityDialog = SHObjectProperties(0, SHOP_FILEPATH, _
>         StrPtr(sPath), StrPtr("Security"))
> End Function

I'm testing it with the more standard ShellExecuteEx (sample here:
http://allapi.mentalis.org/apilist/AA95A292647BBDF92F3428F026829452.html )

My code so far is:

     ' I want to open an explorer window with the folder already
     ' selected, so:
     Shell "Explorer.exe  /e,/select," & Chr(34) _
         & FolderToShare & Chr(34), vbMaximizedFocus

     ' then show the dialog
     ShowProps FolderToShare, Me.hwnd

The user will need to manually select the right tab and share the folder.

The problem that I'm facing now is that the property screen opens hidden
behind the explorer window.

What I'll need to do is to find the handle of the two windows and use
SetParent to set the property one as child of the explorer window...
ohhh no!



Mon, 13 Feb 2012 02:26:55 GMT  
 API NetShareAdd not really working

Quote:

> I'm testing it with the more standard ShellExecuteEx (sample here:
> http://allapi.mentalis.org/apilist/AA95A292647BBDF92F3428F026829452.html )

> My code so far is:

>     ' I want to open an explorer window with the folder already
>     ' selected, so:
>     Shell "Explorer.exe  /e,/select," & Chr(34) _
>         & FolderToShare & Chr(34), vbMaximizedFocus

>     ' then show the dialog
>     ShowProps FolderToShare, Me.hwnd

> The user will need to manually select the right tab and share the folder.

> The problem that I'm facing now is that the property screen opens hidden
> behind the explorer window.

You will need to wait for explorer window to show up, and use its hWnd
instead of Me.hwnd. Personally, I wouldn't start Explorer.


Mon, 13 Feb 2012 02:37:03 GMT  
 API NetShareAdd not really working
Nobody escribi:

Quote:

>> I'm testing it with the more standard ShellExecuteEx (sample here:
>> http://allapi.mentalis.org/apilist/AA95A292647BBDF92F3428F026829452.html )

>> My code so far is:

>>     ' I want to open an explorer window with the folder already
>>     ' selected, so:
>>     Shell "Explorer.exe  /e,/select," & Chr(34) _
>>         & FolderToShare & Chr(34), vbMaximizedFocus

>>     ' then show the dialog
>>     ShowProps FolderToShare, Me.hwnd

>> The user will need to manually select the right tab and share the folder.

>> The problem that I'm facing now is that the property screen opens hidden
>> behind the explorer window.

> You will need to wait for explorer window to show up, and use its hWnd
> instead of Me.hwnd. Personally, I wouldn't start Explorer.

Why not? What would you do, just show the property screen?

I wanted to show the explorer so they realize that it's a screen of
Windows and not something from the program.
Besides, they can see where the folder actually is in a way that they
are familiar with.

PS: I want to avoid confusion and support calls



Mon, 13 Feb 2012 03:46:15 GMT  
 API NetShareAdd not really working
Nobody escribi:

Quote:
> You will need to wait for explorer window to show up, and use its hWnd
> instead of Me.hwnd.

It seems that the .hwnd member does't work as the handle of the owner
window as the sample in
http://allapi.mentalis.org/apilist/AA95A292647BBDF92F3428F026829452.html
suggests

It's not owned by the vb form in my test.

http://msdn.microsoft.com/en-us/library/bb759784%28VS.85%29.aspx sais:

hwnd
     Optional. A window handle to any message boxes that the system
might produce while executing this function. This value can be NULL.



Mon, 13 Feb 2012 03:54:18 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Really simple thing not working my way =D

2. $$$ BELIEVE IT OR NOT, IT REALLY WORKS! $$$

3. Not really API related but.......

4. Does NetShareAdd work remotely on Win9x?

5. ! How to use the NETSHAREADD API CALL !?

6. NetShareAdd API

7. NetShareAdd API under Win95

8. API NETSHAREADD

9. API NetShareAdd

10. DestroyWindow API call not working?

11. Calling Win32 API not working

12. CopyMemory API not working

 

 
Powered by phpBB® Forum Software