Houri S Houri S - 2 months ago 48
C# Question

how to use idman 1.0 type library in c#

i've been making an automated .dlc url file decrypting then unrestrecting using real-debrid servers, the last stage is to pass an array of direct links to idm(internet download manager) which is a known software that has an api from
http://www.internetdownloadmanager.com/support/idm_api.html
here's my testing code:

IDManLib.ICIDMLinkTransmitter2 IDM;
string[][] plink = new string[3][];
for (int i = 0; i < plink.Length; i++)
{
plink[i] = new string[4];
plink[i][0] = url[i];
plink[i][1] = null;
plink[i][2] = null;
plink[i][3] = null;
}
plink[0][3] = "user-Agent";
IDM.SendLinksArray("location", plink);


now that you can add idm 1.0 type library either from references com tab, or you can build a new dll with type library importer which is irrelevant.

also assume that url array has 3 valid links, the nulls are a must for the API, from there site.

the problem i'm facing is that i'm always getting null reference exception, since IDM is not set to an object.
so is there a way to initialize the interface somehow or am i doing something wrong using the API ?

EDIT

thanks to Virgil i was able to finish my code, here's the most important part of it for anyone interested:

namespace RealDebridDL
{
public partial class Form1 : Form
{
public Form1()//construcot, read last settings
{
InitializeComponent();
string tok = "";
try
{
tok = File.ReadAllText("APITOKEN.ini");//api token from https://real-debrid.com/apitoken
}
catch (Exception)
{
}
if (tok != "")
{
textBox1.Text = tok;
}
tok = "";
try
{
tok = File.ReadAllText("loc.ini"); //file constains last location files dowloaded to with idm
}
catch (Exception)
{
}
if (tok != "")
{
textBox2.Text = tok;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)//save settings
{
File.WriteAllText("APITOKEN.ini", textBox1.Text);
File.WriteAllText("loc.ini", textBox2.Text);
}
public string decryptContainer(string dlc_content)//send request and get responce to decrypt the content of a dlc file
{
using (var client = new WebClient())
{
byte[] result = client.UploadValues("http://dcrypt.it/decrypt/paste", new System.Collections.Specialized.NameValueCollection { { "content", dlc_content } });
return Encoding.UTF8.GetString(result);
}
}
private async void Form1_DragDrop(object sender, DragEventArgs e)//dragged dlc files
{
button1.Enabled = false;
var b = (string[])e.Data.GetData(DataFormats.FileDrop, false);
for (int i = 0; i < b.Length; i++)
{
using (var client = new HttpClient())
{
var respo = decryptContainer(File.ReadAllText(b[i]));
string tempo = "";
for (int z = respo.IndexOf('[') + 1; z < respo.IndexOf(']'); z++)
{
tempo += respo[z];
}
respo = tempo.Trim();
respo = respo.Replace(",", "");
respo = respo.Replace("\"", "");
respo = respo.Replace("n.A.", "");
List<string> links = new List<string>();
if (respo.EndsWith("%"))
{
links = respo.Split('%').ToList();
for (int z = 0; z < links.Count; z++)
{
links[z] = links[z].Replace("%", "");
}

}
else
{
links = respo.Split(Environment.NewLine.ToCharArray()).ToList();
}
for (int j = 0; j < links.Count; j++)
{

using (var clienti = new HttpClient())
{
var values = new Dictionary<string, string> { { "link", links[j].Trim() }, { "", "" } };
var content = new FormUrlEncodedContent(values);
var responseString = await (await clienti.PostAsync(getAPIurl("unrestrict/link"), content)).Content.ReadAsStringAsync();
responseString = responseString.Replace("{", "");
responseString = responseString.Replace("}", "");
var f = responseString.Split(',');
for (int c = 0; c < f.Length; c++)
{
if (f[c].Contains("\"download\": "))
{
f[c] = f[c].Replace("\"download\": \"", "");
f[c] = f[c].Replace("\\", "");
f[c] = f[c].Replace("\"", "");
f[c] = f[c].Replace(",", "");
f[c] = f[c].Trim();
addLinktoIDM(f[c], textBox2.Text, f[c].Substring(f[c].LastIndexOf('/')));
listBox1.Items.Add(f[c]);
autoscroll();
}
}
}


}

}
listBox1.Items.Add(separator);
autoscroll();
}
startIDMschedual();
button1.Enabled = true;
}

private void copyall()
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
string cpy = "";
for (int m = 0; m < listBox1.Items.Count; m++)
{
if (listBox1.Items[m].ToString() != separator)
{
cpy += listBox1.Items[m].ToString() + Environment.NewLine;
}
}
if (cpy != "")
{
Clipboard.SetText(cpy);
}
}
}

const string separator = "... new file :)";
const string baseapi = "https://api.real-debrid.com/rest/1.0/";
string getAPIurl(string method)
{
return baseapi + method + "?auth_token=" + textBox1.Text;

}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private async void button1_Click(object sender, EventArgs e)//paste url link from clipboard sperated by newline
{
button1.Enabled = false;
if (Clipboard.ContainsText())
{
var links = Clipboard.GetText().Split(Environment.NewLine.ToCharArray());
for (int j = 0; j < links.Length; j++)
{
links[j] = links[j].Trim();
using (var clienti = new HttpClient())
{
var values = new Dictionary<string, string> { { "link", links[j] }, { "", "" } };
var content = new FormUrlEncodedContent(values);
var responseString = await (await clienti.PostAsync(getAPIurl("unrestrict/link"), content)).Content.ReadAsStringAsync();
responseString = responseString.Replace("{", "");
responseString = responseString.Replace("}", "");
var f = responseString.Split(',');
for (int c = 0; c < f.Length; c++)
{
if (f[c].Contains("\"download\": "))
{
f[c] = f[c].Replace("\"download\": \"", "");
f[c] = f[c].Replace("\\", "");
f[c] = f[c].Replace("\"", "");
f[c] = f[c].Replace(",", "");
f[c] = f[c].Trim();
addLinktoIDM(f[c], textBox2.Text, f[c].Substring(f[c].LastIndexOf('/')));
listBox1.Items.Add(f[c]);
autoscroll();
}
}
}
}
listBox1.Items.Add(separator);
autoscroll();
}
button1.Enabled = true;
startIDMschedual();

}
void autoscroll()
{
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;
}
void startIDMschedual()
{
Process.Start(new ProcessStartInfo(Process.GetProcessesByName("IDMan").FirstOrDefault().MainModule.FileName,
"/s ")).Dispose();
}
void addLinktoIDM(string link, string dir, string filename)
{
Process.Start(new ProcessStartInfo(Process.GetProcessesByName("IDMan").FirstOrDefault().MainModule.FileName,
$"/a /n /d {link} /p {dir} /f {filename}")).Dispose();

}
}
}


some may say y not use real-debrid decryptor instead, i tried it an it's unreliable as it doesn't always responce with links ,also has some kind of delay

another very important thing is that my code is very messy and doesn't take into consideration any special cases (ie: idm not running).

Answer

internet download manager supports CommandLine arguments, meaning you can add links, and download files through the program if you have the application installed and location of the executable is known.

You may start IDM from the command line using the following parameters

idman /s

or idman /d URL [/p local_path] [/f local_file_name] [/q] [/h][/n] [/a] Parameters:

/d URL - downloads a file

e.g. IDMan.exe /d "http://www.internetdownloadmanager.com/path/FileName.zip"

/s - starts queue in scheduler

/p local_path - defines the local path where to save the file

/f local_file_name - defines the local file name to save the file

/q - IDM will exit after the successful downloading. This parameter works only for the first copy

/h - IDM will hang up your connection after the successful downloading

/n - turns on the silent mode when IDM doesn't ask any questions

/a - add a file specified with /d to download queue, but don't start downloading

Parameters /a, /h, /n, /q, /f local_file_name, /p local_path work only if you specified the file to download with /d URL

Examples

C:>idman.exe /n /d http://www.tonec.com/download/idman317.exe

Source

in order to do that in c# you have to start a process for IDM executable and pass the arguments as explained in command line webpage of the internet download manager :

// idman /d URL [/p local_path] [/f local_file_name] [/q] [/h][/n] [/a]
var p = new Process();
p.StartInfo = new ProcessStartInfo
  {
  Arguments = $" /n /d {yourDownloadLink} /p {Environment.CurrentDirectory} /f nameOfYourFile.zip /q  ",
  WorkingDirectory = @"D:\_installed Apps\Internet Download Manager",
  FileName = @"D:\_installed Apps\Internet Download Manager\idman.exe"
  };
p.Start();
p.WaitForExit();// in order to wait for the item to complete before your code proceeds, i used that because i made idm exit after this download with /q
Comments