Not-managed code version of Path.ChangeExtension?

  • Thread starter Thread starter x
  • Start date Start date
X

x

Hello,
I am looking for a implementation of the Path.ChangeExtension method for a Win32 Console Project (without .NET). I know I could write my own implementation parsing the filename string, but I wonder if there is anything in the standard C/C++ libraries or the Win32 SDK that would serve the same purpose.

Thanks
x
 
A function to extract the filename portion of a path would also help.
Hello,
I am looking for a implementation of the Path.ChangeExtension method for a Win32 Console Project (without .NET). I know I could write my own implementation parsing the filename string, but I wonder if there is anything in the standard C/C++ libraries or the Win32 SDK that would serve the same purpose.

Thanks
x
 
Hi,

Thanks for posting in the community.

According to William suggested's doc, I think you need something like the
following:
...
#include <stdlib.h>
#include <stdio.h>
...
char* ChangeFileExt(char* filename, char* newExt)
{
if (!filename)
return NULL;

char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
char* new_path = new char[_MAX_PATH];

_splitpath(filename, drive, dir, fname, ext);
_makepath(new_path, drive, dir, fname, newExt);
if(!rename(filename, new_path))
return new_path;
else{
delete[] new_path;
return NULL;
}
}
...

Wish it helps!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Thank you! This is an excellent solution using the standard library. But since I only expect to run my app in Windows, the Shell PathRenameExtension function solves my problem in just one step.

Thanks again.
x

Hi,

Thanks for posting in the community.

According to William suggested's doc, I think you need something like the
following:
..
#include <stdlib.h>
#include <stdio.h>
..
char* ChangeFileExt(char* filename, char* newExt)
{
if (!filename)
return NULL;

char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
char* new_path = new char[_MAX_PATH];

_splitpath(filename, drive, dir, fname, ext);
_makepath(new_path, drive, dir, fname, newExt);
if(!rename(filename, new_path))
return new_path;
else{
delete[] new_path;
return NULL;
}
}
..

Wish it helps!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Back
Top