C++ - Creating folder method - c++

I have the following method in C++:
In case the folder already exists, the correct message is displayed on the screen. However, if the folder does NOT exist, nothing is displayed on the screen, that is, the part identified by case NULL is not executed. How can I solve this problem?
In other words, how can I get the code after the case NULL to run if the folder does not exist?

First, if the folder does not exist, CreateDirectory() will probably succeed and return a non-zero value, so the if condition will return false and you will never get to the switch statement.
Second, GetLastError() does as advertised: it returns the last error. If CreateDirectory() does not raise an error, it will return whatever was the last error set by any other function. Checking for NULL is not very useful.

The spec say that as long as CreateDirectory succeeds, the return value is nonzero.
CreateDirectory
So why don't you use an else to the if clause to print that
At max you can use a default in your switch to print
"There was some error".
Since the switch only executes in case of an error

Related

GetLastError() Returns 'ERROR_NO_MORE_FILES' (18) after attempting to create a registry key

I have the following code, attempting to allow the program to run at startup:
HKEY key;
if(RegOpenKey(HKEY_CURRENT_USER,LPCWSTR("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),&key) != ERROR_SUCCESS) {
std::cout<<"Unable to open Reg key last error - "<<GetLastError()<<"\n";
system("pause");
}
Omitting the parts that aren't necessary. It prints that there was an error creating the registry key with the error code 18, which according to this page means that I'm encountering an error that returns ERROR_NO_MORE_FILES. Unfortunately the description says the same thing and I don't know what this means in the case of creating a registry key. I have tried running the program as an administrator, the key does not already exist either. Thanks.
If you read the documentation, it states:
Return value
"If the function succeeds, the return value is ERROR_SUCCESS.
If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error."
It does not state to call GetLastError(). Also, this has nothing to do with C++.
Your error checking is wrong. Registry API functions return error codes. They don't use SetLastError. You have to use the error code returned by the function to diagnose errors.
The obvious error in your code is the cast to wide text. That does not change the fact that your string is actually 8 bit text. Use the L prefix.
L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"
For what it is worth, you should use RegOpenKeyEx to open keys rather than RegOpenKey. And for creating keys use RegCreateKeyEx.
I had the same result (GetLastError() returned ERROR_NO_MORE_FILES) when trying to call RegCreateKeyEx() on a registry key where the running process had insufficient rights (in my case "HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\Application"). When starting the process with elevated privileges it succeeded.
So maybe there could be a connection between error code 18 and UAC.

"Invalid expression, assumed zero" error on if-statement

I'm getting the following error whenever I compile my code: "Error 029: Invalid expression, assumed zero"
The error is thrown on the following line:
if ((PlayerInfo[playerid][ADMINLevel])) || (IsPlayerAdmin(playerid))
I want the if-statement to check if "ADMINLevel" is above zero or if the player is logged in as an RCON admin.
You're constructing your if-statement wrong. The correct way to do it is
if(PlayerInfo[playerid][ADMINLevel] > 0 || IsPlayerAdmin(playerid))
{
/* Put your desired script here */
}
Your code was nearly correct (although it did have some unnecessary brackets), you just need to actually add a comparison to the ADMINLevel check. An if-statement should be like a question ("is admin level more than 0", rather than just "is admin level"). You can find more information about if-statements in Pawn here, and I think it will be useful for you to read.
PlayerInfo[..][..] does not return a boolean. Add > 0 to fix it

EnumDisplaySettings determine failure

How to determine winapi EnumDisplaySettings failure? According to MSDN
If the function fails, the return value is zero.
also
Graphics mode indexes start at zero. To obtain information for all of
a display device's graphics modes, make a series of calls to
EnumDisplaySettings, as follows: Set iModeNum to zero for the first
call, and increment iModeNum by one for each subsequent call. Continue
calling the function until the return value is zero.
How to determine if returned zero is a failure sign or mode doesnt exist (iModeNum value too big)?
There's nothing told about GetLastError. It seems like this winapi doesnt set last error on failure.
This is typical for GDI api calls, they don't set the GetLastError error code. All you've got is the "it didn't work" return value.
Do note that you must start with iModeNum at 0. If that returns FALSE then you can safely assume there's something drastically wrong with the device name argument. Keep incrementing iModeNum until you get FALSE.

Using GetLastError when using Dos Commands in C++

So i have a bit of code that uses Dos Commands to try to rename a folder. So
system("rename C:\\Users\\me\\SecondDir NewDir);
So this tries to rename SecondDir to NewDir. There is already a folder at that location called NewDir so it should fail. And it does. Im then using GetLastError to get the error code returned to ensure the problem is what i expect it to be. But it only ever returns ERROR_NO_MORE_FILES. Which isnt the error i should be getting, which is ERROR_ALREADY_EXISTS. Im assuming this is something to do with using the system command?
EDIT: I just checked and i even get ERROR_NO_MORE_FILES returned when a command is successful.
GetLastError will not return a meaningful value except in the circumstances where it is documented to do so. This is not one of them - the values you are getting are irrelevant and intended for someone else.
To rename a file from C you should use the C runtime rename function not use system to invoke a rename utility.
GetLastError is only meaningful immediately after calling a Win32 function which is documented to set the thread Last Error using SetLastError. The C equivalent is errno, which applies to C functions.
The rename function returns -1 on failure and sets errno.
E.g.: http://msdn.microsoft.com/en-us/library/zw5t957f(v=VS.80).aspx

Strange error trying to read registry value with Windows/C++

I'm trying to read the install path for an application, and I'm baffled at the behaviour I'm getting. First, here's the code that didn't work (formatted it a little so it doesn't take up a huge line):
LONG status = RegQueryValueEx(
hkRegistry,
"InstallPath",
0,
&regType, (LPBYTE)installPath,
&regSize );
if (status == ERROR_SUCCESS) {
// Handle success.
}
I realized that it was failing on the call to RegQueryValueEx, so I decided to probe the return value by throwing it within an exception by adding:
else {
throw Exception( status );
}
But then... the code started to work and the call to RegQueryValueEx succeeded. I've been able to repeat this behaviour as long as I throw something within the else. If I comment out the body of the else, the error returns.
Edit: Okay, I tried calling MessageBox instead of an exception and I get the same behaviour. If I comment it out, it stops working again.
Is there a rational explanation for this?
It's possible that the buffer for installPath is too small compared to the value contained in regSize (which must be initialized to the size of the buffer).
If installPath is a stack-allocated value I suspect that it is overflown, causing the value of status to get overwritten.