Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Share a local folder to the network 5

Status
Not open for further replies.

ugagrad

Programmer
Sep 12, 2003
49
US
I am trying to share a local folder to the network. I have tried to figure out the function netshareadd. But I can not get it to work. Could anyone help me out Please.

 
Hi
As soon as I get a chance, I will try and get it working on WinNT+, and post it as o VCL component, or .pas file for all *Delphi* user to benefit...

Regards
QuentinB
 
i will do the same good luck to all. Tonhu as far as the dll's go round about where should i grab them in my code.
 
Do the function(pointer) initialisation in a initialisation part of a unit, as well as the finalisation, best in the same unit.

Nb.: The functions are all in a separate unit, in my case.

HTH
TonHu
 
Hello all,
I have been working on the problem and I haven't gotten anywhere. Have any of you. I moved the finalization code to the end of my units execution. Have you guys gotten anywhere on it.
Thanks, [pipe]
 
I worked a bit on it last week and made a small progress, but haven't got much time at the moment. Maybe later during this week.

TIA
TonHu
 
I have finally gotten it to work on a non-NT machine. Does anyone outthere know how to use netshareadd on an NT machine
 
That's the *only* prblem I still have....s-)

TIA
TonHu
 
Hi All
I spent most of last week browsing the net to see if *ANYBODY* has got this working on a NT machine... no luck.[ponder]
The code works fine on a non NT machine, but for some reason, I get a result of "123" or "87" every time. I even tried different API header files from the JEDI Project, still get the same results...[sadeyes]

I changed *All* string / PChar values to widechars and tried all the different *Structs* as specified by Microsoft on their site, but still nothing works.

I hope *We* can crack this thing... Good Luck...

QuentinB
 
I've gotten a little closer, as a new share is created, but still get the errormessage. Proceeding...later prb next week.

TIA
TonHu
 
Hi
Have you gotten futher on NT, If you have, could you maybe post your Code here ??

Then I will try and get futher ASAP.

Thanks
QuentinB
 
I hope that we aren't working on an unsolvable problem. Wouldn't that be terrible.
 
hey guys. I don't know if this helps but on Windows 2000 and XP you can call a program that pulls up a share dialog. It is shrpubw.exe inside of the system32 folder. This is kind of a fix for my problem but I will still try to get the other to work. Later
 
one more thing you can use shell execute to call the program and I will put my actual code on here on monday
 
Hi All

As you can see from my code attached, IT WORKS !!![2thumbsup](PS: The code sample above works on Win9x, the code I pasted works on Win NT)

I would like to thank, my Mom, and my Dad....[bigglasses]

When I get a chance, I will create a non visual component from this, and "Share" it with the world...(If you give me your email addresses, I will send it to you as a beta version)
If you have any problems, feel free to contact me...
------------------------------------------------------------
unit NetTest1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TFormNetTest = class(TForm)
BtnShare: TButton;
procedure BtnShareClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

type
Share_Info2 = Packed Record
shi2_netname : Lpwstr;
shi2_type : DWord;
shi2_remark : Lpwstr;
shi2_permis : DWord;
shi2_maxusers : DWord;
shi2_curusers : DWord;
shi2_path : Lpwstr;
shi2_password : Lpwstr;
shi2_eof: DWord;
end;

var
FormNetTest : TFormNetTest;
Hand : LongWord;

//Net share Add from NetApi32
NetShareAddNT : Function(ServerName : PChar; ShareLevel : DWord; Buffer : Pointer; Error : Pointer) : Integer; StdCall;

implementation

{$R *.DFM}
{------------------------------------------------------------------------------}
Procedure TFormNetTest.BtnShareClick(Sender: TObject);
Var Tmp : Share_Info2;
Result : Integer;
NoError : DWord;
Begin
Tmp.shi2_netname := 'Temp Share';
Tmp.shi2_type := 0; //Disk
Tmp.shi2_remark := 'TestShare';
Tmp.shi2_permis := 0;
Tmp.shi2_maxusers := 4;
Tmp.shi2_curusers := 0;
Tmp.shi2_path := 'C:\Temp';
Tmp.shi2_password := nil;
Result := NetShareAddNT(nil, 2, @Tmp, @NoError);
Case Result of
ERROR_ACCESS_DENIED : ShowMessage('The user does not have access to the requested information.');
ERROR_INVALID_LEVEL : ShowMessage('The value specified for the level parameter is invalid.');
ERROR_INVALID_NAME : ShowMessage('The character or file system name is invalid.');
ERROR_INVALID_PARAMETER : ShowMessage('The specified parameter is invalid.');
ERROR_SUCCESS : ShowMessage('Share Success...');
end;
end;

{------------------------------------------------------------------------------}
Procedure TFormNetTest.FormCreate(Sender: TObject);
Begin
Hand := LoadLibrary('NetApi32');
NetShareAddNT := GetProcAddress(Hand,'NetShareAdd');
end;

{------------------------------------------------------------------------------}
Procedure TFormNetTest.FormClose(Sender: TObject; var Action: TCloseAction);
Begin
If Hand <> 0 then
FreeLibrary(Hand);
end;

{------------------------------------------------------------------------------}
end.
 
HOOK ME UP WITH A BETA VERSION
CRSAVAGE0630@AOL.COM
 
this is great I am so happy [lightsaber]
but does anyone know how to allow a unlimited amount of users
 
This is what I came up with, after seeing the light from QuentinB's post, and a lot of 'Aha-erblebis' s-)

The typecasting was the hard part to find, as there's practically no docs in the Delphi Helpsystem on unicode :-(

(Just the NT specific part)
Code:
function ...
var  WNetName, WRemark, WFilePath, WRWPass : WideString;
...
begin
...
    If IsNT then
    Begin
        NoError := 0;
        WNetName := NetName;  // Cast string to WideString
        MyShareNT.shi2_netname := PWideChar(WNetName);  // Correct Pointertype
        MyShareNT.shi2_type := ShareType;
        MyShareNT.shi2_permis := $7F; // => ACCESS_ALL
        WRemark := Remark;
        MyShareNT.shi2_remark := PWideChar(WRemark);
        MyShareNT.shi2_maxusers := $FFFFFFFF;   // Unlimited users
        WFilePath := FilePath;
        MyShareNT.shi2_path := PWideChar(WFilePath);
        WRWPass := RWPass;
        MyShareNT.shi2_password := PWideChar(WRWPass);
        MyShareNT.shi2_eof := $0;       // No more to follow?
        Result := ERROR_SUCCESS;
        try
            Result := NetShareAddNT(ServerName,2,@MyShareNT,@NoError);
        finally
            Case Result of
                ERROR_ACCESS_DENIED,
                ERROR_INVALID_LEVEL,
                ERROR_INVALID_NAME,
                ERROR_INVALID_PARAMETER:
                    ShowMessage('FilePath : ' + FilePath + ' Size : ' + IntToStr(SizeOf(MyShareNT))+ #13 + #10 + 'Result : ' + IntToStr(Result) + ' ' + GetNetErrorString(2100+Result) + ' Error : ' + IntToStr(NoError));
            End;
        End;
    End
    Else
// Windows 9x part (see my first code-post)
...

Now I'll just refine my UI, and have a nice 'Restore Mappings and Sharings' tool. It already worked fine for mappings on both W9x & W-NT(+W2K, Wxp) but the sharing part ony worked on W9x. That's fixed now. :eek:)
I't going to be freeware without source (too ugly ;-) ) I'll post here if it's available. (Dutch version first)

HTH
TonHu
 
Hi All
I am glad we all got this working...
After spending a little time refining my code, I found that if you declare your paths and passwords as widestrings, then you can say: MyShareNT.shi2_path := PWideChar(Widestring variable); as in TonHu's post.

Have Fun...
QuentinB
 
sorry to bother you guys again. But can your mapping work if the share has a name that is longer than one char. Also now my non-NT share doesn't work could one of you post your code on how you did this.
Thanks, [flush3]
 
For starters, my sharename was 'BomasShare' and the shared dir was 'D:\BOMAS'. After calling the function with the passwords being '' I have a nice share created, either running Windows 98 or Windoes XP.
It does involve some cutting and pasting of the beforementioned code ;-) but it does really work!
Could it be you made a typo, like WideChar instead of WideString? The PWideChar is correct though!
Also be aware that the share_info2 type is *very* different from the share_info50 type used on Win9x.

er, my errortrapping is not very sophisticated, I know s-)

HTH
TonHu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top