質問編集履歴

1

\+=解決しました

2016/09/23 11:28

投稿

IchigoTaruto
IchigoTaruto

スコア159

test CHANGED
File without changes
test CHANGED
@@ -167,3 +167,121 @@
167
167
  その理由もお教えいただけると幸いです。
168
168
 
169
169
  よろしくお願いします。
170
+
171
+
172
+
173
+ 解決しました
174
+
175
+ ---
176
+
177
+ ご回答いただいた内容を元に以下の通り修正したところ、うまくいきました。
178
+
179
+ ありがとうございます。
180
+
181
+ ```C
182
+
183
+ #pragma comment(lib, "advapi32.lib")
184
+
185
+
186
+
187
+ #include <stdio.h>
188
+
189
+ #include <conio.h>
190
+
191
+ #include <stdlib.h>
192
+
193
+ #include <windows.h>
194
+
195
+ #include <winbase.h>
196
+
197
+
198
+
199
+ int main(int argc, char **argv)
200
+
201
+ {
202
+
203
+ char *name;
204
+
205
+ HANDLE hdl;
206
+
207
+ int ret;
208
+
209
+
210
+
211
+ if(argc != 2)
212
+
213
+ return;
214
+
215
+
216
+
217
+ name = argv[1];
218
+
219
+ printf("name == '%s'\n", name);
220
+
221
+
222
+
223
+ {
224
+
225
+ SECURITY_DESCRIPTOR sd;
226
+
227
+ SECURITY_ATTRIBUTES secAttribute;
228
+
229
+ InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
230
+
231
+ SetSecurityDescriptorDacl(&sd, TRUE, 0, FALSE);
232
+
233
+ secAttribute.nLength = sizeof(secAttribute);
234
+
235
+ secAttribute.lpSecurityDescriptor = &sd;
236
+
237
+ secAttribute.bInheritHandle = TRUE;
238
+
239
+ hdl = CreateMutexA(&secAttribute, FALSE, name);
240
+
241
+ }
242
+
243
+
244
+
245
+ printf("hdl == %d\n", (int)hdl);
246
+
247
+
248
+
249
+ if(hdl == NULL)
250
+
251
+ return;
252
+
253
+
254
+
255
+ // _getch();
256
+
257
+
258
+
259
+ printf("LOCKING...\n");
260
+
261
+ ret = WaitForSingleObject((HANDLE)hdl, INFINITE);
262
+
263
+
264
+
265
+ if(ret != WAIT_OBJECT_0)
266
+
267
+ return; // fatal
268
+
269
+
270
+
271
+ printf("LOCKED\n");
272
+
273
+
274
+
275
+ _getch();
276
+
277
+
278
+
279
+ ReleaseMutex(hdl);
280
+
281
+ CloseHandle(hdl);
282
+
283
+ printf("RELEASE & CLOSED\n");
284
+
285
+ }
286
+
287
+ ```