PathName SessionImpl::GetMyProgramFile(bool canonicalized) { // we do this once if (myProgramFile.Empty()) { #if defined(__APPLE__) CharBuffer<char> buf; uint32_t bufsize = buf.GetCapacity(); if (_NSGetExecutablePath(buf.GetData(), &bufsize) < 0) { buf.Reserve(bufsize); if (_NSGetExecutablePath(buf.GetData(), &bufsize) != 0) { MIKTEX_UNEXPECTED(); } } myProgramFile = buf.GetData(); #else string invocationName = initInfo.GetProgramInvocationName(); if (invocationName.empty()) { MIKTEX_FATAL_ERROR(T_("No invocation name has been set.")); } if (Utils::IsAbsolutePath(invocationName.c_str())) { myProgramFile = invocationName; } else if (invocationName.length() > 3 && (invocationName.substr(0, 2) == "./" || invocationName.substr(0, 3) == "../")) { myProgramFile = GetFullPath(invocationName.c_str()); } else if (!Utils::FindProgram(invocationName, myProgramFile)) { MIKTEX_FATAL_ERROR_2(T_("The invoked program could not be found in the PATH."), "invocationName", invocationName); } #endif myProgramFileCanon = myProgramFile; myProgramFileCanon.Canonicalize(); } if (canonicalized) { return myProgramFileCanon; } else { return myProgramFile; } }
// see Q246772 bool Utils::GetDefPrinter(string& printerName) { OSVERSIONINFOW osv; osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW); GetVersionExW(&osv); if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { #if defined(MIKTEX_SUPPORT_LEGACY_WINDOWS) unsigned long dwNeeded, dwReturned; EnumPrintersW(PRINTER_ENUM_DEFAULT, 0, 2, 0, 0, &dwNeeded, &dwReturned); if (dwNeeded == 0) { return false; } AutoGlobalMemory hMem(GlobalAlloc(GPTR, dwNeeded)); if (hMem.Get() == nullptr) { OUT_OF_MEMORY("GlobalAlloc"); } PRINTER_INFO_2W* ppi2 = reinterpret_cast<PRINTER_INFO_2W *>(hMem.Get()); if (!EnumPrintersW(PRINTER_ENUM_DEFAULT, 0, 2, reinterpret_cast<LPBYTE>(ppi2), dwNeeded, &dwNeeded, &dwReturned)) { return false; } size_t l = StrLen(ppi2->pPrinterName); if (l >= *pBufferSize) { *pBufferSize = l + 1; return false; } StringUtil::CopyString(pPrinterName, *pBufferSize, ppi2->pPrinterName); *pBufferSize = l + 1; return true; #else UNSUPPORTED_PLATFORM(); #endif } else { if (osv.dwPlatformId != VER_PLATFORM_WIN32_NT) { MIKTEX_UNEXPECTED(); } if (osv.dwMajorVersion >= 5) { CharBuffer<wchar_t> printerNameBuf; DWORD dwBufferSize = static_cast<DWORD>(printerNameBuf.GetCapacity()); BOOL bDone = GetDefaultPrinterW(printerNameBuf.GetData(), &dwBufferSize); if (!bDone) { if (::GetLastError() == ERROR_FILE_NOT_FOUND) { return false; } else { MIKTEX_FATAL_WINDOWS_ERROR("GetDefaultPrinterW"); } } else { printerName = WU_(printerNameBuf.GetData()); return true; } } else { #if defined(MIKTEX_SUPPORT_LEGACY_WINDOWS) wchar_t cBuffer[4096]; if (GetProfileStringW(L"windows", L"device", L",,,", cBuffer, 4096) <= 0) { return false; } Tokenizer tok(StringUtil::WideCharToUTF8(cBuffer), ","); if (tok.GetCurrent() == nullptr) { return false; } unsigned long l = static_cast<unsigned long>(StrLen(tok.GetCurrent())); if (l >= *pBufferSize) { *pBufferSize = l + 1; return false; } StringUtil::CopyString(pPrinterName, *pBufferSize, tok.GetCurrent()); *pBufferSize = l + 1; return true; #else UNSUPPORTED_PLATFORM(); #endif } } }