teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

ソースコードをmarkdownで記載

2021/04/12 13:38

投稿

yanna
yanna

スコア13

title CHANGED
File without changes
body CHANGED
@@ -34,6 +34,9 @@
34
34
  https://docs.microsoft.com/ja-jp/dotnet/api/system.accessviolationexception?view=net-5.0
35
35
 
36
36
  ## ソースコード(文字数オーバーのためコメントは一部消しています)
37
+
38
+ ``` ShareFolderManager.cs
39
+
37
40
  using System;
38
41
  using System.Runtime.InteropServices;
39
42
 
@@ -238,4 +241,6 @@
238
241
  }
239
242
  }
240
243
  }
241
- }
244
+ }
245
+
246
+ ```

1

環境とソースコードを追記しました。

2021/04/12 13:38

投稿

yanna
yanna

スコア13

title CHANGED
File without changes
body CHANGED
@@ -5,6 +5,11 @@
5
5
 
6
6
  例外が発生する原因や回避策について何かご存じの人いましたら、お手数ですが教えていただけないでしょうか?
7
7
 
8
+ ## 環境
9
+
10
+ * OS:Windows10
11
+ * フレームワーク:.NET Framework 3.5
12
+
8
13
  ## 試したこと
9
14
 
10
15
  fnShareAddメソッドには以下の引数を設定しているのですが、3つの環境で実行し、どの環境でも同様の例外が発生してしまいます。
@@ -26,4 +31,211 @@
26
31
  https://docs.microsoft.com/en-us/windows/win32/api/lmshare/nf-lmshare-netshareadd
27
32
 
28
33
  また、AccessViolationExceptionについても調べたのですが、アンマネージコードで割り当てられていないメモリにアクセスしているから例外が発生していると解釈したのですが、対処としてどのようにすればよいかわかりませんでした。
29
- https://docs.microsoft.com/ja-jp/dotnet/api/system.accessviolationexception?view=net-5.0
34
+ https://docs.microsoft.com/ja-jp/dotnet/api/system.accessviolationexception?view=net-5.0
35
+
36
+ ## ソースコード(文字数オーバーのためコメントは一部消しています)
37
+ using System;
38
+ using System.Runtime.InteropServices;
39
+
40
+ namespace ShareFolderSample
41
+ {
42
+ public static class ShareFolderManager
43
+ {
44
+ private const short STYPE_DISKETREE = 0;
45
+ private const int NERR_Success = 0;
46
+ /// <summary>
47
+ /// ユーザーには、要求した情報へのアクセス権がありません。
48
+ /// <summary>
49
+ private const int ERROR_ACCESS_DENIED = 5;
50
+ /// <summary>
51
+ /// level パラメータで指定した値が無効です。
52
+ /// <summary>
53
+ private const int ERROR_INVALID_LEVEL = 124;
54
+ /// <summary>
55
+ /// 文字またはファイルシステム名が無効です。
56
+ /// <summary>
57
+ private const int ERROR_INVALID_NAME = 123;
58
+ /// <summary>
59
+ /// 指定されたパラメータは無効です。
60
+ /// <summary>
61
+ private const int ERROR_INVALID_PARAMETER = 87;
62
+ /// <summary>
63
+ /// 指定した共有名は、既にこのサーバーで使われています。
64
+ /// <summary>
65
+ private const int NERR_DuplicateShare = 2118;
66
+ /// <summary>
67
+ /// リダイレクトされたリソースに対して、この操作は無効です。指定されたデバイス名は、
68
+ /// 共有リソースに割り当てられています。
69
+ /// <summary>
70
+ private const int NERR_RedirectedPath = 2117;
71
+ /// <summary>
72
+ /// デバイスまたはディレクトリが存在しません。
73
+ /// <summary>
74
+ private const int NERR_UnknownDevDir = 2116;
75
+ /// <summary>
76
+ /// 利用可能なメモリが不足しています。
77
+ /// <summary>
78
+ private const int ERROR_NOT_ENOUGH_MEMORY = 8;
79
+ /// <summary>
80
+ /// 指定された共有名が存在しません。
81
+ /// <summary>
82
+ private const int NERR_NetNameNotFound = 2310;
83
+
84
+ private struct SHARE_INFO_2
85
+ {
86
+ public int shi2_netname;
87
+ /// <summary>
88
+ /// 共有タイプ
89
+ /// </summary>
90
+ public int shi2_type;
91
+ /// <summary>
92
+ /// コメント
93
+ /// </summary>
94
+ public int shi2_remark;
95
+ /// <summary>
96
+ /// パーミッション
97
+ /// </summary>
98
+ public int shi2_permissions;
99
+ /// <summary>
100
+ /// 最大接続数
101
+ /// </summary>
102
+ public int shi2_max_uses;
103
+ /// <summary>
104
+ /// ?
105
+ /// </summary>
106
+ public int shi2_current_uses;
107
+ /// <summary>
108
+ /// 共有フォルダの絶対パス
109
+ /// </summary>
110
+ public int shi2_path;
111
+ /// <summary>
112
+ /// パスワード
113
+ /// </summary>
114
+ public int shi2_passwd;
115
+
116
+ private SHARE_INFO_2(
117
+ int shi2_netname, int shi2_type, int shi2_remark, int shi2_permissions,
118
+ int shi2_max_uses, int shi2_current_uses, int shi2_path, int shi2_passwd)
119
+ {
120
+ // VisualStudio2008でビルド警告を防ぐためだけに設定している
121
+ // この初期化処理は全く意味がない
122
+ this.shi2_netname = 0; // 文字列:null
123
+ this.shi2_type = 0; // 数値:0
124
+ this.shi2_remark = 0; // 文字列:null
125
+ this.shi2_permissions = 0; // 数値:0
126
+ this.shi2_max_uses = 0; // 数値:0
127
+ this.shi2_current_uses = 0; // 数値:0
128
+ this.shi2_path = 0; // 文字列:null
129
+ this.shi2_passwd = 0; // 文字列:null
130
+ }
131
+ }
132
+ [DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
133
+ private static extern int NetShareAdd(
134
+ string servername, int level, ref SHARE_INFO_2 buf, ref int parm_err);
135
+
136
+ [DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
137
+ private static extern int NetShareDel(string servername, string netname, int reserved);
138
+
139
+ public static void fnShareAdd(
140
+ string sServerName, string sShareName, int iMaxUses, string sComment,
141
+ string sPhysicsFolderPath, string sPassword)
142
+ {
143
+ SHARE_INFO_2 si2;
144
+ //-------------------------------------------------------------------------------------
145
+ // 共有フォルダの設定
146
+ //-------------------------------------------------------------------------------------
147
+ // 共有名
148
+ GCHandle gchNetName = GCHandle.Alloc(sShareName, GCHandleType.Pinned);
149
+ si2.shi2_netname = gchNetName.AddrOfPinnedObject().ToInt32();
150
+ // 共有タイプ
151
+ si2.shi2_type = STYPE_DISKETREE;
152
+ // コメント
153
+ GCHandle gchNetRemark = GCHandle.Alloc(sComment, GCHandleType.Pinned);
154
+ si2.shi2_remark = gchNetRemark.AddrOfPinnedObject().ToInt32();
155
+ // パーミッション
156
+ si2.shi2_permissions = 0;
157
+ // 最大接続数
158
+ si2.shi2_max_uses = iMaxUses;
159
+ // ?
160
+ si2.shi2_current_uses = 0;
161
+ // 共有フォルダパス
162
+ GCHandle gchPath = GCHandle.Alloc(sPhysicsFolderPath, GCHandleType.Pinned);
163
+ si2.shi2_path = gchPath.AddrOfPinnedObject().ToInt32();
164
+ // パスワード
165
+ GCHandle gchPassword = GCHandle.Alloc(sPassword, GCHandleType.Pinned);
166
+ si2.shi2_passwd = gchPassword.AddrOfPinnedObject().ToInt32();
167
+ //-------------------------------------------------------------------------------------
168
+ // 共有フォルダ作成
169
+ //-------------------------------------------------------------------------------------
170
+ int iParamErr = 0;
171
+ // 第二引数はSHARE_INFO_2構造体を使うことを示している(らしい)
172
+ int iResult = NetShareAdd(sServerName, 2, ref si2, ref iParamErr);
173
+ //-------------------------------------------------------------------------------------
174
+ // GCHandle解放
175
+ //-------------------------------------------------------------------------------------
176
+ gchNetName.Free();
177
+ gchNetRemark.Free();
178
+ gchPath.Free();
179
+ gchPassword.Free();
180
+ //-------------------------------------------------------------------------------------
181
+ // 戻り値判定
182
+ //-------------------------------------------------------------------------------------
183
+ switch (iResult)
184
+ {
185
+ case NERR_Success:
186
+ return;
187
+ case ERROR_ACCESS_DENIED:
188
+ throw new SystemException(
189
+ "ユーザーには、要求した情報へのアクセス権がありません。");
190
+ case ERROR_INVALID_LEVEL:
191
+ throw new SystemException("level パラメータで指定した値が無効です。");
192
+ case ERROR_INVALID_NAME:
193
+ throw new SystemException("文字またはファイルシステム名が無効です。");
194
+ case ERROR_INVALID_PARAMETER:
195
+ throw new SystemException("指定されたパラメータは無効です。");
196
+ case NERR_DuplicateShare:
197
+ throw new SystemException(
198
+ "指定した共有名は、既にこのサーバーで使われています。");
199
+ case NERR_RedirectedPath:
200
+ throw new SystemException(
201
+ "リダイレクトされたリソースに対して、この操作は無効です。" +
202
+ "指定されたデバイス名は、共有リソースに割り当てられています。");
203
+ case NERR_UnknownDevDir:
204
+ throw new SystemException("デバイスまたはディレクトリが存在しません。 ");
205
+ default:
206
+ string sMsg = string.Format(
207
+ "不明なエラー。NetShareAdd戻り値:{0}、iParamErr:{1}",
208
+ iResult, iParamErr);
209
+ throw new SystemException(sMsg);
210
+ }
211
+ }
212
+ public static void fnShareDel(string sServerName, string sShareName)
213
+ {
214
+ //-------------------------------------------------------------------------------------
215
+ // 共有フォルダ削除
216
+ //-------------------------------------------------------------------------------------
217
+ int iResult = NetShareDel(sServerName, sShareName, 0);
218
+ //-------------------------------------------------------------------------------------
219
+ // 戻り値判定
220
+ //-------------------------------------------------------------------------------------
221
+ switch (iResult)
222
+ {
223
+ case NERR_Success:
224
+ return;
225
+ case ERROR_ACCESS_DENIED:
226
+ throw new SystemException(
227
+ "ユーザーには、要求した情報へのアクセス権がありません。");
228
+ case ERROR_INVALID_PARAMETER:
229
+ throw new SystemException("指定されたパラメータは無効です。");
230
+ case ERROR_NOT_ENOUGH_MEMORY:
231
+ throw new SystemException("利用可能なメモリが不足しています。");
232
+ case NERR_NetNameNotFound:
233
+ throw new SystemException("指定された共有名が存在しません。");
234
+ default:
235
+ string sMsg = string.Format(
236
+ "不明なエラー。NetShareDel戻り値:{0}", iResult);
237
+ throw new SystemException(sMsg);
238
+ }
239
+ }
240
+ }
241
+ }