[英]Determine whether the class library is called from a windows based application
This is based on C#. 这是基于C#的。 I am having a classlibrary in which I would like to know specifically if its being called from a windows based application
我在一个图书馆中,我想具体了解它是否从基于Windows的应用程序中调用
I searched a lot but all I found was code to know if it was called from a Console application. 我进行了很多搜索,但发现的只是代码,以了解是否从控制台应用程序调用了它。 I only want to know if its called from a windows based application or not
我只想知道它是否从基于Windows的应用程序中调用
Please Help 请帮忙
If you want to know whether the application is running as a service or system app and thus cannot display a dialogue, test the state of Environment.UserInteractive. 如果您想知道该应用程序是作为服务运行还是作为系统应用程序运行,因此无法显示对话,请测试Environment.UserInteractive的状态。
There is no easy way to determine if the application is a windows or cmd-based application. 没有简单的方法来确定应用程序是Windows还是基于cmd的应用程序。 For example, I might build a winforms-based application that doesn't create a window and instead runs as command-line style application by not opening a window.
例如,我可能会构建一个基于Winforms的应用程序,该应用程序不会创建窗口,而是通过不打开窗口而作为命令行样式的应用程序运行。
Two (not necessarily reliable) ways of testing if it's a windows-based app would be to test Application.OpenForms (if it contains forms, then it's a windows app) or Environment.GetCommandLineArgs() (if 0 args, assume it's a windows app). 两种(不一定可靠)测试它是否为基于Windows的应用程序的方法是测试Application.OpenForms(如果包含表单,则为Windows应用程序)或Environment.GetCommandLineArgs()(如果args为0,则假定它为Windows。应用)。
If you absolutely need to know whether the app is a console or windows one, then you need to step out of the world of managed code and start delving into the exe file using unmanaged C++ code. 如果您绝对需要知道该应用程序是控制台还是Windows,那么您需要走出托管代码领域,并开始使用非托管C ++代码深入研究exe文件。 There is just one byte difference between the two in the PE header of the exe file: Subsystem is set to 2 for a windows app and 3 for a cmd app.
exe文件的PE标头中的两个字节之间只有一个字节的差异:Windows应用程序的子系统设置为2,cmd应用程序的子系统设置为3。 See http://msdn.microsoft.com/en-us/library/ms809762.aspx and http://support.microsoft.com/kb/90493/en-us for details if you really want to do this.
如果您确实要执行此操作,请参阅http://msdn.microsoft.com/zh-cn/library/ms809762.aspx和http://support.microsoft.com/kb/90493/zh-cn了解详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.