Skip to content

Commit e2b9154

Browse files
committed
initial commit
0 parents  commit e2b9154

12 files changed

+1002
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bin/
2+
*.opensdf
3+
*.sdf
4+
*.suo
5+
ipch/
6+
Win32/
7+
x64/

DeviceNameResolver.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "DeviceNameResolver.h"
2+
#include "DeviceNameResolverInternal.h"
3+
#include "DynBuf.h"
4+
5+
extern "C" __declspec(dllexport) bool DevicePathToPathW(const wchar_t* szDevicePath, wchar_t* szPath, size_t nSize)
6+
{
7+
DeviceNameResolver deviceNameResolver;
8+
wchar_t targetPath[MAX_PATH]=L"";
9+
if(!deviceNameResolver.resolveDeviceLongNameToShort(szDevicePath, targetPath))
10+
return false;
11+
wcscpy_s(szPath, nSize/sizeof(wchar_t), targetPath);
12+
return true;
13+
}
14+
15+
extern "C" __declspec(dllexport) bool DevicePathToPathA(const char* szDevicePath, char* szPath, size_t nSize)
16+
{
17+
size_t len = strlen(szDevicePath);
18+
DynBuf newDevicePathBuf((len+1)*sizeof(wchar_t));
19+
wchar_t* newDevicePath = (wchar_t*)newDevicePathBuf.GetPtr();
20+
*newDevicePath=L'\0';
21+
if(MultiByteToWideChar(CP_ACP, NULL, szDevicePath, -1, newDevicePath, (int)len+1))
22+
{
23+
DynBuf newPathBuf(nSize*sizeof(wchar_t));
24+
wchar_t* newPath = (wchar_t*)newPathBuf.GetPtr();
25+
if(!DevicePathToPathW(newDevicePath, newPath, nSize*sizeof(wchar_t)))
26+
return false;
27+
if(!WideCharToMultiByte(CP_ACP, NULL, newPath, -1, szPath, (int)wcslen(newPath) + 1, NULL, NULL))
28+
return false;
29+
}
30+
return true;
31+
}

DeviceNameResolver.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _DEVICENAMERESOLVER_H
2+
#define _DEVICENAMERESOLVER_H
3+
4+
#ifdef __cplusplus
5+
extern "C"
6+
{
7+
#endif
8+
9+
__declspec(dllexport) bool DevicePathToPathW(const wchar_t* szDevicePath, wchar_t* szPath, size_t nSize);
10+
__declspec(dllexport) bool DevicePathToPathA(const char* szDevicePath, char* szPath, size_t nSize);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif // _DEVICENAMERESOLVER_H

DeviceNameResolver.sln

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DeviceNameResolver", "DeviceNameResolver.vcxproj", "{3A22175E-6B72-FDCC-1603-C4A2163C7900}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Release|Win32 = Release|Win32
9+
Release|x64 = Release|x64
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{3A22175E-6B72-FDCC-1603-C4A2163C7900}.Release|Win32.ActiveCfg = Release|Win32
13+
{3A22175E-6B72-FDCC-1603-C4A2163C7900}.Release|Win32.Build.0 = Release|Win32
14+
{3A22175E-6B72-FDCC-1603-C4A2163C7900}.Release|x64.ActiveCfg = Release|x64
15+
{3A22175E-6B72-FDCC-1603-C4A2163C7900}.Release|x64.Build.0 = Release|x64
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

DeviceNameResolver.vcxproj

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Release|Win32">
5+
<Configuration>Release</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<ItemGroup>
14+
<ClCompile Include="DeviceNameResolverInternal.cpp" />
15+
<ClCompile Include="DeviceNameResolver.cpp" />
16+
<ClCompile Include="NativeWinApi.cpp" />
17+
</ItemGroup>
18+
<ItemGroup>
19+
<ClInclude Include="DeviceNameResolverInternal.h" />
20+
<ClInclude Include="DynBuf.h" />
21+
<ClInclude Include="DeviceNameResolver.h" />
22+
<ClInclude Include="NativeWinApi.h" />
23+
</ItemGroup>
24+
<PropertyGroup Label="Globals">
25+
<ProjectGuid>{3A22175E-6B72-FDCC-1603-C4A2163C7900}</ProjectGuid>
26+
<Keyword>Win32Proj</Keyword>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
30+
<ConfigurationType>DynamicLibrary</ConfigurationType>
31+
<UseDebugLibraries>false</UseDebugLibraries>
32+
<CharacterSet>Unicode</CharacterSet>
33+
</PropertyGroup>
34+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
35+
<ConfigurationType>DynamicLibrary</ConfigurationType>
36+
<UseDebugLibraries>false</UseDebugLibraries>
37+
<CharacterSet>Unicode</CharacterSet>
38+
</PropertyGroup>
39+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
40+
<ImportGroup Label="ExtensionSettings">
41+
</ImportGroup>
42+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
43+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
44+
</ImportGroup>
45+
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
46+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
47+
</ImportGroup>
48+
<PropertyGroup Label="UserMacros" />
49+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
50+
<LinkIncremental>false</LinkIncremental>
51+
<OutDir>$(SolutionDir)bin\x32\</OutDir>
52+
<IntDir>$(Platform)\$(Configuration)\</IntDir>
53+
<TargetName>DeviceNameResolver</TargetName>
54+
<TargetExt>.dll</TargetExt>
55+
<GenerateManifest>false</GenerateManifest>
56+
</PropertyGroup>
57+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
58+
<LinkIncremental>false</LinkIncremental>
59+
<OutDir>$(SolutionDir)bin\x64\</OutDir>
60+
<TargetName>DeviceNameResolver</TargetName>
61+
<TargetExt>.dll</TargetExt>
62+
<GenerateManifest>false</GenerateManifest>
63+
</PropertyGroup>
64+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
65+
<ClCompile>
66+
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
67+
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
68+
<WarningLevel>Level3</WarningLevel>
69+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
70+
</ClCompile>
71+
<Link>
72+
<TargetMachine>MachineX86</TargetMachine>
73+
<GenerateDebugInformation>false</GenerateDebugInformation>
74+
<SubSystem>Windows</SubSystem>
75+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
76+
<OptimizeReferences>true</OptimizeReferences>
77+
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
78+
<ProgramDatabaseFile>$(TargetDir)$(TargetName).pdb</ProgramDatabaseFile>
79+
</Link>
80+
</ItemDefinitionGroup>
81+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
82+
<ClCompile>
83+
<PreprocessorDefinitions>WIN64;NDEBUG;_WINDOWS;_USRDLL;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
84+
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
85+
<WarningLevel>Level3</WarningLevel>
86+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
87+
</ClCompile>
88+
<Link>
89+
<GenerateDebugInformation>false</GenerateDebugInformation>
90+
<SubSystem>Windows</SubSystem>
91+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
92+
<OptimizeReferences>true</OptimizeReferences>
93+
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
94+
<ProgramDatabaseFile>$(TargetDir)$(TargetName).pdb</ProgramDatabaseFile>
95+
</Link>
96+
</ItemDefinitionGroup>
97+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
98+
<ImportGroup Label="ExtensionTargets">
99+
</ImportGroup>
100+
</Project>

DeviceNameResolver.vcxproj.filters

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{e71097ee-972f-484d-9678-04f1a5ff606d}</UniqueIdentifier>
6+
</Filter>
7+
<Filter Include="Header Files">
8+
<UniqueIdentifier>{b5560705-07f4-43ed-b061-9d6bbc3336fc}</UniqueIdentifier>
9+
</Filter>
10+
<Filter Include="Resource Files">
11+
<UniqueIdentifier>{e68720e1-78f4-4d40-b8af-3ab88bc78fc2}</UniqueIdentifier>
12+
</Filter>
13+
</ItemGroup>
14+
<ItemGroup>
15+
<ClCompile Include="NativeWinApi.cpp">
16+
<Filter>Source Files</Filter>
17+
</ClCompile>
18+
<ClCompile Include="DeviceNameResolverInternal.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
<ClCompile Include="DeviceNameResolver.cpp">
22+
<Filter>Source Files</Filter>
23+
</ClCompile>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<ClInclude Include="NativeWinApi.h">
27+
<Filter>Header Files</Filter>
28+
</ClInclude>
29+
<ClInclude Include="DynBuf.h">
30+
<Filter>Header Files</Filter>
31+
</ClInclude>
32+
<ClInclude Include="DeviceNameResolverInternal.h">
33+
<Filter>Header Files</Filter>
34+
</ClInclude>
35+
<ClInclude Include="DeviceNameResolver.h">
36+
<Filter>Header Files</Filter>
37+
</ClInclude>
38+
</ItemGroup>
39+
</Project>

DeviceNameResolver.vcxproj.user

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
</Project>

DeviceNameResolverInternal.cpp

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include "DeviceNameResolverInternal.h"
2+
#include "NativeWinApi.h"
3+
4+
DeviceNameResolver::DeviceNameResolver()
5+
{
6+
NativeWinApi::initialize();
7+
initDeviceNameList();
8+
}
9+
10+
DeviceNameResolver::~DeviceNameResolver()
11+
{
12+
deviceNameList.clear();
13+
}
14+
15+
void DeviceNameResolver::initDeviceNameList()
16+
{
17+
TCHAR shortName[3] = {0};
18+
TCHAR longName[MAX_PATH] = {0};
19+
HardDisk hardDisk;
20+
21+
shortName[1] = TEXT(':');
22+
23+
deviceNameList.reserve(3);
24+
25+
for ( TCHAR shortD = TEXT('a'); shortD < TEXT('z'); shortD++ )
26+
{
27+
shortName[0] = shortD;
28+
if (QueryDosDevice( shortName, longName, MAX_PATH ) > 0)
29+
{
30+
hardDisk.shortName[0] = _totupper(shortD);
31+
hardDisk.shortName[1] = TEXT(':');
32+
hardDisk.shortName[2] = 0;
33+
34+
hardDisk.longNameLength = _tcslen(longName);
35+
36+
_tcscpy_s(hardDisk.longName, longName);
37+
deviceNameList.push_back(hardDisk);
38+
}
39+
}
40+
41+
fixVirtualDevices();
42+
}
43+
44+
bool DeviceNameResolver::resolveDeviceLongNameToShort(const TCHAR * sourcePath, TCHAR * targetPath)
45+
{
46+
for (unsigned int i = 0; i < deviceNameList.size(); i++)
47+
{
48+
if (!_tcsnicmp(deviceNameList[i].longName, sourcePath, deviceNameList[i].longNameLength))
49+
{
50+
_tcscpy_s(targetPath, MAX_PATH, deviceNameList[i].shortName);
51+
_tcscat_s(targetPath, MAX_PATH, sourcePath + deviceNameList[i].longNameLength);
52+
return true;
53+
}
54+
}
55+
return false;
56+
}
57+
58+
void DeviceNameResolver::fixVirtualDevices()
59+
{
60+
const USHORT BufferSize = MAX_PATH * 2 * sizeof(WCHAR);
61+
WCHAR longCopy[MAX_PATH] = {0};
62+
OBJECT_ATTRIBUTES oa = {0};
63+
UNICODE_STRING unicodeInput = {0};
64+
UNICODE_STRING unicodeOutput = {0};
65+
HANDLE hFile = 0;
66+
ULONG retLen = 0;
67+
HardDisk hardDisk;
68+
69+
unicodeOutput.Buffer = (PWSTR)malloc(BufferSize);
70+
if (!unicodeOutput.Buffer)
71+
return;
72+
73+
for (unsigned int i = 0; i < deviceNameList.size(); i++)
74+
{
75+
wcscpy_s(longCopy, deviceNameList[i].longName);
76+
77+
NativeWinApi::RtlInitUnicodeString(&unicodeInput, longCopy);
78+
InitializeObjectAttributes(&oa, &unicodeInput, 0, 0, 0);
79+
80+
if(NT_SUCCESS(NativeWinApi::NtOpenSymbolicLinkObject(&hFile, SYMBOLIC_LINK_QUERY, &oa)))
81+
{
82+
unicodeOutput.Length = BufferSize;
83+
unicodeOutput.MaximumLength = unicodeOutput.Length;
84+
ZeroMemory(unicodeOutput.Buffer, unicodeOutput.Length);
85+
86+
if (NT_SUCCESS(NativeWinApi::NtQuerySymbolicLinkObject(hFile, &unicodeOutput, &retLen)))
87+
{
88+
hardDisk.longNameLength = wcslen(unicodeOutput.Buffer);
89+
wcscpy_s(hardDisk.shortName, deviceNameList[i].shortName);
90+
wcscpy_s(hardDisk.longName, unicodeOutput.Buffer);
91+
deviceNameList.push_back(hardDisk);
92+
}
93+
94+
NativeWinApi::NtClose(hFile);
95+
}
96+
}
97+
98+
free(unicodeOutput.Buffer);
99+
}
100+

DeviceNameResolverInternal.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <Windows.h>
2+
3+
4+
#pragma once
5+
6+
#include <Windows.h>
7+
#include <vector>
8+
#include <tchar.h>
9+
10+
class HardDisk
11+
{
12+
public:
13+
TCHAR shortName[3];
14+
TCHAR longName[MAX_PATH];
15+
size_t longNameLength;
16+
};
17+
18+
class DeviceNameResolver
19+
{
20+
public:
21+
DeviceNameResolver();
22+
~DeviceNameResolver();
23+
bool resolveDeviceLongNameToShort(const TCHAR * sourcePath, TCHAR * targetPath);
24+
private:
25+
std::vector<HardDisk> deviceNameList;
26+
27+
void initDeviceNameList();
28+
void fixVirtualDevices();
29+
};
30+

0 commit comments

Comments
 (0)