C#相当于在的DllMain C(WinAPI的)的 [英] C# equivalent of DllMain in C (WinAPI)

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

问题描述

我有一个旧的应用程序(约2005年),它接受DLL插件。该应用程序最初被设计为Win32Ç插件,但我有一个工作C#DLL模板。我的问题:我需要做一些一次性初始化,这在Win32 C DLL将在DllMain中进行:

  BOOL APIENTRY的DllMain(HANDLE HMODULE,DWORD ul_reason_for_call,LPVOID升preserved){
  [一次性的东西在这里...]
}

有一个C#相当于呢?没有的DllMain在C#模板我。我尝试了字面C#间pretation,但不走:该DLL的作品,但它不会触发DllMain函数

 公共静态布尔的DllMain(INT HMODULE,诠释原因,IntPtr的升preserved){
  [一次性的东西在这里...]
}


解决方案

给你的类静态构造函数,做你的初始化那里。它将运行在第一时间有人称之为类的静态方法或属性或构造你的类的实例。

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...]
}

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#相当于在的DllMain C(WinAPI的)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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