-1

I need to create one application that runs in the background.

I tried to hide it on startup: Me.Hide. It didn't work out, tho.

My question now is how do I make a VB.NET application that runs as a administrator and is hidden without the user knowing about it.

Thanks for your time.

BanForFun
  • 752
  • 4
  • 15
Rootel
  • 149
  • 16
  • 1
    Don't hide the form in the `Load` event, it hasn't even start showing by then. Subscribe to the `Shown` event and hide it there instead. -- But RoyalPotato's solution would be the very best one. – Visual Vincent Aug 03 '16 at 21:25
  • 1
    `how do I make a VB.NET application that runs as a administrator and is hidden without the user knowing about it.` - Trying to make a virus, ey? – Visual Vincent Aug 03 '16 at 21:32
  • 1
    lol, or maybe he just doesn't want to have to deal with those stupid prompts Windows pukes up all the time xD – RoyalPotato Aug 04 '16 at 06:00
  • 1
    Visual Vincent, no i'm not. That should be done with Kali Linux and not with Visual Basic. I'm making a background worker for my xData project. – Rootel Aug 04 '16 at 14:49

1 Answers1

3

I assume you're using Windows Forms since you said "I tried to hide it on startup, Me.Hide. Didn't work out, tho."

It's pretty simple, actually. Don't show the form until you want to. To do this, I'd disable the Enable Application Framework option in the project properties. (See https://msdn.microsoft.com/en-us/library/tzdks800.aspx, the option I mentioned is there somewhere.).

Then, create a new class (or you can use a module) in your project and name it "Program". The name isn't really important, but by convention it's usually "Program".

Create the famous "Main" procedure within this class. There are a few available signatures for this function.

  1. The simplest is as simple as: Public Shared Sub Main()
  2. Alternatively, you can receive command line arguments: Public Shared Sub Main(ByVal args() As String)
  3. Repeat 1, but as a function returning a 32-bit signed-integer.
  4. Repeat 2, but as a function returning a 32-bit signed-integer.

The point of returning an integer on the end of the 'Main' functions is to return an exit code specifying if everything went "OK". You generally return 0 if everything worked and an error-code or something to specify an error occurred.

Set the project's "Startup Object" to your new class.

Finally, if you ever want to show a form simply instantiate an object whose type is your form and show it.

Dim form As New Form1() form.Show()

And as always, dispose the form when you're done with it.

RoyalPotato
  • 515
  • 4
  • 15
  • 2
    If you wouldn't have written this so early I would have :). This is the best solution IMO since it still lets your application interact with the user's desktop (one important difference between this and a Windows Service). -- A little note however: when showing the first form you should call `Application.Run(New Form1)` instead so that your application will enter a message loop (or else it will just exit, I think). – Visual Vincent Aug 03 '16 at 21:29
  • 1
    Ah yes, I totally forgot about Application.Run! Everybody, do as Visual Vincent says and call Application.Run when you show the first form. Note though that (and I'm pretty sure that Visual Vincent just did it as an example) you may or may not want to actually create the form object as a parameter. If you do, then you can't explicitly dispose it. Which may or may not be important to you depending on how much control over this kind of thing you want/need/are-used-to. – RoyalPotato Aug 04 '16 at 05:57
  • 1
    Ah, thanks. But do you also know how to let it run as an administrator. I would love to know! You're my hero ;) – Rootel Aug 04 '16 at 14:55
  • 2
    @Rootel : Start the application at login via the Task Scheduler. This has been asked before: http://stackoverflow.com/questions/5127375/running-program-as-administrator-at-startup – Visual Vincent Aug 04 '16 at 21:51