How do my .NET-based clients get the app's HINSTANCE to pass to my function?
来源:互联网 发布:js回调函数的执行顺序 编辑:程序博客网 时间:2024/05/22 10:26
Q I'm in the process of converting an existing C++ class library to the Managed Extensions so I can expose it to my .NET Framework-based clients. Some of my code calls API functions that require the HINSTANCE of the current running module. I don't want to use the HINSTANCE of my DLL; I want the caller to supply the HINSTANCE of the EXE calling my DLL. I can declare the HINSTANCE parameter as an IntPtr, but how do my .NET-based clients get the app's HINSTANCE to pass to my function? For example, how would they do it in C#?
A Good question! This one stumped me for about 15 minutes. In MFC you can call AfxGetInstanceHandle(). MFC stores the HINSTANCE in its Application object, in CWinApp::m_hInstance. So if you're using the Microsoft® .NET Framework, you might think that you should take a look in the Application object for Application.HInstance or an Application property or some such thing. Why not? Because it's not there!
If you search the Framework documentation for "hinstance," you'll discover there's a method Marshal.GetHINSTANCE. Well, that sounds promising. This static Marshal method requires the module whose HINSTANCE you want to obtain:
// In C#
Module m;
...
IntPtr h = Marshal.GetHINSTANCE(m);
Now you know how to get the HINSTANCE—but where do you get the module? Again, you might think to look in the Application class for something like Application.GetModule. Or perhaps Application is derived from module, that would make sense. Alas, neither is true. Well, maybe there's a Module property. Nope. Well, not quite. There is a Module property, but it's not a property of the Application, it's a Type property. In the .NET Framework, every object has a Type and every Type has a Module. Type.Module represents whichever module implements the type. So to get the HINSTANCE of the calling module, your clients can write:
Type t = myObj.GetType();
Module m = t.Module;
IntPtr h = Marshal.GetHINSTANCE(m);
You can also use typeof (--typeof in C++)to get the type without an object instance, as in typeof(MyApp). Just tell your customers to make sure they use a type that's implemented in the calling module. If you use some other type—for example, one of the Framework types like String—you'll get the wrong module.
Figure 1 shows a simple C# console app, ShowModule, that illustrates this. Figure 2 shows it running. ShowModule displays module information including the HINSTANCE for two types: the MyApp class defined in the application itself and the String[] (String array) type defined in mscorlib.
Figure 2 Module Information
- How do my .NET-based clients get the app's HINSTANCE to pass to my function?
- A Function to Do All My SQL Statements Based On All The Others Comments
- How to check the entitlements on my Application's Signature
- How do I get my old emails from Windows 98/Outlook 2000 to Win...
- How do I get my program to act like a daemon?
- My Core Exam failed, how to do it again?
- My suggestions to do estimation
- IT'S MY FIRST BLOG JUST TO DO TEST! HaHa
- How to launch App Store from my iPhone application?
- How to enable file sharing for my app?
- How do i prevent end users from connecting to the database other than my application?", version 8.1.5
- How to add -Xlint:unchecked to my Android Gradle based project?
- How to pass the value to the function when clicking on the UIButton
- How to install the pycharm in my ubuntu
- To do list in my lifetime
- I was eager to get my paddles wet and see how this kayak performed in the water.
- The poem to my darling~
- How to do SIP-based VoIP call?
- ECO(Enterprise Core Objects)的类框架图(较有用,鸟瞰重要类)
- Happy new year&NCL
- 快速启动Oracle数据库
- JUnit学习笔记之NetBeans篇
- 解决(播放此文件需要由格式标记 75 标识的音频编解码器)的问题
- How do my .NET-based clients get the app's HINSTANCE to pass to my function?
- C#字符串函数
- Hibernate 简化继承映射
- (存储过程)编写经验和优化措施
- Gif 文件格式分析 —— (转)
- 从晏才宏老师的凄凉到李开复讲座之“火爆”
- 我是你的爱么?
- 开始了……
- 怎样在vc环境下 调用direct x 的api呀