Bonjour,

Je code avec Delphi 7 Personnel. Le but de mon programme est de partager un lecteur correspondant à un support amovible sur le réseau.

J'ai donc trouver une implémentation de la fonction NetShareAdd de la DLL netapi.dll

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
unit uShare;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, StdCtrls, Dialogs;
 
type
 _SHARE_INFO_2 = record
     shi2_netname: LPWSTR;
     shi2_type: DWORD;
     shi2_remark: LPWSTR;
     shi2_permissions: DWORD;
     shi2_max_uses: DWORD;
     shi2_current_uses: DWORD;
     shi2_path: LPWSTR;
     shi2_passwd: LPWSTR;
  end;
  PSHARE_INFO_2= ^_SHARE_INFO_2;
 
var
  Share: PSHARE_INFO_2;
  ParamErr: lpdword;
  Rep : longint;
 
const
 STYPE_DISKTREE = 0;
 STYPE_PRINTQ = 1;
 STYPE_DEVICE = 2;
 STYPE_IPC = 3;
 STYPE_DFS = 100;
 STYPE_SPECIAL = $80000000;
 
 ACCESS_NONE = 0;
 ACCESS_READ  = 1;
 ACCESS_WRITE = 2;
 ACCESS_CREATE = 4;
 ACCESS_EXEC = 8;
 ACCESS_DELETE = 16;
 ACCESS_ATRIB = 32;
 ACCESS_PERM = 64;
 ACCESS_ALL = (ACCESS_READ+ACCESS_WRITE+ACCESS_CREATE+ACCESS_EXEC+ACCESS_DELETE+ACCESS_ATRIB+ACCESS_PERM);
 
  function StringToPWideChar(const SrcStr: string): PWideChar;
 
  function ajouter_partage(nom, chemin, commentaire: String; permissions, nombre_utilisateurs: Cardinal): integer;
  function supprimer_partage(nom: PWideChar): integer;
 
implementation
 
{
  StringToPWideChar() :
    args 0 : const StrStr (string) : La chaîne de caractères à convertir.
    retourne : la chaîne convertit en PWideChar;
}
function StringToPWideChar(const SrcStr: string): PWideChar;
var
  // Le tampon de sortie de la fonction
  Dest: PWideChar;
begin
  // On réserve la mémoire pour la chaîne (taille d'un WideChar * le nb de caractères de la chaîne.
  GetMem(Dest, Sizeof(WideChar) * Succ(Length(SrcStr)));
 
  // Conversion
  StringToWideChar(SrcStr, Dest, Succ(Length(SrcStr)));
 
  //Retour.
  Result:=Dest;
 
  // Libération de la mémoire.
  FreeMem(Dest);
end;
 
function NetShareAdd(servername: LPWSTR;
    level: DWORD;
    buf: pSHARE_INFO_2;
    parm_err: LPDWORD):LongWord; stdcall; external 'netapi32.dll';
 
function NetShareDel(servername: LPWSTR;
    netname : LPWSTR;
    reserved : DWORD):LongWord; stdcall; external 'netapi32.dll';
 
function ajouter_partage(nom, chemin, commentaire: String; permissions, nombre_utilisateurs: Cardinal): integer;
var
  e: Cardinal;
begin
  New(share);
  with Share^ do
  begin
    shi2_netname := StringToPWideChar(nom);
    shi2_type := STYPE_DISKTREE;
    shi2_remark := StringToPWideChar(commentaire);
    shi2_permissions := permissions;
    shi2_max_uses := nombre_utilisateurs;
    shi2_current_uses := 0;
    shi2_path := StringToPWideChar(chemin);
    shi2_passwd := nil;
  end;
  Result:= NetShareAdd(nil, 2, SHARE, ParamErr);
  Dispose(share);
end;
 
function supprimer_partage(nom: PWideChar): integer;
begin
  Result:= NetShareDel(nil, nom, 0);
end;
 
 
end.
Quand je fais appel à ma fonction ajouter_partage() :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
procedure TfMain.Button1Click(Sender: TObject);
var
  Rep: Integer;
begin
  Rep := ajouter_partage('TESTSHR', 'C:\Temp', '', ACCESS_ALL, 4);
 
  if (Rep <> 0) then
    ShowMessage(Format('Erreur lors de la création du partage : #%d - %s', [Rep, SysErrorMessage(Rep)]));
end;
J'obtiens :

Erreur [...] #123 - La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte
Pourtant le dossier existe bel et bien donc je comprends pas. J'ai essayé avec plein d'autres il ne veut rien savoir.
Il y a peut-être un problème avec la fonction StringToPWideChar que j'utilise dans la fonction ajouter_partage() et que j'ai trouvé sur le net.
Mais comme je comprends pas trop le principe du PWideChar, PWideString, etc ... et bien je galère.

Si vous avez des idées ... merci d'avance pour votre aide.

A bientôt, bonne soirée.