使用SetDefaultDllDirectories破坏字体处理 [英] Using SetDefaultDllDirectories breaks Font handling

查看:216
本文介绍了使用SetDefaultDllDirectories破坏字体处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在一个过去运行良好的程序中遇到问题.我将其跟踪到以下代码:

I recently got a problem in a program that used to work fine. I tracked it down to the following code:

using System.Drawing;
using System.Runtime.InteropServices;

namespace Foo
{
    static class CProgram
    {
        [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetDefaultDllDirectories(int directoryFlags);

        public const int LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x000001000;

        private static void Main()
        {
            SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
            Font font = SystemFonts.DefaultFont;
        }
    }
}

一旦我将SetDefaultDllDirectories用作非零参数,程序就会崩溃.我将其追溯到SafeNativeMethods.Gdip.GdipGetGenericFontFamitSansSerif(out fontfamily);简称为"GdipGetGenericFontFamilySansSerif".但是此调用失败,并显示FontFamilyNotFound错误.

Once I use SetDefaultDllDirectories with anything but zero as a parameter the program crashes. I traced it to SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif(out fontfamily); which simply calls "GdipGetGenericFontFamilySansSerif". But this call fails with an FontFamilyNotFound error.

它可以在没有SetDefaultDllDirectories的情况下工作.如果我将字体分配放在调用之后的AND之前,它甚至可以工作.

It works without the SetDefaultDllDirectories. And it even works, if I place the font assignment before AND after that call.

我的系统上是否有引起此错误的消息,或者是MS的更新导致了此错误?

Is there anything on my system that causes this or was it an update from MS that causes this bug?

系统:Win7 x64,完全更新,使用了最新的Beta驱动程序的AMD Radeon HD

System: Win7 x64, fully updated, AMD Radeon HD with latest beta drivers used

背景:我需要使用AddDllDirectory函数来添加我的可执行文件路径的子目录(例如C:/MyProgram/myLibrariesX)

Background: I need that function to use AddDllDirectory to add a subdirectory of my executables path (something like C:/MyProgram/myLibrariesX)

推荐答案

我遇到了同样的问题,因为我需要在基本文件夹(即x86和x64)下的体系结构文件夹中找到本机库.我所使用的解决方法是改为将特定于体系结构的文件夹附加到PATH环境变量中.

I ran into the same problem as I needed native libraries to be found within an architecture folder under the base folder, i.e. x86 and x64. The work around I used was to instead append the architecture specific folder to the PATH environment variable.

var archPath = $"{AppDomain.CurrentDomain.BaseDirectory}{Environment.Is64BitProcess ? "x64" : "x86"}\\";
var pathVariable = Environment.GetEnvironmentVariable("PATH");
pathVariable = $"{archPath};{pathVariable}";
Environment.SetEnvironmentVariable("PATH", pathVariable);

AppDomain.CurrentDomain.AssemblyResolve 事件.

The PATH isn't used for assembly probing but is used for native DLL probing. For assemblies I used the AppDomain.CurrentDomain.AssemblyResolve event.

这篇关于使用SetDefaultDllDirectories破坏字体处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆