关于RegisterClass的注册位置

本文详细探讨了RegisterClass函数的工作原理及其返回的ATOM值的作用。解释了窗口类是如何被保存在User32.dll维护的原子表中,并分析了ATOM值在获取窗口类信息时的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天在smth,有人问起RegisterClass函数到底将窗口类注册到哪里了,想了一下,应该是一个系统级的存储空间里,但是却没有一个明确的说法,msdn上看了半天,基本上没有提到具体注册的位置。倒是返回值给了不少提示,ATOM,查ATOM终于找到如下的一段描述

中文翻译:

该系统提供了一个原子表的数量。每个原子表提供不同的目的。例如,动态数据交换(DDE)应用程序使用全局原子表与其他应用共享的项目名称和主题名称字符串。而不是通过实际的字符串,一个DDE应用程序传递全局原子,其合作伙伴应用程序。合作伙伴使用的原子,从原子表获得的字符串。应用程序可以使用本地原子表来存储自己的项目名称协会。该系统使用的应用程序不能直接访问的原子表。然而,应用程序使用这些原子时调用各种功能。例如,注册剪贴板格式存储在原子内部系统所使用的表。应用程序中添加原子原子表使用RegisterClipboardFormat函数。此外,注册类存储在原子内部系统所使用的表。应用程序中添加原子原子表使用RegisterClass或RegisterClassEx函数。

原文:

 The system provides a number of atom tables. Each atom table serves a different purpose. For example, dynamic data exchange (DDE) applications use the global atom table to share item-name and topic-name strings with other applications. Rather than passing actual strings, a DDE application passes global atoms to its partner application. The partner uses the atoms to obtain the strings from the atom table.

Applications can use local atom tables to store their own item-name associations.

The system uses atom tables that are not directly accessible to applications. However, the application uses these atoms when calling a variety of functions. For example, registered clipboard formats are stored in an internal atom table used by the system. An application adds atoms to this atom table using the RegisterClipboardFormat function. Also, registered classes are stored in an internal atom table used by the system. An application adds atoms to this atom table using the RegisterClass or RegisterClassEx function.


也就是说应该在一个原子表中,于是google之,终于找到一片像样的文章。转贴如下:

中文翻译:

registerClass返回原子有什么用?

通过registerClass和RegisterClassEx函数返回一个原子。什么是原子?所有注册窗口类的名字被保存在一个原子表的内部,以USER32。由类注册函数的返回值是原子。您还可以检索一个窗口类,该类的窗口通过GetClassWord(HWND,GCW_ATOM)要求其类原子的原子。原子可以转换为整数的原子通过的MAKEINTATOM的宏,然后就可以使用函数接受字符串或原子的形式中的类名。最常见的情况是lpClassName参数CreateWindow的宏观和CreateWindowEx函数。不太常用,你也可以作为的lpClassName的getClassInfo并GetClassInfoEx功能参数使用。 (虽然你为什么会做到这一点我无法弄清楚。为了使原子传递到摆在首位GetClassInfo,你必须注册类(因为那是什么返回原子),在这种情况下,为什么你要求有关类的信息,你注册了吗?)转换类原子的类的名称,你可以创建一个该类的虚拟窗口,然后做上述GetClassWord(HWND,GCW_ATOM)的。或者您可以采取的事实,从GetClassInfoEx函数的返回值是原子类,转换为一个BOOL的优势。这可以让你做转换,而无需创建一个虚拟的窗口。 (请注意,然而,GetClassInfoEx的返回值是不是原子在Windows 95的派生操作系统。)但是,什么是原子?不多,真的。当然,它可以节省你不必像CreateWindow的函数传递一个字符串,但它确实是替换字符串与一个整数,你现在有一个全局变量中保存供以后使用。什么是一个字符串,你可以硬编码现在是一个原子,你必须跟踪。目前还不清楚,你实际上有赢得什么。我想你可以用它快速检查一个窗口是否属于一个特定的类。你得到这个类的原子(通过GetClassInfo,说),然后得到窗口的原子,并加以比较。但你不能缓存类的原子,因为类可能会得到未注册和重新注册(这将给它一个新的原子数)。你不能预取类原子,因为类可能尚未被登记在你预取点。 (如上所述,你可以不缓存预取的价值了。),所以这种情况是相当多的非起动无论如何,你可能也使用GetClassName函数和比较你要找的类生成的类名为。换句话说,窗口类的原子是不合时宜的。替换对话框类一样,这是一个Win32 API的那些泛泛从未真正得到掉在地上,但必须进行向前向后兼容性。但至少现在,你知道它们是什么。

原文:

What's the atom returned by RegisterClass useful for?

The RegisterClass and RegisterClassEx functions return an ATOM. What is that ATOM good for?

The names of all registered window classes is kept in an atom table internal to USER32. The value returned by the class registration functions is that atom. You can also retrieve the atom for a window class by asking a window of that class for its class atom via GetClassWord(hwnd, GCW_ATOM).

The atom can be converted to an integer atom via the MAKEINTATOM macro, which then can be used by functions that accept class names in the form of strings or atoms. The most common case is the lpClassName parameter to the CreateWindow macro and the CreateWindowEx function. Less commonly, you can also use it as the lpClassName parameter for the GetClassInfo and GetClassInfoEx functions. (Though why you would do this I can't figure out. In order to have the atom to pass to GetClassInfo in the first place, you must have registered the class (since that's what returns the atom), in which case why are you asking for information about a class that you registered?)

To convert a class name to a class atom, you can create a dummy window of that class and then do the aforementioned GetClassWord(hwnd, GCW_ATOM). Or you can take advantage of the fact that the return value from the GetClassInfoEx function is the atom for the class, cast to a BOOL. This lets you do the conversion without having to create a dummy window. (Beware, however, that GetClassInfoEx's return value is not the atom on Windows 95-derived operating systems.)

But what good is the atom?

Not much, really. Sure, it saves you from having to pass a string to functions like CreateWindow, but all it did was replace a string with with an integer you now have to save in a global variable for later use. What used to be a string that you could hard-code is now an atom that you have to keep track of. Unclear that you actually won anything there.

I guess you could use it to check quickly whether a window belongs to a particular class. You get the atom for that class (via GetClassInfo, say) and then get the atom for the window and compare them. But you can't cache the class atom since the class might get unregistered and then re-registered (which will give it a new atom number). And you can't prefetch the class atom since the class may not yet be registered at the point you prefetch it. (And as noted above, you can't cache the prefetched value anyway.) So this case is pretty much a non-starter anyway; you may as well use the GetClassName function and compare the resulting class name against the class you're looking for.

In other words, window class atoms are an anachronism. Like replacement dialog box classes, it's one of those generalities of the Win32 API that never really got off the ground, but which must be carried forward for backwards compatibility.

But at least now you know what they are.

最终的结论,RegisterClass应该是将窗口类的数据放在User32.dll维护的一个原子表中了:)

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Hamxj/archive/2007/05/09/1601758.aspx

### 如何注册COM组件 在开发过程中,如果需要让其他应用程序通过 COM 技术调用 C# 编写的类库,则必须完成 COM 组件的注册过程。以下是关于如何实现这一目标的具体方法。 #### 使用 `regasm.exe` 工具注册 COM 组件 Microsoft 提供了一种命令行工具 `RegAsm.exe` 来简化 .NET 程序集到 Windows 注册表中的映射操作[^1]。此工具可以将程序集中包含的信息写入系统的注册表项中,从而允许非托管客户端代码创建并使用其中的对象实例。 执行如下命令即可完成基本的 COM 注册工作: ```cmd regasm /codebase YourAssembly.dll ``` 上述指令会把指定路径下的 DLL 文件及其公开类型记录至本地计算机的系统配置数据库里[^1]。需要注意的是,在某些情况下可能还需要额外设置权限或者管理员身份运行 CMD 才能成功应用更改。 #### 在代码内部手动处理注册逻辑 除了依赖外部实用程序之外,也可以直接于项目源码层面加入自定义属性 `[ComVisible(true)]` 和继承自 `System.EnterpriseServices.ServicedComponent` 的基类来增强功能支持程度[^1]。另外一种方式则是利用静态构造函数配合反射技术动态获取当前类型的 GUID 值,并将其提交给操作系统用于后续查找匹配用途。 下面展示了一个简单的例子说明怎样通过编程手段达成相同效果: ```csharp using System; using System.Runtime.InteropServices; namespace ComRegistrationExample { [Guid("F072B8CA-F95E-4A6D-BCCB-EFC360BAEBFB")] public interface IMyComInterface { void DoWork(); } [ClassInterface(ClassInterfaceType.None)] [Guid("C83EF3FF-DAD2-42ae-AEDF-CCEBF3EEAEDE")] [ProgId("ComRegistrationExample.MyComClass")] public class MyComClass : IMyComInterface { public void DoWork() { Console.WriteLine("Working..."); } /// <summary> /// 自动化注册/注销流程控制入口点. /// </summary> [ComRegisterFunction()] public static void RegisterClass(string key) { StringBuilder s = new StringBuilder(key); s.Replace(@"HKEY_CLASSES_ROOT\", ""); using (RegistryKey k = Registry.ClassesRoot.CreateSubKey(s.ToString() + @"\CLSID")) { k.SetValue("", "Description of my component"); } } [ComUnregisterFunction()] public static void UnregisterClass(string key) { StringBuilder s = new StringBuilder(key); s.Replace(@"HKEY_CLASSES_ROOT\", ""); try { Registry.ClassesRoot.DeleteSubKeyTree(s.ToString()); } catch {} } } } ``` 以上片段展示了如何借助特性装饰以及特定签名的方法来自定义安装卸载行为[^1]。当编译后的二进制文件被引入环境变量 PATH 中所列位置之一时,这些特殊标记就会触发相应的动作序列。 #### 解决常见错误提示 有时即使完成了所有必要的前期准备工作仍然会出现无法加载的情况,这可能是由于缺少正确的架构兼容性声明造成的。确保生成的目标平台选项与实际部署机器保持一致非常重要;例如对于 x64 架构的操作系统而言,默认构建出来的 AnyCPU 可能不会自动切换成适合的形式,因此建议显式指明 Target CPU Architecture 设置为 X86 或者 Amd64 根据具体需求而定[^1]。 此外还需注意版本冲突问题——假如多个不同修订号共存的话可能会引起混乱现象发生。可以通过强命名机制加以区分管理避免此类状况再次重现出来。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值