C Registry Functions In Windows API - c

RegOpenKeyEx()
I want to printf("Success") or printf("Failure") depending on if the function fails or succeeds
How would I do such a conditional while keeping it neat and legible?
I am wanting to stay away from this :
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,"HUGE LONG KYEY STRUCTURE HERE",0,KEY_SET_VALUE) != 0 )
{
//CODE
}

Are you asking how to check why it failed?
"A nonzero error code defined in Winerror.h indicates failure. To get a generic description of the error, call FormatMessage with the FORMAT_MESSAGE_FROM_SYSTEM flag set."
So.. ERROR_SUCCESS if succeeded, error code if it fails.

After checking the unedited question, I think the only thing you can do is to break up the statement into multiple statements like this:
const char* regKey = "BIG_STRING......";
DWORD errorCode = RegOpenKeyEx(...);
if(ERROR_SUCCESS == errorCode)
{
//Rest of the code
}
else
{
//Error handling
}

You can use newlines, or name things or both,
LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"HUGE LONG KYEY STRUCTURE HERE",
0,KEY_SET_VALUE);
if(result != 0 ) {
...
or perhaps
LPCTSTR keypath = "HUGE LONG KYEY STRUCTURE HERE";
if( RegOpenKeyEx(HKEY_LOCAL_MACHINE,keypath,0,KEY_SET_VALUE) != 0 ) {
...

You could instead use the ternary operator:
bool success = (RegKeyOpenEx(...) == ERROR_SUCCESS);
printf("%s\n", success ? "Success" : "Failure");

It does not return ERROR_SUCCESS when it does not succeed. Is that what you mean?
for multiple checks of error codes use:
LONG errors = 0;
errors += abs( RegKeyOpenEx(HKEY_LOCAL_MACHINE,"HUGE LONG KYEY STRUCTURE HERE",0,KEY_SET_VALUE) );
errors += abs( RegKeyOpenEx(HKEY_LOCAL_MACHINE,"HUGE LONG KYEY STRUCTURE HERE",0,KEY_SET_VALUE) );
errors += abs( RegKeyOpenEx(HKEY_LOCAL_MACHINE,"HUGE LONG KYEY STRUCTURE HERE",0,KEY_SET_VALUE) );
errors += abs( RegKeyOpenEx(HKEY_LOCAL_MACHINE,"HUGE LONG KYEY STRUCTURE HERE",0,KEY_SET_VALUE) );
errors += abs( RegKeyOpenEx(HKEY_LOCAL_MACHINE,"HUGE LONG KYEY STRUCTURE HERE",0,KEY_SET_VALUE) );
if( errors > 0 )
{
print( "OMG It went wrong!\n" );
}
else
{
print( "Hero!\n" );
}

Related

How to get parent Directory in C programming? [duplicate]

Question
How do you extract the base path from pathname in C?
Are there any functions built into the C language or the C-Runtime Library to extract the base path from a pathname in C?
I'm asking basically the opposite of this question.
NOTE: I prefer a cross-platform solution, but I'm working in Windows so if there is a Windows API call that does this, I'd still like to know.
Examples
Input | Output
---------------------------------
C:\path\to\file -> C:\path\to\
C:\path\to\file.c -> C:\path\to\
C:\file -> C:\
.\file -> .\
.\ -> .\
\ -> \
References
Extract file name from full path in C using MSVS2005
On Windows there is _splitpath.
Example
#include <Windows.h>
#include <tchar.h>
// Use your own error codes here
#define SUCCESS 0L
#define FAILURE_NULL_ARGUMENT 1L
#define FAILURE_API_CALL 2L
#define FAILURE_INSUFFICIENT_BUFFER 3L
DWORD GetBasePathFromPathName( LPCTSTR szPathName,
LPTSTR szBasePath,
DWORD dwBasePathSize )
{
TCHAR szDrive[_MAX_DRIVE] = { 0 };
TCHAR szDir[_MAX_DIR] = { 0 };
TCHAR szFname[_MAX_FNAME] = { 0 };
TCHAR szExt[_MAX_EXT] = { 0 };
size_t PathLength;
DWORD dwReturnCode;
// Parameter validation
if( szPathName == NULL || szBasePath == NULL )
{
return FAILURE_NULL_ARGUMENT;
}
// Split the path into it's components
dwReturnCode = _tsplitpath_s( szPathName, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, szFname, _MAX_FNAME, szExt, _MAX_EXT );
if( dwReturnCode != 0 )
{
_ftprintf( stderr, TEXT("Error splitting path. _tsplitpath_s returned %d.\n"), dwReturnCode );
return FAILURE_API_CALL;
}
// Check that the provided buffer is large enough to store the results and a terminal null character
PathLength = _tcslen( szDrive ) + _tcslen( szDir );
if( ( PathLength + sizeof( TCHAR ) ) > dwBasePathSize )
{
_ftprintf( stderr, TEXT("Insufficient buffer. Required %d. Provided: %d\n"), PathLength, dwBasePathSize );
return FAILURE_INSUFFICIENT_BUFFER;
}
// Copy the szDrive and szDir into the provide buffer to form the basepath
if( ( dwReturnCode = _tcscpy_s( szBasePath, dwBasePathSize, szDrive ) ) != 0 )
{
_ftprintf( stderr, TEXT("Error copying string. _tcscpy_s returned %d\n"), dwReturnCode );
return FAILURE_API_CALL;
}
if( ( dwReturnCode = _tcscat_s( szBasePath, dwBasePathSize, szDir ) ) != 0 )
{
_ftprintf( stderr, TEXT("Error copying string. _tcscat_s returned %d\n"), dwReturnCode );
return FAILURE_API_CALL;
}
return SUCCESS;
}
Are there any functions built into the C language or C-Runtime to extract the base path from a pathname in C?
No there are not. Rules for path names are platform specific and so the standard does not cover them.
In Windows you can use the API call "PathRemoveFileSpec" http://msdn.microsoft.com/en-us/library/bb773748(v=vs.85).aspx
Cross platform solutions will not really be possible due to variations in file systems bewtween different OS's.
Just loop from back to forward until you meet the first \
WinAPI (shlwapi) PathRemoveFileSpec should do all of that with the exception of .\file which would come back as .
There is no standard C99 function for doing this. POSIX has dirname(), but that won't help you much on Windows. It shouldn't be too hard for you to implement your own function, though; just search through the string, looking for the last occurrence of the directory separator, and discard anything after it.
I think the best solution on Windows is to use _splitpath as was suggested, to use something like basename on Linux (more on that here).
That said, since someone has already suggested implementing my own (and since I had already done it while I was waiting for an answer), here is what I came up with. It is not cross-platform and it does not check for /valid/ paths or expand short or relative path names.
// Retrieves the pathpath from a pathname.
//
// Returns: SUCCESS if the basepath is present and successfully copied to the p_base_path buffer
// FAILURE_NULL_ARGUMENT if any arguments are NULL
// FAILURE_INVALID_ARGUMENTS if either buffer size is less than 1
// FAILURE_BUFFER_TOO_SMALL if the p_basepath buffer is too small
// FAILURE_INVALID_PATH if the p_pathname doesn't have a path (e.g. C:, calc.exe, ?qwa)
// FAILURE_API_CALL if there is an error from the underlying API calls
int get_base_path_from_pathname( const char* const p_pathname,
size_t pathname_size,
char* const p_basepath,
size_t basepath_size );
int get_base_path_from_pathname( const char* const p_pathname,
size_t pathname_size,
char* const p_basepath,
size_t basepath_size )
{
char* p_end_of_path;
size_t path_length;
int return_code;
// Parameter Validation
if( p_pathname == NULL || p_basepath == NULL ) { return FAILURE_NULL_ARGUMENT; }
if( pathname_size < 1 || basepath_size < 1 ) { return FAILURE_INVALID_ARGUMENTS; }
// Returns a pointer to the last occurrence of \ in p_pathname or NULL if it is not found
p_end_of_path = strrchr( p_pathname, '\\' );
if( p_end_of_path == NULL )
{
// There is no path part
return FAILURE_INVALID_PATH;
}
else
{
path_length = (size_t)( p_end_of_path - p_pathname + 1 );
// Do some sanity checks on the length
if( path_length < 1 ) { return FAILURE_INVALID_PATH; }
if( ( path_length + 1 ) > basepath_size ) { return FAILURE_BUFFER_TOO_SMALL; }
// Copy the base path into the out variable
if( strncpy( p_basepath, p_pathname, path_length ) != 0 ) { return FAILURE_API_CALL; }
p_basepath[path_length] = '\0';
}
return SUCCESS;
}
Before str is the full path and file name, after str is just the path:
char dir_ch = '\\'; // set dir_ch according to platform
char str[] = "C:\\path\\to\\file.c";
char *pch = &str[strlen(str)-1];
while(*pch != dir_ch) pch--;
pch++;
*pch = '\0';

Return error code after first detected error

I have a function which does some initialization and calls other functions, each of which returns an error code. I want to be able to return from this function after the first detected error like this:
int error_code = FirstFunction();
if (error_code != 0) {
return error_code;
}
error_code = SecondFunction();
if (error_code != 0) {
return error_code;
}
// etc...
However, not only does this look rather cumbersome, it also has multiple return statements, and for compliance reasons at my company this is not allowed.
How can I rearrange this so that there is only one return statement, but still stop after the first error code? The only way I can think of is to do nested if statements:
int error_code = FirstFunction();
if (error_code == 0) {
error_code = SecondFunction();
if (error_code == 0) {
error_code = ThirdFunction();
// etc...
}
}
return error_code;
But this could get unreasonable pretty fast. Is there another way to do this?
EDIT: In my program, return code of 0 means success (OK) and non-zero means failure/error (NOT OK)
You don't have to nest all the function calls, the code below do the job as well and should comply with your code writing rules:
error_code = FirstFunction();
if (error_code == 0) {
error_code = SecondFunction();
}
if (error_code == 0) {
error_code = ThirdFunction();
}
// etc...
return error_code;
Here is another lean method that can return different error codes depending on which function fails:
int func(void)
{
int code;
int error_code = (code = FirstFunction()) ? code :
(code = SecondFunction()) ? code :
(code = ThirdFunction()) ? code : 0;
/* ... */
return error_code;
}
Lean and clean (like this one, but avoiding the disliked gotos):
int foo(void)
{
int error_code;
do {
if (0 != (error_code = FirstFunction()))
{
break;
}
if (0 != (error_code = SecondFunction()))
{
break;
}
...
} while (0);
return error_code;
}
This, BTW, follows the more common pattern: 0 is OK, everything else isn't. Adjust as needed)
You could even obfuscate this using a macro:
#define RUN_AND_BREAK_ON_ERROR(rc, f, ...) \
if (0 != (rc = f(__VA_ARGS__))) \
{ \
break; \
}
int foo(void)
{
int error_code;
do {
RUN_AND_BREAK_ON_ERROR(error_code, FirstFunction, <args go here>);
RUN_AND_BREAK_ON_ERROR(error_code, SecondFunction, <args go here>);
...
} while (0);
return error_code;
}
if( (error_code = FirstFunction()) || (error_code = SecondFunction()) || ... ){
return error_code ;
}
return error_code; //denoting no error.
This would return only the first function which returns nonzero. The idea is that for if statement the first function that returns nonzero would short-circuit the whole evaluation and returns the error_code from the function which returned non-zero error_code. Also another thing is value of an assignment statement is the value assigned. That's why this works.
A more easier way would be to sequential if-else
if( error_code = FirstFunction() ) {}
else if( error_code = SecondFunction() ) {}
...
return error_code;
If all these functions take the same type of parameters and have the same return type, you could put them in a function array and iterate over it. When an error is found, it simply breaks out of the loop and returns.
int (*function_array[max_array])();
/*Fill the array with the functions you need*/
for(i=0;i<max_array;i++){
if((error_code=function_array[i]())!=OK){
break;
}
}
return error_code;
(OK is whatever the success return value is for these functions)
Well, there's the one used e.g. in the Linux kernel:
int somefunc(whatever)
{
if (do_something()) {
ret = -EINVAL;
goto err;
}
if (do_something_else()) {
ret = -EPERM;
goto err;
}
/* ... */
ret = 0;
err:
some_mandatory_cleanup();
return ret;
}
But I suspect that's going to be even less well received. (Before you scream, the whole point of that is the mandatory cleanup in the end. The goto arranges it to be executed always, but still puts it out of way.)
Really, I think the code in your first snippet is fine, and the issue is with your guidelines. Even if we only write return error_code; in one place, it's not enough to guarantee that the error code saved in variable is always correct, or that the function completes all cleanup that might be required. (Consider something that allocates memory, and has to release it in any case.)

Delete files and folders by date using SHFileOperation

I'm trying to write a program that will delete a set of files/folders that are matching a specific naming pattern (wild cards) based on their dates using the windows API
...
SHFILEOPSTRUCT shFileOpStruct = {
.hwnd = NULL,
.wFunc = processByDate->op,
.pTo = NULL,
.fFlags = FOF_NOCONFIRMATION | FOF_SILENT
};
buildReferenceDate( &refTime, processByDate->nDays );
hFind = FindFirstFile( processByDate->srcFileName, &findFileData );
errorCode = GetLastError();
while ( errorCode == ERROR_SUCCESS ) {
LONG res = CompareFileTime( &refTime, &findFileData.ftCreationTime );
if ( (processByDate->nDays ^ res) > 0 ) {
sprintf( strrchr(processByDate->srcFileName, '\\') + 1, "%s%c",
findFileData.cFileName, '\0');
shFileOpStruct.pFrom = processByDate->srcFileName;
fprintf( stdout, "\n%s\n", shFileOpStruct.pFrom);
fprintf( stdout, "\n0x%x\n", SHFileOperation( &shFileOpStruct ));
}
FindNextFile( hFind, &findFileData );
errorCode = GetLastError();
}
if ( errorCode != ERROR_NO_MORE_FILES )
displayError ( stdout, errorCode );
...
Only the first matching file is deleted, because FindNextFile terminates with "The handle is invalid." apparently SHFileOperation somehow invalidates the file handle (or at least so I suppose). The only solution I can think of is to save the name of the matching files/folders and delete them one by one. Is there any other simpler solution?
Thanks
FindNextFile( hFind, &findFileData );
errorCode = GetLastError();
That's wrong. Only call GetLastError() when you get a FALSE return from FindNextFile(). Fix:
if (!FindNextFile( hFind, &findFileData ) {
errorCode = GetLastError();
}
The thread's last error code is not set when a function succeeds. Instead of calling GetLastError, you have to check the return value of FindNextFile.
If the function succeeds, the return value is nonzero and the lpFindFileData parameter contains information about the next file or directory found.
If the function fails, the return value is zero and the contents of lpFindFileData are indeterminate.
FindNextFile function
The loop should look like this:
HANDLE handle(FindFirstFile(...));
if (handle != INVALID_HANDLE_VALUE)
{
do
{
// filter files here
}
while (FindNextFile(handle, ...));
FindClose(handle);
}

Extract base path from a pathname in C

Question
How do you extract the base path from pathname in C?
Are there any functions built into the C language or the C-Runtime Library to extract the base path from a pathname in C?
I'm asking basically the opposite of this question.
NOTE: I prefer a cross-platform solution, but I'm working in Windows so if there is a Windows API call that does this, I'd still like to know.
Examples
Input | Output
---------------------------------
C:\path\to\file -> C:\path\to\
C:\path\to\file.c -> C:\path\to\
C:\file -> C:\
.\file -> .\
.\ -> .\
\ -> \
References
Extract file name from full path in C using MSVS2005
On Windows there is _splitpath.
Example
#include <Windows.h>
#include <tchar.h>
// Use your own error codes here
#define SUCCESS 0L
#define FAILURE_NULL_ARGUMENT 1L
#define FAILURE_API_CALL 2L
#define FAILURE_INSUFFICIENT_BUFFER 3L
DWORD GetBasePathFromPathName( LPCTSTR szPathName,
LPTSTR szBasePath,
DWORD dwBasePathSize )
{
TCHAR szDrive[_MAX_DRIVE] = { 0 };
TCHAR szDir[_MAX_DIR] = { 0 };
TCHAR szFname[_MAX_FNAME] = { 0 };
TCHAR szExt[_MAX_EXT] = { 0 };
size_t PathLength;
DWORD dwReturnCode;
// Parameter validation
if( szPathName == NULL || szBasePath == NULL )
{
return FAILURE_NULL_ARGUMENT;
}
// Split the path into it's components
dwReturnCode = _tsplitpath_s( szPathName, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, szFname, _MAX_FNAME, szExt, _MAX_EXT );
if( dwReturnCode != 0 )
{
_ftprintf( stderr, TEXT("Error splitting path. _tsplitpath_s returned %d.\n"), dwReturnCode );
return FAILURE_API_CALL;
}
// Check that the provided buffer is large enough to store the results and a terminal null character
PathLength = _tcslen( szDrive ) + _tcslen( szDir );
if( ( PathLength + sizeof( TCHAR ) ) > dwBasePathSize )
{
_ftprintf( stderr, TEXT("Insufficient buffer. Required %d. Provided: %d\n"), PathLength, dwBasePathSize );
return FAILURE_INSUFFICIENT_BUFFER;
}
// Copy the szDrive and szDir into the provide buffer to form the basepath
if( ( dwReturnCode = _tcscpy_s( szBasePath, dwBasePathSize, szDrive ) ) != 0 )
{
_ftprintf( stderr, TEXT("Error copying string. _tcscpy_s returned %d\n"), dwReturnCode );
return FAILURE_API_CALL;
}
if( ( dwReturnCode = _tcscat_s( szBasePath, dwBasePathSize, szDir ) ) != 0 )
{
_ftprintf( stderr, TEXT("Error copying string. _tcscat_s returned %d\n"), dwReturnCode );
return FAILURE_API_CALL;
}
return SUCCESS;
}
Are there any functions built into the C language or C-Runtime to extract the base path from a pathname in C?
No there are not. Rules for path names are platform specific and so the standard does not cover them.
In Windows you can use the API call "PathRemoveFileSpec" http://msdn.microsoft.com/en-us/library/bb773748(v=vs.85).aspx
Cross platform solutions will not really be possible due to variations in file systems bewtween different OS's.
Just loop from back to forward until you meet the first \
WinAPI (shlwapi) PathRemoveFileSpec should do all of that with the exception of .\file which would come back as .
There is no standard C99 function for doing this. POSIX has dirname(), but that won't help you much on Windows. It shouldn't be too hard for you to implement your own function, though; just search through the string, looking for the last occurrence of the directory separator, and discard anything after it.
I think the best solution on Windows is to use _splitpath as was suggested, to use something like basename on Linux (more on that here).
That said, since someone has already suggested implementing my own (and since I had already done it while I was waiting for an answer), here is what I came up with. It is not cross-platform and it does not check for /valid/ paths or expand short or relative path names.
// Retrieves the pathpath from a pathname.
//
// Returns: SUCCESS if the basepath is present and successfully copied to the p_base_path buffer
// FAILURE_NULL_ARGUMENT if any arguments are NULL
// FAILURE_INVALID_ARGUMENTS if either buffer size is less than 1
// FAILURE_BUFFER_TOO_SMALL if the p_basepath buffer is too small
// FAILURE_INVALID_PATH if the p_pathname doesn't have a path (e.g. C:, calc.exe, ?qwa)
// FAILURE_API_CALL if there is an error from the underlying API calls
int get_base_path_from_pathname( const char* const p_pathname,
size_t pathname_size,
char* const p_basepath,
size_t basepath_size );
int get_base_path_from_pathname( const char* const p_pathname,
size_t pathname_size,
char* const p_basepath,
size_t basepath_size )
{
char* p_end_of_path;
size_t path_length;
int return_code;
// Parameter Validation
if( p_pathname == NULL || p_basepath == NULL ) { return FAILURE_NULL_ARGUMENT; }
if( pathname_size < 1 || basepath_size < 1 ) { return FAILURE_INVALID_ARGUMENTS; }
// Returns a pointer to the last occurrence of \ in p_pathname or NULL if it is not found
p_end_of_path = strrchr( p_pathname, '\\' );
if( p_end_of_path == NULL )
{
// There is no path part
return FAILURE_INVALID_PATH;
}
else
{
path_length = (size_t)( p_end_of_path - p_pathname + 1 );
// Do some sanity checks on the length
if( path_length < 1 ) { return FAILURE_INVALID_PATH; }
if( ( path_length + 1 ) > basepath_size ) { return FAILURE_BUFFER_TOO_SMALL; }
// Copy the base path into the out variable
if( strncpy( p_basepath, p_pathname, path_length ) != 0 ) { return FAILURE_API_CALL; }
p_basepath[path_length] = '\0';
}
return SUCCESS;
}
Before str is the full path and file name, after str is just the path:
char dir_ch = '\\'; // set dir_ch according to platform
char str[] = "C:\\path\\to\\file.c";
char *pch = &str[strlen(str)-1];
while(*pch != dir_ch) pch--;
pch++;
*pch = '\0';

what is return error value of mysql_query in C?

folks,
from the following code
int a = mysql_query(conn,"INSERT into data VALUES (NULL,'tes','aja'));
how come i could make sure mysql_query is do the supposed thing, because i've tried wrong sql query and it becomes same return value
from mysql_query function which from file mysql.h:
int STDCALL mysql_query(MYSQL *mysql, const char *q);
is there a way to check that string of query is valid and queried properly ?
According to the MySQL reference documentation for mysql_query this should return zero on success and non-zero on failure. Are you sure that it is returning 0 value for an invalid query?
I was surprised that the MySQL database I had to use did not return error on "wrong query". (The MySQL database is not in my administration) You do not write what exact problem do you have with you query; what is the problem. I suppose my problem can be similar to your problem.
(Example table is simplified.)
My table 'T' with cols 'A' varchar[10] not null, 'B' varchar[10]
int a = mysql_query(conn,"INSERT INTO T (B) VALUE ('data')");
The problem is that the result is OK, error message is empty and only one warning is present.
Solution is to test not only error but also warnings.
if ( mysql_errno( conn ) || mysql_warning_count( conn ) )
result = 1; //error
else
result = 0; //OK
If you need to display warnings/errors there is my simple sample code:
static void display_warnings( MYSQL *conn )
{
MYSQL_RES *sql_result;
MYSQL_ROW row;
if ( ( conn == NULL ) || mysql_query( conn, "SHOW WARNINGS" ) )
{
printf( "Can not display list of errors/warnings!\n" );
return;
}
sql_result = mysql_store_result( conn );
if ( ( sql_result == NULL ) || ( sql_result->row_count == 0 ) )
{
printf( "Can not display list of errors/warnings!\n" );
if ( sql_result )
mysql_free_result( sql_result );
return;
}
row = mysql_fetch_row( sql_result );
if ( row == NULL )
{
printf( "Can not display list of errors/warnings!\n" );
mysql_free_result( sql_result );
return;
}
do
{
// Format: "message [Type: number]"
printf( "%s [%s: %s]\n", row[2], row[0], row[1] );
row = mysql_fetch_row( sql_result );
} while ( row );
mysql_free_result( sql_result );
}
I need to handle that all given data must be correct and none is missing. Missing data (according to table definition) is unacceptable; trimming of data ('ABC12345678' changed to 'ABC1234567') is also unacceptable. Both these problems are handled only as warning. Therefore I handle all warnings as errors.

Resources