unsigned long GetCurrentFileName(__out wchar_t* pPath, __inout size_t nPathLength, __out wchar_t* pFileName, __inout size_t nFileNameLength)
{
unsigned long ulError = ERROR_SUCCESS;
//------------------------------------------------------------------
// [
wchar_t szFilePath[MAX_PATH] = {0, };
wchar_t szDrive[_MAX_DRIVE] = {0, };
wchar_t szDir[_MAX_DIR] = {0, };
wchar_t szFName[_MAX_FNAME] = {0, };
wchar_t szExt[_MAX_EXT] = {0, };
if (pPath == NULL || nPathLength == 0 || pFileName == NULL || nFileNameLength == 0)
{
ulError = ERROR_INVALID_PARAMETER;
DEBUGPRINT(_T("invalid paramter."));
goto $error;
}
if (0 == GetModuleFileName(NULL, szFilePath, _countof(szFilePath)))
{
ulError = GetLastError();
DEBUGPRINT(_T("get module file name error(%d)"), ulError);
goto $error;
}
if (0 != _wsplitpath_s(szFilePath, szDrive, _countof(szDrive), szDir, _countof(szDir),
szFName, _countof(szFName), szExt, _countof(szExt)))
{
ulError = errno;
DEBUGPRINT(_T("split path error(%d)"), ulError);
goto $error;
}
ZeroMemory(szFilePath, _countof(szFilePath));
if (0 != _wmakepath_s(szFilePath, _countof(szFilePath), szDrive, szDir, szFName, szExt))
{
ulError = errno;
DEBUGPRINT(_T("make path error(%d)"), ulError);
goto $error;
}
ZeroMemory(pPath, nPathLength);
if (0 != _wmakepath_s(pPath, nPathLength, szDrive, szDir, NULL, NULL))
{
ulError = errno;
DEBUGPRINT(_T("make path error(%d)"), ulError);
goto $error;
}
nPathLength = wcslen(pPath);
ZeroMemory(pFileName, nFileNameLength);
if (0 != _wmakepath_s(pFileName, nFileNameLength, NULL, NULL, szFName, szExt))
{
ulError = errno;
DEBUGPRINT(_T("make path error(%d)"), ulError);
goto $error;
}
nFileNameLength = wcslen(pFileName);
$error:
// ]
//------------------------------------------------------------------
return ulError;
}