Skip to content

Commit

Permalink
kenjinote#1 カレントディレクトリを変更できない
Browse files Browse the repository at this point in the history
kenjinote#2 cmdchat.exeと同一ディレクトリにcmd.exeがあるとコマンド実行時に実行されてしまう

テキスト入力フォントをConsolasに変更
  • Loading branch information
kenjinote committed Oct 18, 2020
1 parent dcf3fa2 commit e0a1173
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
/cmdchat.aps
/cmdchat.vcxproj.user
/cmdchat.vcxproj.filters
/Debug
/Release
/x64/Release
60 changes: 58 additions & 2 deletions Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <atlbase.h>
#include <atlhost.h>
#include <string>
#include <winternl.h>
#include "resource.h"

#define DEFAULT_DPI 96
Expand Down Expand Up @@ -57,6 +58,53 @@ BOOL GetScaling(HWND hWnd, UINT* pnX, UINT* pnY)
return bSetScaling;
}

LPWSTR GetCurrentWorkingDirectory(HANDLE hProcess)
{
struct UPP {
long MaximumLength;
long Length;
long Flags;
long DebugFlags;
HANDLE ConsoleHandle;
long ConsoleFlags;
HANDLE StdInputHandle;
HANDLE StdOuputHandle;
HANDLE StdErrorHandle;
UNICODE_STRING CurrentDirectoryPath;
HANDLE CurrentDirectoryHandle;
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine;
};
LPWSTR lpszReturn = 0;
HMODULE hModule = GetModuleHandleW(L"ntdll");
if (hModule) {
typedef NTSTATUS(__stdcall* fnNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
fnNtQueryInformationProcess NtQueryInformationProcess = fnNtQueryInformationProcess(GetProcAddress(hModule, "NtQueryInformationProcess"));
if (NtQueryInformationProcess) {
PROCESS_BASIC_INFORMATION pbi = { 0 };
ULONG len = 0;
if (NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), &len) == 0 && len > 0) {
SIZE_T nRead = 0;
PEB peb = { 0 };
UPP upp = { 0 };
if (ReadProcessMemory(hProcess, pbi.PebBaseAddress, &peb, sizeof(peb), &nRead) && nRead > 0 &&
ReadProcessMemory(hProcess, peb.ProcessParameters, &upp, sizeof(upp), &nRead) && nRead > 0) {
PVOID buffer = upp.CurrentDirectoryPath.Buffer;
USHORT length = upp.CurrentDirectoryPath.Length;
lpszReturn = (LPWSTR)GlobalAlloc(0, (length / 2 + 1) * sizeof(WCHAR));
if (!ReadProcessMemory(hProcess, buffer, lpszReturn, length, &nRead) || nRead == 0)
{
GlobalFree(lpszReturn);
lpszReturn = 0;
}
lpszReturn[length / 2] = 0;
}
}
}
}
return lpszReturn;
}

LPWSTR RunCommand(LPCWSTR lpszCommand)
{
if (!lpszCommand) return 0;
Expand All @@ -77,7 +125,10 @@ LPWSTR RunCommand(LPCWSTR lpszCommand)
si.wShowWindow = SW_HIDE;
LPWSTR lpszSendBuffer = (LPWSTR)GlobalAlloc(0, (lstrlenW(lpszCommand) + 256) * sizeof(WCHAR));
if (lpszSendBuffer) {
lstrcpyW(lpszSendBuffer, L"cmd.exe /C ");
WCHAR szCmdExePath[MAX_PATH] = { 0 };
GetEnvironmentVariableW(L"ComSpec", szCmdExePath, _countof(szCmdExePath));
lstrcpyW(lpszSendBuffer, szCmdExePath);
lstrcatW(lpszSendBuffer, L" /K ");
lstrcatW(lpszSendBuffer, lpszCommand);
PROCESS_INFORMATION pi = { 0 };
if (CreateProcessW(NULL, lpszSendBuffer, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
Expand All @@ -104,6 +155,11 @@ LPWSTR RunCommand(LPCWSTR lpszCommand)
MultiByteToWideChar(CP_THREAD_ACP, 0, str.c_str(), -1, lpszReturn, nLength + 1);
}
CloseHandle(pi.hThread);
{
LPWSTR lpszCurrentDirectory = GetCurrentWorkingDirectory(pi.hProcess);
SetCurrentDirectoryW(lpszCurrentDirectory);
GlobalFree((HGLOBAL)lpszCurrentDirectory);
}
TerminateProcess(pi.hProcess, 0);
CloseHandle(pi.hProcess);
}
Expand Down Expand Up @@ -363,7 +419,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_APP+1:
GetScaling(hWnd, &uDpiX, &uDpiY);
DeleteObject(hFont);
hFont = CreateFontW(-POINT2PIXEL(12), 0, 0, 0, FW_NORMAL, 0, 0, 0, SHIFTJIS_CHARSET, 0, 0, 0, 0, L"Yu Gothic UI");
hFont = CreateFontW(-POINT2PIXEL(12), 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, L"Consolas");
SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, 0);
break;
case WM_CLOSE:
Expand Down
4 changes: 4 additions & 0 deletions cmdchat.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<TargetName>cmdc</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<TargetName>cmdc</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<TargetName>cmdc</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<TargetName>cmdc</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand Down

0 comments on commit e0a1173

Please sign in to comment.