Reputation: 1
I am useing windows 8.1. I have called an API CreateDirectory in my code. but it return failed. I have also get last error code using GetLastError. my code is here. I get Error code 3. please help me where my error is. Sorry if any point is incomplete/
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
CString sFullPath;
sFullPath.Format("%s\\%d", sRecPath, sysTime.wYear);
BOOL bRes = CreateDirectory(sFullPath, NULL);
int nErrorCode = GetLastError();
Upvotes: 0
Views: 869
Reputation: 613262
First of all, you are checking for errors incorrectly. As stated in the documentation, failure is indicated by the return value. Only if bRes
is false is the value returned by GetLastError
meaningful. So you should write:
if CreateDirectory(...)
DWORD err = GetLastError();
Do note also that GetLastError
returns an unsigned DWORD
and not an int
.
Assuming that CreateDirectory
really does fail and the error code of 3
is accurate, that error code is ERROR_PATH_NOT_FOUND
, described like this:
The system cannot find the path specified.
Most likely that means that some part of the path specified in sFullPath
does not exist. Note that CreateDirectory
will not create a directory tree for you. It will only create a new directory object in an existing directory.
For instance, suppose that you are trying to create a directory like this:
CreateDirectory("C:\\ParentDir1\\ParentDir2\\MyNewDir", NULL)
Now, if C:\ParentDir1\ParentDir2
already exists, then CreateDirectory
will succeed. However, if C:\ParentDir1\ParentDir2
does not exist already, then CreateDirectory
will fail, and GetLastError
will return ERROR_PATH_NOT_FOUND
.
This is even called out explicitly in the documentation which states that the function will fail and the error code will be ERROR_PATH_NOT_FOUND
if:
One or more intermediate directories do not exist; this function will only create the final directory in the path.
You will be able to make progress by inspecting the value of sFullPath
.
I do wonder how closely you read the documentation of this function. My bonus free piece of advice is to make sure that you read documentation carefully. Read it twice if that helps you take it in.
Upvotes: 3