手写Windows文件路径获取小工具
目的
给Windows右键增加功能,右键任何文件(夹)显示复制文件路径的扩展。
效果展示
实现思路
右键调用,自身会把文件路径传递给被调用文件,被调用文件内只需将路径参数复制到剪贴板即可。
局部关键代码
字符串复制到剪贴板:
static DWORD SetStringToClipboard(LPCWSTR string) {
int string_length = lstrlen(string);
int memory_size = (string_length + 1) * sizeof(wchar_t);
HGLOBAL memory_handle = GlobalAlloc(GMEM_MOVEABLE, memory_size);
if (memory_handle == NULL) {
return GetLastError();
}
DWORD error = CopyStringToMemory(string, string_length, memory_handle);
if (error != ERROR_SUCCESS) {
GlobalFree(memory_handle);
return error;
}
HANDLE result = SetClipboardData(CF_UNICODETEXT, memory_handle);
error = GetLastError();
if (result == NULL) {
GlobalFree(memory_handle);
return error;
}
return ERROR_SUCCESS;
}
源代码
纯C实现,VS2022 MSVC-64bit 编译
#include <Windows.h>
#include <Shlwapi.h>
#include <strsafe.h>
static DWORD SetStringToClipboard(LPCWSTR string);
static DWORD CopyStringToMemory(LPCWSTR string, int copied_length, HGLOBAL memory_handle);
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
int argument_count = 0;
LPWSTR* arguments = CommandLineToArgvW(GetCommandLine(), &argument_count);
if (argument_count < 3) {
return ERROR_INVALID_PARAMETER;
}
BOOL name_only = FALSE;
if (lstrcmp(arguments[1], L"-n") == 0) {
name_only = TRUE;
}
else if (lstrcmp(arguments[1], L"-p") == 0) {
name_only = FALSE;
}
else {
return ERROR_INVALID_PARAMETER;
}
LPWSTR string_to_set = arguments[2];
if (name_only) {
string_to_set = PathFindFileName(string_to_set);
}
BOOL is_succeeded = OpenClipboard(NULL);
if (!is_succeeded) {
return GetLastError();
}
EmptyClipboard();
DWORD error = SetStringToClipboard(string_to_set);
CloseClipboard();
return error;
}
static DWORD SetStringToClipboard(LPCWSTR string) {
int string_length = lstrlen(string);
int memory_size = (string_length + 1) * sizeof(wchar_t);
HGLOBAL memory_handle = GlobalAlloc(GMEM_MOVEABLE, memory_size);
if (memory_handle == NULL) {
return GetLastError();
}
DWORD error = CopyStringToMemory(string, string_length, memory_handle);
if (error != ERROR_SUCCESS) {
GlobalFree(memory_handle);
return error;
}
HANDLE result = SetClipboardData(CF_UNICODETEXT, memory_handle);
error = GetLastError();
if (result == NULL) {
GlobalFree(memory_handle);
return error;
}
return ERROR_SUCCESS;
}
static DWORD CopyStringToMemory(LPCWSTR string, int copied_length, HGLOBAL memory_handle) {
LPVOID memory = GlobalLock(memory_handle);
if (memory == NULL) {
return GetLastError();
}
HRESULT result = StringCchCopy(memory, copied_length + 1, string);
GlobalUnlock(memory_handle);
if (FAILED(result)) {
return ERROR_OUTOFMEMORY;
}
return ERROR_SUCCESS;
}
安装与卸载
编译完程序后,需要增加到右键功能栏,即需要修改注册表增加功能。
使用方式:保存为.bat文件后,双击运行。
安装:
这里用bat批处理脚本实现安装:
@echo off
set exe_path="%~dp0Release\CopyPath.exe"
if not exist %exe_path% (
echo %exe_path% not found.
echo Please build the solution with release configuration,
echo or modify this script to point to an existent executable file.
echo Install failed.
goto :end
)
set reg_path=HKEY_CURRENT_USER\Software\Classes
reg add %reg_path%\*\shell\CopyPath /f /ve /d "Copy path"
reg add %reg_path%\*\shell\CopyPath\command /f /ve /d "%exe_path% -p \"%%1\""
reg add %reg_path%\*\shell\CopyName /f /ve /d "Copy name"
reg add %reg_path%\*\shell\CopyName\command /f /ve /d "%exe_path% -n \"%%1\""
reg add %reg_path%\Folder\shell\CopyPath /f /ve /d "Copy path"
reg add %reg_path%\Folder\shell\CopyPath\command /f /ve /d "%exe_path% -p \"%%1\""
reg add %reg_path%\Folder\shell\CopyName /f /ve /d "Copy name"
reg add %reg_path%\Folder\shell\CopyName\command /f /ve /d "%exe_path% -n \"%%1\""
echo Install succeeded.
:end
pause
注意:exe_path需要设置为编译后exe所在路径,且运行脚本后,exe不可移动!
卸载:
清理增加的注册表项即可。
@echo off
set reg_path=HKEY_CURRENT_USER\Software\Classes
reg delete %reg_path%\*\shell\CopyPath /f
reg delete %reg_path%\*\shell\CopyName /f
reg delete %reg_path%\Folder\shell\CopyPath /f
reg delete %reg_path%\Folder\shell\CopyName /f
echo Uninstallation completed.