开发者

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 h开发者_如何学运维ave 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. 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.

Sorry for the wall of text :)


I'd suggest using a "secret" command-line parameter.

When Launcher starts Main, it would execute

    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. So in Main, if the command line doesn't have the -Launched parameter, it starts Launcher and then exits. If it DOES have the -Launched parameter, you can assume that it was started by Launcher application and continue.


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. 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. (If that proves false, you might have to increase the maximum value in the I loop.)


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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜