1

I have an application which relies on being called by a custom URL protocol; for reference see this post. The registry link works, but when I try to catch passed parameters from the URL (e.g. I launch customurl://param1=xy&param2=xy in a browser) I seem to fail it with the code below;

Program.cs

static void Main(string[] args) {
    [...]
    Application.Run(new Form1(args));
} 

Form1.cs

public Form1(string[] args) {
    [...]
    if (args.Length > 0) {
        string name = args[0];
        label1.Text = "received paramter: " + name;
    } else {
        label1.Text = "no received parameter!";
    }
}

The condition always chooses the else branch, which means the args[] array contained none of the passed parameters. What am I doing wrong? Is there another specific method to catch parameters given these conditions?

EKOlog
  • 432
  • 7
  • 19
Viktor
  • 135
  • 11
  • Not sure if this is a typo, but you seem to have forgotten to denote the start of the GET query using `?`. Your URL should be `customurl://?param1=xy&param2=xy` – thebugsdontwork Oct 27 '21 at 14:35
  • @ogjtech it was not a typo, but the result is the same unfortunately – Viktor Oct 27 '21 at 14:37
  • 1
    Did you include the `"%1"` in the registry which is the placeholder for the URL? Here is another SE post with similar instructions [How do I register a custom URL protocol in Windows?](https://stackoverflow.com/questions/80650/how-do-i-register-a-custom-url-protocol-in-windows) – Stephen Ostermiller Oct 29 '21 at 20:13
  • Try taking separate variable instead of args array in forms1 method as parameter and make sure your url looks like `customurl?param1=xy&param2=xy` – Shafiqul Bari Sadman Oct 27 '21 at 14:30
  • @StephenOstermiller, that was exactly the mistake I did :) If you repost this as an answer, I'll accept it as correct. – Viktor Nov 02 '21 at 10:17

1 Answers1

1

The behavior you observe can be caused by forgetting the "%1" in the registry. "%1" is a placeholder for the URL. If you forget to include it, the URL won't get passed to your handler.

Here is another SE post with similar instructions How do I register a custom URL protocol in Windows? It shows you exactly where you need to put the "%1".

Stephen Ostermiller
  • 21,408
  • 12
  • 81
  • 104