Problems with _mkdir();

closed account (NUCkSL3A)
Hello. I am recently working on a project.

_mkdir("C:\\windows\\system32\\test");
The above line is causing problems, as it doesn't create the folder. I'm running with full admin on a Windows 7 machine. mkdir used to work for some reason but now it doesn't. Any help is greatly appreciated.
Last edited on
Try using wchar_t * or LPCSTR instead of char *.

 
_wmkdir(L"C:\\windows\\system32\\test"); 


Also system() command can bypass rights:
http://www.cplusplus.com/articles/j3wTURfi/

so maybe this would work
1
2
system(Notepad)
_wmkdir(L"C:\\windows\\system32\\test");
Last edited on
closed account (NUCkSL3A)
The problem is it works on other locations, like program files. It only seems to not work on some locations. Again, I'm using an admin account on the debugging computer.
You can try the CreateDirectory function and and use GetLastError in case it doesn't work.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx
Is it possible that your antivirus program is preventing the creation of the directory in that "system level" directory?
closed account (NUCkSL3A)
I don't have an antivirus. Can anyone who has Visual Studio tell me their settings and give me a working example?
This code worked for me. Make sure that you run VS as administrator.

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
#include <windows.h>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string GetLastWinError ()
{
  LPVOID lpMsgBuf = 0;
  LPVOID lpDisplayBuf = 0;
  DWORD dw = GetLastError ();

  FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw,
    MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf,
    0, NULL);

  std::ostringstream oss;
  oss << "Windows Error ";
  oss << dw << " :" << (LPCSTR)lpMsgBuf;

  LocalFree (lpMsgBuf);

  return oss.str();

}

int main()
{
  const char szDirectory[] = "C:\\Windows\\System32\\test";
  cout << "Try creating directory " << szDirectory << "\n";
  if (CreateDirectoryA(szDirectory, 0))
    cout << "Directory created successfully\n";
  else
    cerr << GetLastWinError() << "\n\n";

  system("pause");
  return 0;
}
closed account (NUCkSL3A)
No.. for some reason none of these work. :/ #Murphy'sLaw.
What error msg did you get ?
Topic archived. No new replies allowed.