如何禁用 Word 中的加载项?

Ric*_*den 3 windows microsoft-word

Word 具有禁用的加载项功能(帮助 | 关于 | 禁用项目)。

如何将加载项添加到禁用列表中,而不必使加载项崩溃并等待错误出现?

小智 5

我试图自己找出 DisabledItems 键中值的二进制格式,而您在这里的帖子使我走上了正确的轨道。但是,我认为该格式与您所看到的略有不同,至少在 Office 2010 中是这样。

据我所知,格式是这样的:

  • 前四个字节是一个 32 位整数。它通常似乎具有值 1。我不确定它有什么目的。

  • 接下来的四个字节是一个 32 位整数,它告诉我们 dll 路径的长度(以字节为单位),包括终止字符(空或 0x0000)。

  • 接下来的四个字节是一个 32 位整数,它告诉我们友好名称的长度(以字节为单位),包括终止字符(空或 0x0000)。

  • 下一个字节序列是一个以空字符结尾的 big-endian unicode 字符串,其中包含加载项 dll 的路径。出于某种原因,这条路径似乎总是只包含小写字符。

  • 下一个字节序列是一个以空字符结尾的 big-endian unicode 字符串,其中包含加载项的友好名称。

我已经能够使用以下 C# 代码成功地硬禁用加载项:

string path = "<full path to add-in dll>".ToLower();
string friendlyName = "<add-in friendly name>";
        
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(1); // type of disabled item : 1 => Add in / 2 => Document / 3 => Task pane
writer.Write((path.Length * 2) + 2); // Path length, 2 bytes per character
writer.Write((friendlyName.Length * 2) + 2); // Friendly name length
writer.Write(Encoding.Unicode.GetBytes(path)); // Path
writer.Write(Convert.ToInt16(0)); // null terminator
writer.Write(Encoding.Unicode.GetBytes(friendlyName)); // Friendly name
writer.Write(Convert.ToInt16(0)); // null terminator
writer.Close();

// Version numbers: 11.0 = Office 2003, 12.0 = Office 2007, 14.0 = Office 2010
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\14.0\Word\Resiliency\DisabledItems", true);
key.SetValue("63CB962", stream.ToArray(), RegistryValueKind.Binary);
key.Close();
Run Code Online (Sandbox Code Playgroud)

类似的方法可用于解码现有值的 dll 路径,如下所示:

// Let 'bytes' be a byte array containing the binary registry value
BinaryReader binaryReader = new BinaryReader(new MemoryStream(bytes));
binaryReader.ReadInt32(); // Read the first four bytes and ignore
int pathLength = binaryReader.ReadInt32(); // The next four bytes are the length of the path
binaryReader.Close();
if (bytes.Length >= 12 + pathLength)
{
    string path = Encoding.Unicode.GetString(bytes, 12, pathLength - 2);
}
Run Code Online (Sandbox Code Playgroud)