简体   繁体   English

VB.NET在主exe打开时强制'启动'应用程序?

[英]VB.NET Forcing 'launcher' app when main exe is opened?

I was trying to figure out a way to have a launcher/updater app for a program I'm working on. 我试图找到一种方法来为我正在进行的程序安装启动器/更新器应用程序。 It'll have people register, and not let them run the main program without registering. 它会让人注册,而不是让他们在没有注册的情况下运行主程序。 So, say, I have launcher.exe, and main.exe, and I open main.exe. 所以,比方说,我有launcher.exe和main.exe,我打开main.exe。 I need it to exit and open launcher. 我需要它退出并打开启动器。 I was going to use sockets to do a simple ping/pong type deal, but sockets are breaking my face in vb.net since I've only used them in vb6. 我打算用套接字做一个简单的ping / pong类型的交易,但是套接字在vb.net中打破了我的脸,因为我只在vb6中使用它们。

Sorry for the wall of text :) 对不起,文字墙:)

I'd suggest using a "secret" command-line parameter. 我建议使用“秘密”命令行参数。

When Launcher starts Main, it would execute 当Launcher启动Main时,它将执行

C:\\Path\\Main -Launched

If the user double-clicks on Main.exe (or types "Main" at a DOS prompt), the command-line parameter won't be seen. 如果用户双击Main.exe(或在DOS提示符下键入“Main”),则不会看到命令行参数。 So in Main, if the command line doesn't have the -Launched parameter, it starts Launcher and then exits. 所以在Main中,如果命令行没有-Launched参数,它会启动Launcher然后退出。 If it DOES have the -Launched parameter, you can assume that it was started by Launcher application and continue. 如果它具有-Launched参数,您可以假设它是由Launcher应用程序启动并继续。


The only drawback to this is that a hacker could rename Main.exe to Main2.exe, and then install their own version of Main.exe which displays the command line and then launches Main2. 唯一的缺点是黑客可以将Main.exe重命名为Main2.exe,然后安装自己版本的Main.exe,显示命令行,然后启动Main2。 This would allow them to see the "secret" command-line parameter. 这将允许他们看到“秘密”命令行参数。 Even most hackers wouldn't bother with this, though, and non-hackers would never see the parameter. 但是,即便是大多数黑客都不会为此烦恼,而非黑客也永远不会看到这个参数。 But if you're worried about this, you can do this instead: 但如果你担心这个,你可以这样做:

(Note: Not tested, there may be minor typographical errors here.) (注意:未经测试,此处可能存在轻微的印刷错误。)

' In Launcher:
' ...
Dim Param as string = Now.ToString("MMddhhmmss").GetHashCode.ToString
' Now launch Main, using Param as the command-line parameter
' ...


' Somewhere in Main, probably in the main form's Form1_Load:
' ...
Dim H As Integer = 0
Integer.TryParse(Command, H)

Dim T As Date = Now
Dim Launched As Boolean = False
For I As Integer = 0 To 30
    Dim X As Integer = T.AddSeconds(-I).ToString("MMddhhmmss").GetHashCode
    If X = H Then
        Launched = True
        Exit For
    End If
Next I

If Not Launched Then
    ' Assume that user launched program directly
    ' Start Launcher, then exit
End If

' The program was started from Launcher. Continue.
' ...

This code assumes that when Launcher starts Main, Main's code starts executing within 30 seconds. 此代码假定当Launcher启动Main时,Main的代码在30秒内开始执行。 (If that proves false, you might have to increase the maximum value in the I loop.) (如果证明是错误的,则可能必须增加I循环中的最大值。)

I'd reverse the whole thing. 我会扭转整个事情。 Rather than starting up your main program and have it play switching games with your updater, assign your "main" icon and program name to the updater and have it check and update the real main app at startup. 而不是启动您的主程序并让它与您的更新程序一起玩切换游戏,将您的“主”图标和程序名称分配给更新程序,并让它在启动时检查并更新真正的主应用程序。 Then it will start up your real app before exiting. 然后它将在退出之前启动您的真实应用程序。 This way there's only one switch instead of two. 这样只有一个开关而不是两个。

Savy users will be able to find and run your real .exe file directly, but ofr the vast majority it will just be another file hidden away in your program files folder. Savy用户将能够直接找到并运行您的真实.exe文件,但绝大多数情况下,它只是隐藏在程序文件文件夹中的另一个文件。

Another option is make your main program a dll (class library project) instead of a .exe file, and load it dynamically after startup using the Assembly.LoadFile() function. 另一个选择是使您的主程序成为一个DLL(类库项目)而不是.exe文件,并在启动后使用Assembly.LoadFile()函数动态加载它。 This is safe as long as your entry point into that dll remains consistent, and makes it harder for users to just run your main program directly without using your updater. 只要您进入该dll的入口点保持一致,并且使用户更难直接运行主程序而不使用更新程序,这是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM