NetShareAdd: Error handling

Hi,

Running to code below gives me error = 8.
Can anybody tell me what this error means and how it might be solved?
In fact other errors should be handled as well, so at least an informative message can be presented to the user.

oh, but when you say you get an error = 8 , you might be talking about the last parameter, not the function result (as Aflarin has modified the code to display it).

If that is the case, you are mistaken about the signification of this last parameter :

parm_err [out] :
Pointer to a value that receives the index of the first member of the share information structure that causes the ERROR_INVALID_PARAMETER error. If this parameter is NULL, the index is not returned on error. For more information, see the NetShareSetInfo function.

=> I am not sure about the index thing, but it might be the password that is not correct. Have you tried with and empty string instead of nil ?

No, it isn't the error code.
In the initial function the error code indicates the field of structure where the error happens.

It seems the error code is ERROR_INVALID_PARAMETER and the problem parameter is shi2_PassWord

0

Stef MerlijnDeveloperAuthor Commented: 2010-08-30

Your improved version gives error: 123 8
Which is not much better... :-)
Getting this error seems a bit strange as there is more then plenty storage and memory on my computer.
Bye the way I try to share folder:
C:\Documents and Settings\All Users\Documenten\MyAppName

When I try to share the same folder from within Windows Explorer, I get the message that there is already a share available with the same ShareName (NetName). In my case "MyApp"
So I need to remove the old share first.

So I need some function that will remove a share by it's name (if it exists).
My Application will then have one share assigned to it (to it's base-folder).
Moving the base-folder will delete the share of the old base-folder and create a new share for the newly assigned base-folder.

Yes.
Asuming that my application generates some unique sharename, It should never be created twice. So if it already exists as a sharename, then the old share can be removed and the new share can be created/added.
Do you agree with this?

Then there is the issue about how to remove the old share?
If a sharename is already present and it's NOT covering the sharing of the new base-Folder, then it must be removed so a new share can be created with the same name.
This also elliminates the problem of having multiple shares that aren't used anymore, but still give full access to all users.

Really, that is not a problem with sharing a folder that is already shared, as in this case IT IS JUST REPLACED.

Are you sure you can share the folder you want using windows explorer ?

0

Stef MerlijnDeveloperAuthor Commented: 2010-08-30

epasquier: Yes I can share the folder in Windows Explorer.
The folder nor the drivepath are shared. Only the default admninistrative share "C$" is available. When I select the same folder in my aplication, then it gives the mentioned error (even with a completely unique sharename).systan:I believe your code could result in an endless loop. If for some other reason, than a duplicate sharename, the share can't be created.

If you are trying to create share on other PC, you have to use the first param of NetShareAdd

0

Stef MerlijnDeveloperAuthor Commented: 2010-08-30

Can I use the first param of NetShareAdd even when I'm on a local computer?
That would make things a lot easier for me, as I store the UNC-path in the database (because an other user in the network needs to be pointed to the same folder on the server)..

>> Can I use the first param of NetShareAdd even when I'm on a local computer?

I think you can, but it isn't a good idea. Why?

1. At the first you have to extract Server name and Local Path from UNC in any event. (Because server name is the first param on NetShareAdd and the local path is the shi2_Path of structure.

2. At the second... I'm not sure how Windows handles if you are using server name param on local machine. It should be ok, but... I don't like a risk in such cases

0

Stef MerlijnDeveloperAuthor Commented: 2010-08-30

In my database I store two paths:

Base-folder

Templates-folder

Both are always located on the server.
Currently these paths are stored as UNCpath.
So I need to translate the UNC-path back to a local path in order to check for shares.
Can this be done easily?
Or else I need to do a reset of these paths, so the user can select them again. And then store it as local filepaths in the database?

0

Stef MerlijnDeveloperAuthor Commented: 2010-08-30

You can forget the last conversion question of the UNC-path.
Got that covered.

0

Stef MerlijnDeveloperAuthor Commented: 2010-08-30

Thank you all.
aflarin has posted the first solution that supplies a informative errormessage and also came up with the cause of the error while sharing a folder (=> don't use a UNCpath locally)

>>So I need to translate the UNC-path back to a local path in order to check for shares.
Thats the answer of your need, what do you think?

0

Stef MerlijnDeveloperAuthor Commented: 2010-08-30

It's worth testing, but for now I got all the basics covered.
The thing you do with AFolder to get the local path is something I do, but then before I go into this functies.
For the rest your function look good.