Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Using Window's URI Scheme.

Hi,

I am going to show you how get advantage of Window's URI scheme so you can easily start your application from a website or pass arguments to it.
It's the same method that torrent clients use to open Magnet links, skype and Battlefield.

Here is an example:
[Image: z8ot2mR.gif]

Skills required:
Basic Knowledge of vb.NET
Know how to open the windows registry and how it works.
Some HTML knowledge.

Dificulty level: 3/10?



So, let's start:

First we have to create a key in the registry.
Open the start menu -> type regedit
Create:
Code
HKEY_CLASSES_ROOT -> yourcommand -> shell -> open -> command
and change the default value of command into this:
Code
"C:\yourappsdir\app.exe" "%1"
You can create this key in setup or when the user starts the application for first time.
>Have a look Here on how to create Registry Keys with VB.net
This is how the registry key should look like:
[Image: NP5jPhZ.png]

As you probably understood the URI scheme requires an executable location.
This means if your application is portable you should change the location of the registry each time you start your application.



Next, the website files.
Nothing special, some simple HTML tags and a javascript function:
Code:
PHP Code

<?php 
<script type='text/javascript'>
      function 
SendData(data) {
      
command = 'cCommand:'
      
redirecturl = command+data;
      
window.open(redirecturl,"_self")
      }
</
script>

<
input type='text' name='text' id='text' />
<
input type='button' value='Send Data' onclick='javascript:SendData("-text="+ document.getElementById("text").value);' />
<
input type='button' value='Clear Textbox' onclick='javascript:SendData("-clear");' /> 

I have no idea of web programming, searching for sources on the web made it possible.
Upload this to your webserver and you are ready to test it.




Now the application:

First, make your application single instance:
Go to Project Properties -> Application -> check Make single instance application
[Image: DbxRAKt.png]

This will prevent your application from opening multiple times.

Then for this tutorial just add a textbox into your main form,
[Image: KXNkdbB.png]
and this into the code:
Code
Imports Microsoft.VisualBasic.ApplicationServices

Public Class frmMain

    'Check for Arguments after the application has started.
    Public Sub app_NextInstance(ByVal sender As Object, ByVal e As ApplicationServices.StartupNextInstanceEventArgs)
      CheckForArguments(cCollectionToArray(e.CommandLine))
    End Sub
    'Check for Arguements on start up.
    Private Sub app_StartUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      CheckForArguments(Environment.GetCommandLineArgs)
    End Sub
    'Here you can include whatever u like, it's simple to check for arguments and parse it's data.
    Sub CheckForArguments(ByVal Arguments As String())
      For Each Argument As String In Arguments
      If Argument.Contains("-text=") Then
      TextBox1.AppendText(Argument.Split("=")(1) & vbNewLine)
      ElseIf Argument.Contains("-clear") Then
      TextBox1.Clear()
      End If
      Next
    End Sub
    'Just a simple function that converts a collection of string into an array of strings, nothing to worry
    Private Function cCollectionToArray(ByVal Collection As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As String()
      Dim Array(Collection.Count - 1) As String
      For i As Integer = 0 To (Collection.Count - 1)
      Array(i) = Collection.Item(i)
      Next
      Return Array
    End Function
End Class

Next,
Go to Project Properties -> Application -> View Application Events

and add a new sub:
Code
Sub App_NextInstance(ByVal sender As Object, ByVal e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
      frmMain.app_NextInstance(sender, e)
      End Sub
[Image: iKuAOQ1.png]
And you are done.

If you haven't understand what's going on until now I will explain you:
What we do is register a special(?) command into the registry that allows us to start and parse arguments to our application from websites, or other applications.

You can also use CMD to do the same:
example:
[Image: aY8EaEO.png]

More links:
Download The Project Here: yet unavailable
How to use command lines
URI Scheme on MSDN
How to create registry keys within vb apps
Just a heads up for anyone who plans on using this, it will only work if you have the MicroSoft.VisaulBasic namespace imported (Horrible practice, in any language other then VB), and won't work in WPF Based applications (It does not have the SingleInstance option you use)

The alternative to this, is to use the same Registry with NamedPipeStreams (If using a language outside of VB), and Mutex (If using WPF). I've previously posted an example of both, using Threading to make sure reading the new arguments does not freeze the application. For anyone interested: hackforums.net/showthread.php?tid=4470491

Also, just a hint: The registry stuff should be done upon installation, not runtime.

Besides the reliance on the Microsoft.VisualBasic namespace, good tutorial!
This is pretty cool!
I always wanted to run my game from my website kinda like other games out there, and now I can.
(Nov 13, 2014 04:56 AM)Nikey646 Wrote:  Just a heads up for anyone who plans on using this, it will only work if you have the MicroSoft.VisaulBasic namespace imported (Horrible practice, in any language other then VB), and won't work in WPF Based applications (It does not have the SingleInstance option you use)

The alternative to this, is to use the same Registry with NamedPipeStreams (If using a language outside of VB), and Mutex (If using WPF). I've previously posted an example of both, using Threading to make sure reading the new arguments does not freeze the application. For anyone interested: hackforums.net/showthread.php?tid=4470491

Also, just a hint: The registry stuff should be done upon installation, not runtime.

Besides the reliance on the Microsoft.VisualBasic namespace, good tutorial!

Nice addition.
Yeah, I mentioned you could create the registry key during installation or first run.
All I wanted to do in this tut is to pass the method and the concept.
Great tutorial. Finally something unique that I haven't seen posted. Good work.
Damn. Great tutorial, OP.
Nice to see you contributing.

Expect green apples from me, when I can. 🤝


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by simmerFeb 18, 2022 11:18 AM 0 74 Feb 18, 2022 11:18 AM
Last Post: simmer
[.NET] by aGFjaw--Oct 31, 2017 09:50 AM 18 483 Oct 31, 2017 09:50 AM
Last Post: Dofeus
[C#] by ImmortalFeb 23, 2016 04:26 AM 3 301 Feb 23, 2016 04:26 AM
Last Post: BahNahNah