void ActivationContextLoader::Initialise(const CStdString& sManifest) { if (CGeneral::FileExists(sManifest)) { m_sManifest = sManifest; } else { CPath path; path.Combine(GetModulePath(), sManifest); m_sManifest = path; } try { // Don't do anything if we're not at least running on xp m_bShouldLoadActivationContext = ShouldLoadActivationContext(); if (m_bShouldLoadActivationContext) { LoadDll(); if (!IsActivationContextAlreadyActiveForManifest()) { LoadAC(); } } } catch (std::exception & e) { CStdString sError = CA2T(e.what()); LOG_WS_ERROR(sError); } }
void ActivationContextLoader::LoadDll() { // Get a handle to the DLL module. CPath dllPath; dllPath.Combine(GetModulePath(), WS_ACTIVATIONCONTEXTLOADER_DLL); m_hinstLib = LoadLibrary(dllPath); // If the handle is valid, try to get the function address. if (m_hinstLib != NULL) { m_funcLAC = (LAC) GetProcAddress(m_hinstLib, WS_ACLOADFUNC); m_funcULAC = (ULAC) GetProcAddress(m_hinstLib, WS_ACUNLOADFUNC); m_funcIAAFM = (IAAFM) GetProcAddress(m_hinstLib, WS_ACALREADYACTIVEFUNC); } else { CStdStringA msg; msg.Format("LoadLibrary on '%s'failed. Error code: %d.", WS_ACTIVATIONCONTEXTLOADER_DLL_A, GetLastError()); throw std::exception(msg); } }
CString CPPageWebServer::GetCurWebRoot() { CString WebRoot; GetDlgItem(IDC_EDIT2)->GetWindowText(WebRoot); WebRoot.Replace('/', '\\'); CPath path; path.Combine(GetMPCDir(), WebRoot); return path.IsDirectory() ? (LPCTSTR)path : _T(""); }
void CPlayerPlaylistBar::LoadPlaylist(LPCTSTR filename) { CString base; if (AfxGetMyApp()->GetAppSavePath(base)) { CPath p; p.Combine(base, _T("default.mpcpl")); if (p.FileExists()) { if (AfxGetAppSettings().bRememberPlaylistItems) { ParseMPCPlayList(p); Refresh(); SelectFileInPlaylist(filename); } else { ::DeleteFile(p); } } } }
bool CWebServer::ToLocalPath(CString& path, CString& redir) { if(!path.IsEmpty() && m_webroot.IsDirectory()) { CString str = path; str.Replace('/', '\\'); str.TrimLeft('\\'); CPath p; p.Combine(m_webroot, str); p.Canonicalize(); if(p.IsDirectory()) { CAtlList<CString> sl; Explode(AfxGetAppSettings().WebDefIndex, sl, ';'); POSITION pos = sl.GetHeadPosition(); while(pos) { str = sl.GetNext(pos); CPath p2 = p; p2.Append(str); if(p2.FileExists()) { p = p2; redir = path; if(redir.GetAt(redir.GetLength()-1) != '/') redir += '/'; redir += str; break; } } } if(_tcslen(p) > _tcslen(m_webroot) && p.FileExists()) { path = (LPCTSTR)p; return true; } } return false; }
void CPlayerPlaylistBar::SavePlaylist() { CString base; if (AfxGetMyApp()->GetAppSavePath(base)) { CPath p; p.Combine(base, _T("default.mpcpl")); if (AfxGetAppSettings().bRememberPlaylistItems) { // Only create this folder when needed if (!::PathFileExists(base)) { ::CreateDirectory(base, NULL); } SaveMPCPlayList(p, CTextFile::UTF8, false); } else if (p.FileExists()) { ::DeleteFile(p); } } }
CString CombinePaths(LPCTSTR dir, LPCTSTR path) { CPath cp; cp.Combine(dir, path); return cp; }
HBITMAP CDpiHelper::CreateDeviceFromLogicalImage(HBITMAP _In_ hImage, ImageScalingMode scalingMode, Color clrBackground) { IfNullAssertRetNull(hImage, "No image given to convert"); // Instead of doing HBITMAP resizing with StretchBlt from one memory DC into other memory DC and HALFTONE StretchBltMode // which uses nearest neighbor resize algorithm (fast but results in pixelation), we'll use a GdiPlus image to do the resize, // which allows specifying the interpolation mode for the resize resulting in smoother result. VsUI::GdiplusImage gdiplusImage; // Attaching the bitmap uses Bitmap.FromHBITMAP which does not take ownership of the HBITMAP passed as argument. // DeleteObject still needs to be used on the hImage but that should happen after the Bitmap object is deleted or goes out of scope. // The caller will have to DeleteObject both the HBITMAP they passed in this function and the new HBITMAP we'll be returning when we detach the GDI+ Bitmap gdiplusImage.Attach(hImage); #ifdef DEBUG static bool fDebugDPIHelperScaling = false; WCHAR rgTempFolder[MAX_PATH]; static int imgIndex = 1; CStringW strFileName; CPath pathTempFile; if (fDebugDPIHelperScaling) { if (!GetTempPath(_countof(rgTempFolder), rgTempFolder)) *rgTempFolder = '\0'; strFileName.Format(_T("DPIHelper_%05d_Before.png"), imgIndex); pathTempFile.Combine(rgTempFolder, strFileName); gdiplusImage.Save(pathTempFile); } #endif Bitmap* pBitmap = gdiplusImage.GetBitmap(); PixelFormat format = pBitmap->GetPixelFormat(); const Color *pclrActualBackground = &clrBackground; InterpolationMode interpolationMode = GetInterpolationMode(scalingMode); ImageScalingMode actualScalingMode = GetActualScalingMode(scalingMode); if (actualScalingMode != ImageScalingMode::NearestNeighbor) { // Modify the image. If the image is 24bpp or lower, convert to 32bpp so we can use alpha values if (format != PixelFormat32bppARGB) { pBitmap->ConvertFormat(PixelFormat32bppARGB, DitherTypeNone, PaletteTypeCustom, nullptr/*ColorPalette*/, 0 /*alphaThresholdPercent - all opaque*/); } // Now that we have 32bpp image, let's play with the pixels // Detect magenta or near-green in the image and use that as background VsUI::GdiplusImage::ProcessBitmapBits(pBitmap, [&](ARGB * pPixelData) { if (clrBackground.GetValue() != TransparentColor.GetValue()) { if (*pPixelData == clrBackground.GetValue()) { *pPixelData = TransparentHaloColor.GetValue(); pclrActualBackground = &clrBackground; } } else { if (*pPixelData == MagentaColor.GetValue()) { *pPixelData = TransparentHaloColor.GetValue(); pclrActualBackground = &MagentaColor; } else if (*pPixelData == NearGreenColor.GetValue()) { *pPixelData = TransparentHaloColor.GetValue(); pclrActualBackground = &MagentaColor; } } }); } // Convert the GdiPlus image if necessary LogicalToDeviceUnits(&gdiplusImage, scalingMode, TransparentHaloColor); // Get again the bitmap, after the resize pBitmap = gdiplusImage.GetBitmap(); if (actualScalingMode != ImageScalingMode::NearestNeighbor) { // Now that the bitmap is scaled up, convert back the pixels. // Anything that is not fully opaque, make it clrActualBackground VsUI::GdiplusImage::ProcessBitmapBits(pBitmap, [&](ARGB * pPixelData) { if ((*pPixelData & ALPHA_MASK) != 0xFF000000) { *pPixelData = pclrActualBackground->GetValue(); } }); // Convert back to original format if (format != PixelFormat32bppARGB) { pBitmap->ConvertFormat(format, DitherTypeNone, PaletteTypeCustom, nullptr/*ColorPalette*/, 0 /*alphaThresholdPercent - all opaque*/); } } #ifdef DEBUG if (fDebugDPIHelperScaling) { strFileName.Format(_T("DPIHelper_%05d_After.png"), imgIndex++); pathTempFile.Combine(rgTempFolder, strFileName); gdiplusImage.Save(pathTempFile); } #endif // Get the converted image handle - this returns a new HBITMAP that will need to be deleted when no longer needed // Detach using TransparentColor (transparent-black). If the result bitmap is to be used with AlphaBlend, that function // keeps the background if the transparent pixels are black HBITMAP hBmpResult = gdiplusImage.Detach( TransparentColor ); // When the image has 32bpp RGB format, when we call GDI+ to return an HBITMAP for the image, the result is actually // an ARGB bitmap (with alpha bytes==0xFF instead of reserved=0x00). Many GDI functions work with it fine, but // adding it to an imagelist with ImageList_AddMasked will produce the wrong result, because the clrTransparent color // won't match any background pixels due to the alpha byte value. So we need to zero-out out those bytes... // If the bitmap was scaled with a bicubic/bilinear interpolation, the colors are interpolated with the clrBackground // which may be transparent, so the resultant image will have alpha channel of interest, and we'll return the image as is. if (format == PixelFormat32bppRGB) { BITMAP bmp = {0}; if (GetObject(hBmpResult, sizeof(bmp), &bmp) == sizeof(bmp) && bmp.bmBits != nullptr) { RGBQUAD* pPixels = reinterpret_cast<RGBQUAD*>(bmp.bmBits); for (int i=0; i< bmp.bmWidth * bmp.bmHeight; i++) { pPixels[i].rgbReserved = 0; } } } // Return the created image return hBmpResult; }