判断主程序是Windows应用程序还是控制台应用程序
来源:互联网 发布:机械战警玩具编程 编辑:程序博客网 时间:2024/06/08 18:30
判断主程序是Windows应用程序还是控制台应用程序
标签: windowsapplicationwinformclassfunctionobject
2007-11-19 19:21 1997人阅读 评论(0)收藏举报
本文章已收录于:
分类:
作者同类文章X
来源:水如烟 - CSDNBlog
我们知道,桌面应用程序一般有两种类型,一是Windows应用程序,二是控制台应用程序。
如何判断调用自己所在Dll的主程序是Windows还是控制台应用程序呢?
解决这个问题,基点是ApplicationBase,ConsoleApplicationBase和WindowsFormsApplicationBase,
攻击的地方,当然是Application了。
我也曾想到能否从Thread.CurrentContext,Thread.CurrentThread,AppDomain.CurrentDomain来切入,不过没有去看。现在想“当然”的Application,有点武断,况且,Application源于System.Windows.Forms空间,用它来判断ConsoleApplicationBase,总觉得有些刺。
据自己的测试,主程序的有三种体现方式,如下:
Public Enum ApplicationType
WindowsForms '这是Form启动的Windows应用程序
ConsoleForms '这是Main启动且含代码Application.Run(New Form)的控制台应用程序
Console '这是Main启动无窗体的控制台应用程序,就算是Form.ShowDialog也列于此项
End Enum
MyApplicationBase.vb
Imports Microsoft.VisualBasic.ApplicationServices
Namespace LzmTW.uSystem.uForms
Public Class MyApplicationBase
'System.Windows.Forms.Application+ThreadContext
Private Shared gThreadContextType As uReflection.TypeHelper
'Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase+WinFormsAppContext
Private Shared gWinFormsAppContextType As uReflection.TypeHelper
Private gAppContext As Object = Nothing 'ApplicationContext or WinFormsAppContext
Private gWindowsFormsApplicationBase As WindowsFormsApplicationBase = Nothing
Private gCurrentType As ApplicationType
Shared Sub New()
gThreadContextType = New uReflection.TypeHelper(GetType(Application), "ThreadContext", True)
gWinFormsAppContextType = New uReflection.TypeHelper(GetType(WindowsFormsApplicationBase), "WinFormsAppContext", True)
End Sub
Sub New()
GetApplicationContext()
GetWindowsFormsApplicationBase()
gCurrentType = GetApplcationType()
End Sub
Private Sub GetApplicationContext()
Dim mCurrentThreadContext As Object
mCurrentThreadContext = gThreadContextType.MethodInvoke("FromCurrent")
gThreadContextType.SetCurrentObj(mCurrentThreadContext)
gAppContext = gThreadContextType.GetMemberValue("ApplicationContext")
End Sub
Private Sub GetWindowsFormsApplicationBase()
If Not gWinFormsAppContextType.CurrentType.IsInstanceOfType(gAppContext) Then Return
gWinFormsAppContextType.SetCurrentObj(gAppContext)
gWindowsFormsApplicationBase = CType(gWinFormsAppContextType.GetMemberValue("m_App"), WindowsFormsApplicationBase)
End Sub
Private Function GetApplcationType() As ApplicationType
If gAppContext Is Nothing Then
Return ApplicationType.Console
End If
If gWindowsFormsApplicationBase Is Nothing Then
Return ApplicationType.ConsoleForms
End If
Return ApplicationType.WindowsForms
End Function
Public ReadOnly Property ApplicationContext() As ApplicationContext
Get
Return CType(gAppContext, ApplicationContext)
End Get
End Property
Public ReadOnly Property WindowsFormsApplicationBase() As WindowsFormsApplicationBase
Get
Return gWindowsFormsApplicationBase
End Get
End Property
Public ReadOnly Property CurrentType() As ApplicationType
Get
Return gCurrentType
End Get
End Property
Public Enum ApplicationType
WindowsForms
ConsoleForms
Console
End Enum
End Class
End Namespace
控制台的一个测试:
Public Class Program
Shared Sub Main()
Console.WriteLine((New LzmTW.uSystem.uForms.MyApplicationBase).CurrentType.ToString)
Application.Run(New Form1)
Console.WriteLine((New LzmTW.uSystem.uForms.MyApplicationBase).CurrentType.ToString)
Console.Read()
End Sub
End Class
而Form1中有一个按钮也来测试
Private Sub ButtonTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTest.Click
Console.WriteLine((New LzmTW.uSystem.uForms.MyApplicationBase).CurrentType.ToString)
End Sub
那么,显示的结果,先是Console,后是ConsoleForms,最后是Console。
MyApplicationBase有没有用?
这当然看自己的需求。
举例来说,如果有一个类需要输出信息,如是控制台应用程序,就输出到控制台,如是WinForm程序,就以Form的形式显示,
这个时候,就需要MyApplicationBase来判定的了。
另,如果是WinForm应用程序,MyApplicationBase.WindowsFormsApplicationBase其实就是My.Application,
这里有大量的信息供自己使用。
个人感觉,这个类可能还是蛮有用的。
我们知道,桌面应用程序一般有两种类型,一是Windows应用程序,二是控制台应用程序。
如何判断调用自己所在Dll的主程序是Windows还是控制台应用程序呢?
解决这个问题,基点是ApplicationBase,ConsoleApplicationBase和WindowsFormsApplicationBase,
攻击的地方,当然是Application了。
我也曾想到能否从Thread.CurrentContext,Thread.CurrentThread,AppDomain.CurrentDomain来切入,不过没有去看。现在想“当然”的Application,有点武断,况且,Application源于System.Windows.Forms空间,用它来判断ConsoleApplicationBase,总觉得有些刺。
据自己的测试,主程序的有三种体现方式,如下:
Public Enum ApplicationType
WindowsForms '这是Form启动的Windows应用程序
ConsoleForms '这是Main启动且含代码Application.Run(New Form)的控制台应用程序
Console '这是Main启动无窗体的控制台应用程序,就算是Form.ShowDialog也列于此项
End Enum
MyApplicationBase.vb
Imports Microsoft.VisualBasic.ApplicationServices
Namespace LzmTW.uSystem.uForms
Public Class MyApplicationBase
'System.Windows.Forms.Application+ThreadContext
Private Shared gThreadContextType As uReflection.TypeHelper
'Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase+WinFormsAppContext
Private Shared gWinFormsAppContextType As uReflection.TypeHelper
Private gAppContext As Object = Nothing 'ApplicationContext or WinFormsAppContext
Private gWindowsFormsApplicationBase As WindowsFormsApplicationBase = Nothing
Private gCurrentType As ApplicationType
Shared Sub New()
gThreadContextType = New uReflection.TypeHelper(GetType(Application), "ThreadContext", True)
gWinFormsAppContextType = New uReflection.TypeHelper(GetType(WindowsFormsApplicationBase), "WinFormsAppContext", True)
End Sub
Sub New()
GetApplicationContext()
GetWindowsFormsApplicationBase()
gCurrentType = GetApplcationType()
End Sub
Private Sub GetApplicationContext()
Dim mCurrentThreadContext As Object
mCurrentThreadContext = gThreadContextType.MethodInvoke("FromCurrent")
gThreadContextType.SetCurrentObj(mCurrentThreadContext)
gAppContext = gThreadContextType.GetMemberValue("ApplicationContext")
End Sub
Private Sub GetWindowsFormsApplicationBase()
If Not gWinFormsAppContextType.CurrentType.IsInstanceOfType(gAppContext) Then Return
gWinFormsAppContextType.SetCurrentObj(gAppContext)
gWindowsFormsApplicationBase = CType(gWinFormsAppContextType.GetMemberValue("m_App"), WindowsFormsApplicationBase)
End Sub
Private Function GetApplcationType() As ApplicationType
If gAppContext Is Nothing Then
Return ApplicationType.Console
End If
If gWindowsFormsApplicationBase Is Nothing Then
Return ApplicationType.ConsoleForms
End If
Return ApplicationType.WindowsForms
End Function
Public ReadOnly Property ApplicationContext() As ApplicationContext
Get
Return CType(gAppContext, ApplicationContext)
End Get
End Property
Public ReadOnly Property WindowsFormsApplicationBase() As WindowsFormsApplicationBase
Get
Return gWindowsFormsApplicationBase
End Get
End Property
Public ReadOnly Property CurrentType() As ApplicationType
Get
Return gCurrentType
End Get
End Property
Public Enum ApplicationType
WindowsForms
ConsoleForms
Console
End Enum
End Class
End Namespace
控制台的一个测试:
Public Class Program
Shared Sub Main()
Console.WriteLine((New LzmTW.uSystem.uForms.MyApplicationBase).CurrentType.ToString)
Application.Run(New Form1)
Console.WriteLine((New LzmTW.uSystem.uForms.MyApplicationBase).CurrentType.ToString)
Console.Read()
End Sub
End Class
而Form1中有一个按钮也来测试
Private Sub ButtonTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTest.Click
Console.WriteLine((New LzmTW.uSystem.uForms.MyApplicationBase).CurrentType.ToString)
End Sub
那么,显示的结果,先是Console,后是ConsoleForms,最后是Console。
MyApplicationBase有没有用?
这当然看自己的需求。
举例来说,如果有一个类需要输出信息,如是控制台应用程序,就输出到控制台,如是WinForm程序,就以Form的形式显示,
这个时候,就需要MyApplicationBase来判定的了。
另,如果是WinForm应用程序,MyApplicationBase.WindowsFormsApplicationBase其实就是My.Application,
这里有大量的信息供自己使用。
个人感觉,这个类可能还是蛮有用的。
- 顶
- 0
- 踩
- 0
- 上一篇强大的模板引擎开源软件NVelocity
- 下一篇IIS的各种身份验证详细测试
0 0
- HOW TO:判断主程序是Windows应用程序还是控制台应用程序
- 判断主程序是Windows应用程序还是控制台应用程序
- 判断主程序是Windows应用程序还是控制台应用程序
- android 判断应用程序是系统程序还是用户程序
- 判断应用程序是32位还是64位
- C#判断应用程序是x86还是x64,连接ACCESS数据库
- windows应用程序与控制台应用程序的区别
- android获取应用程序判断是系统程序还是应用程序并且判断在sd卡中还是内存中
- 关闭应用程序(主程序)(WPF)
- 关闭应用程序(主程序)(WPF)
- 控制台应用程序
- 控制台应用程序
- 控制台应用程序
- 判断当前应用程序处于前台还是后台
- 判断当前应用程序处于前台还是后台
- 判断当前应用程序处于前台还是后台
- Android如何判断应用程序是安装在手机内存中还是SD卡
- 判断应用程序.exe及动态链接库.dll是32位还是64位
- html2--列表的标记
- Genymotion模拟器运行白屏
- 数码时钟
- 一、大话设计模式 之 简单工厂模式
- 【详解】CSS hack大全
- 判断主程序是Windows应用程序还是控制台应用程序
- js中window.location.search的用法和作用。
- TopShelf-(02)服务-01.安装.bat
- Magento: Fatal error: Cannot override final method Mage_Core_Model_Abstract::clearInstance()
- Android 更改纯色背景图片颜色,可实现一张背景圆形图片展示不同颜色
- (LeetCode)First Bad Version --- 查找坏版本
- 要怎样努力,才能成为很厉害的人
- SAP部分支付和剩余支付清账的区别
- Android6.0_r9 CTS 测试 二