C# 等效于 C (WinAPI) 中的 DllMain [英] C# equivalent of DllMain in C (WinAPI)

查看:23
本文介绍了C# 等效于 C (WinAPI) 中的 DllMain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个较旧的应用程序(大约 2005 年),它接受 dll 插件.该应用程序最初是为 Win32 C 插件设计的,但我有一个可用的 C# dll 模板.我的问题:我需要做一些一次性初始化,这在 Win32 C dll 中会在 DllMain 中完成:

I have an older app (ca. 2005) which accepts dll plugins. The app was originally designed for Win32 C plugins, but I have a working C# dll template. My problem: I need to do some one-time initialization, which in a Win32 C dll would be done in DllMain:

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
  [one-time stuff here...]
}

是否有与此等效的 C#?我的 C# 模板中没有DllMain".我尝试了文字 C# 解释,但没有成功:dll 可以工作,但不会触发 DllMain 函数.

Is there a C# equivalent of this? There is no "DllMain" in the C# template I have. I tried a literal C# interpretation, but no go: the dll works but it won't trigger the DllMain function.

public static bool DllMain(int hModule, int reason, IntPtr lpReserved) {
  [one time stuff here...]
}

推荐答案

给你的类一个静态构造函数并在那里进行初始化.它将在任何人第一次调用类的静态方法或属性或构造类的实例时运行.

Give your class a static constructor and do your initialization there. It will run the first time anybody calls a static method or property of your class or constructs an instance of your class.

这篇关于C# 等效于 C (WinAPI) 中的 DllMain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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