C# .NET plugin fails when dragging it to an IA script

Interested in developing new plug-ins? Got one to share? Post here!
erich.einfalt
Posts: 32
Joined: Mon Oct 29, 2012 2:27 pm

C# .NET plugin fails when dragging it to an IA script

Postby erich.einfalt » Tue Feb 23, 2016 3:59 pm

I have successfully compiled a .NET IA plug-in in C# named "REST Get (JSON)" using the .Net Plug-In Wizard template, including its installer that registers it in the registry. The Plug-In is listed in the Plug-Ins list (so I know the plug-in's registration in its installer works) in the IA IDE launched with a test IA project. When I drag the Plug-In to a point in the IA script, the following error message box is displayed:

PlugInFailureCapture.PNG
PlugInFailureCapture.PNG (35.14 KiB) Viewed 34930 times

This would imply that something has not been initialized but I can't seem to find where the error in my code is. Before I fatten up this post with my source code, has anyone seen this behavior before?

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: C# .NET plugin fails when dragging it to an IA script

Postby bokkie » Tue Feb 23, 2016 4:48 pm

Erich,

Attached is a sample plugin I wrote. It returns (if it can find one) the MAC address of the machine it's being run on. The attachment contains a VS2012 solution. Open it and compile it. The last time I checked, it ran okay in VS2010 through VS2015CE. After compiling, close the solution. The attachment contains an IA project. Open it in the IA IDE. Build the media and install it. It will install and register the plugin for use in your version of IA. Create a new project in IA. Go to the plugin list in the MSIcode window. Drag and drop the MAC address plugin onto your MSIcode window. It should appear in the MSIcode window without a problem, meaning, it gets past an access violation error like you report.

A long time I also experienced the same error message but for the life of me I cannot recall what I did to fix it. If my plugin works okay on your machine you'll be able to compare what you're doing to what mine does. It may help you to get past or at least point a finger at your problem. If you look in my plugin's source you'll see a reference to a build compiler conditional statement. If you enable it in your VS it will enable verbose error message boxes to help you trace where your problem is occuring. I added a number of, hopefully, useful comments in the source code.

Look at the screenshot I provided (CallingSequence.png). It shows you exactly what methods in the plugin are called when you do different things with it in IA. You might find it useful and it helps you visualise the calls made to your plugin.

Try this for me and see if it helps you to understand where yours might be failing. Post any additional questions here and I'll see if I can help you.
Attachments
CallingSequence.png
CallingSequence.png (58.68 KiB) Viewed 34925 times
MAC address plugin.7z
(926.37 KiB) Downloaded 1349 times
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

erich.einfalt
Posts: 32
Joined: Mon Oct 29, 2012 2:27 pm

Re: C# .NET plugin fails when dragging it to an IA script

Postby erich.einfalt » Wed Mar 02, 2016 4:06 pm

The MAC addresses .NET plug-in project you posted provided necessary insight into some of what I can only describe as 'vagaries' in IA Plug-In development using .NET. Thank you for that. However, none of the sample projects I have found for writing IA plug-ins (regardless of language) seem to include input variables and they all only one return variable

My original plug-in project design had one input variable (a REST service URL) and two return variables, one, JSON data, the other, HTTPStatusCode of the REST response. I have since simplified the project with a single return variable containing either the JSON data, (from the REST call that the plug-in was written to execute) if successful, or the HTTP Status code if unsuccessful. That eliminated one output variable.

Having refactored my plug-in project using your MAC addresses project as a pattern, the only thing that still does not seem to work as expected is getting the data from the plug-in output back into the variable defined in the IA script.

I wrote a 'test jig' (a C# .NET WinForms app) to emulate the calls from the bridge dlls and I can confirm that the data being returned by my plug-in is among the comma separated VARNAME,VARVALUE pairs in the ref variable 'updatedVariables' argument of the Accessory.Methods.RunTimeExecute() method, which if I understand all this correctly, should be pushed back into the IA script variable defined as the return variable in the plug-in call via the runtime bridge dll. In the IDE, I added an IA MessageBox to look at that return variable after the plug-in call and the return value of the return variable is always empty. What am I missing? Will provide source code snippets as requested.

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: C# .NET plugin fails when dragging it to an IA script

Postby bokkie » Thu Mar 03, 2016 12:38 pm

Erich,

Let me review my plugin and see if something slipped through untested although I don't think I coded anything and left it untested.

In the meantime when you wrote "However, none of the sample projects I have found for writing IA plug-ins (regardless of language) seem to include input variables and they all only one return variable". If I understand your question, you'd like your plugin to receive the name of the variable instead of having to enter it in the plugin's variable name textbox. Is that what you'd like to do? If it is, then unfortunately it's not possible to pass the variable name to the plugin. This was asked a while back and I think it was Francesco or the plugin's original author, Pawel, who confirmed that the plugin bridge can't do that.

As for returning more than one variable name I think it should be possible but I'll have to look at it for you. When the plugin returns the data values it does like you say it should, that is, it changes the data values by searching for the variable name in the list of key/value pairs and if it finds a variable matching the name in the plugin, it replaces the data value with the value the plugin dictates it should return. If you recall that plugin form, you'll recall or might have seen where you enter the name of the plugin variable and a label telling you to make sure you create the variable in the MSIcode before you call the plugin. In theory, you should be able to enter as many variable names as you need in your plugin's textboxes and then substitute the variable values with those names you entered in the plugin. I can't see why you couldn't use more than one variable.

I did test the plugin just now. For some reason, my MAC address is being returned as a null string which seems to endorse what you observed. Could I ask you to try something for me? In the plugin code have a look at that method that does the text replacement. Instead of replacing it with the assumed MAC address, replace it with a literal string such as "ABC123". If you display the variable in a message box, does it display the literal value? If it does, that might explain why you see a null value when it tries to get the MAC address. I will have to load the project later and see why it might return a null value for the MAC address. I'm sure we will get to the bottom of the problem but it's been a while since I last did any plugin development so I need to retrain myself as to what I did. But try my suggestions first and let's see if that helps to move things along. :)
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: C# .NET plugin fails when dragging it to an IA script

Postby bokkie » Fri Mar 04, 2016 6:28 am

Erich,

I can't explain what's going on. I had another plugin which I recompiled and installed which added it into IA's IDE. I can drag the plugin onto the MSIcode window and enter a name for its variable which I created immediately before the call to the plugin. I built the media and for some odd reason it doesn't contain any value when it returns. I originally developed my plugins in VS2010 and my machine now only contains the community edition of VS2015. So, at this time I have two plugins which used to work but now both don't return values! :o

Can you tell me what version of VS you're using? Unfortunately I can't go back to VS2010 again because there are too many side effects like having to install Resharper for example for use in VS2010. Another thing I tested is to create a new IA plugin template. I then compared the size of the assembly files the plugin bridge uses and they are still the same size as they were when I took them from version 18 so it's unlikely to be a bridge file problem. I hope our other plugin member, christ23, gets to read this as he's done a bit of plugin development himself.

CHRIST23: If you see this thread, can you tell us if any of your plugins are working okay? Right now, my suspicion suspects it could be a VS version problem but I can't say for certain.
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

erich.einfalt
Posts: 32
Joined: Mon Oct 29, 2012 2:27 pm

Re: C# .NET plugin fails when dragging it to an IA script

Postby erich.einfalt » Fri Mar 04, 2016 10:52 am

Thanks for the quick response. We are using VS2013 Professional but we're about to move to VS2015.

erich.einfalt
Posts: 32
Joined: Mon Oct 29, 2012 2:27 pm

Re: C# .NET plugin fails when dragging it to an IA script

Postby erich.einfalt » Fri Mar 04, 2016 12:24 pm

Well, this is interesting -- 2 Items of Note:

1. bokkie replied: "... if you'd like your plugin to receive the name of the variable instead of having to enter it in the plugin's variable name textbox. Is that what you'd like to do? If it is, then unfortunately it's not possible to pass the variable name to the plugin. This was asked a while back and I think it was Francesco or the plugin's original author, Pawel, who confirmed that the plugin bridge can't do that." Well, the plug-in bridge (provided by the IA X3 .NET Plugin wizard) IS allowing me to input a variable name (pre-populated with the data to pass to the plug-in) as well as the name for the output variable. When I examine the input variable value (as well as the output variable value) with an IA MessageBox in the IDE placed after the call to my plug-in call in the IA IDE script editor, the MessageBox shows the input variable value. When I look at the input variable (name & value) in my .NET test jig which examines what comes in from the bridge (or while stepping through my plug-in code in the VS debugger when invoked from running IA script), the input variable name and value is present and correct, so the bridge is passing the input variable name and value from the IA script to my plug-in.

2. Regarding the issue of the output not being returned to the output variable (as viewed in the post-plug-in call IA MessageBox), I tried your suggestion of replacing the output variable value setting with a known literal within my plug-in code and, while that value does show up in my test jig, I can confirm that it does not make it back into the output variable in the IA script. As I have mentioned, I am using VS2013 Professional.

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: C# .NET plugin fails when dragging it to an IA script

Postby bokkie » Fri Mar 04, 2016 12:52 pm

Erich,

I think I might have misled you slightly. When the plugin gets called for the first time the variables aren't passed through? At least I seem to recall that's the way it works. I think that's the reason why the variable names aren't automatically pushed through. In other words there are statements in IA which when you edit them in the MSIcode window all the variables names appear selectable. To pass those into a plugin in C# you might need another plugin that returns the variables names into a variable and then you can pass that into a C# plugin and then break them down into single names. As you can see the C# plugin might be convenient to some extent but it's not as comprehensive as those you write in Delphi or C++ which I think are the core languages used to write IA. I apologise if I've got that completely wrong.

When the same method is called the second time, the variables are passed in with their existing values and that's how I loop through them and replace the value, which is usually/sometimes/often/always (choose one or more :D) null, with the stored value the plugin wants to return. The thing that puzzles me is that it all used to work okay and you've since confirmed you're using VS2013. It's just weird why it used to work but now does not. I know for sure that christ23 and me both started off using VS2010 which is why his input could be valuable.

The next thing I need to try is debugging the plugin at runtime. I don't know if you've attempted that but it can be done. It involves using the JIT debugger. Look in that project called Accessory I gave you and switch (in VS) to the project's properties. Select the Build tab, and in the Conditional compilation symbols text you'll see something like this: dontENABLEJITDEBUGGING. Remove the "dont" prefix. In the Accessory solution look at the source code in Accessory.cs at method EnableJitDebugging. The code should now appear compilable and when it gets called the System.Diagnostics.Debugger.Launch() statement should now be visible as compilable code. Copy all your assemblies and pdb files into the same location where your plugin is installed. Set a breakpoint in VS where you want to stop at. Then, when IA calls the bridge into your plugin the JIT debugger should then open a selectable dialog and which allows you to attach to the assembly you want to break at. In theory, when the bridge runs through your plugin your attached debugging session in VS should hit your VS breakpoint and from there you should be able to dig a little deeper.

I know this JIT debugging also used to work because both me and christ23 confirmed it does/did. Whether anything has changed since I last tried it well over a year ago I'm not sure. I have a Word document in which I described it for my own benefit at the time so I'll have a go later and if it still works I'll cut it down to the bare bones and let you have a copy of it for reference. I'm sort of glad I've had a chance to look at the DotNet plugin development again. Unless you discover something in the meantime, it's going to take me a little time to retrain myself.

As I once mentioned somewhere before I'm at the age where I have the memory retention of a garden slug and the keyboard dexterity of a chicken pecking the dirt for grain. :D
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: C# .NET plugin fails when dragging it to an IA script

Postby bokkie » Sat Mar 05, 2016 9:01 am

Erich,

I found the problem in my plugin's code. Refer back to the VS solution I gave you. Open the Accessory project and open the Private.cs file. Replace the method DeconstructState near the bottom of the source file with this new one:

private static void DeconstructState(string statement, out string selection, out string variable)
{
if(string.IsNullOrEmpty(statement)) // The statement cannot be null.
{
selection = variable = string.Empty;
return;
}

// Locate the keywords and extract the substrings.

var i = statement.IndexOf("into", StringComparison.InvariantCulture);
selection = statement.Substring(4, i - 5).Trim();
i = statement.IndexOf("variable", StringComparison.InvariantCulture);
variable = statement.Substring(i + 9).Trim();
i = variable.Length;
if(variable.EndsWith("\""))
{
variable = variable.Substring(0, i - 1).Trim();
}
}

What did I find? The plugin wasn't finding the variable name it expected to find in the list of variables IA pushes into the plugin. The reason why is that on one call the plugin statement was coming in with double quotes at the beginning and end. As my plugin statement ends with the variable name, say XYZ, for example, after DeconstructState returned the variable name was XYZ", that is, the double-quote at the end was part of the variable name. When the plugin searched for IA's list of variable names there was XYZ but not XYZ". Once I removed the "offending" double-quote at the end my plugin now started to work okay. That explains why I was getting no value returned back to IA which is the same problem you had in your plugin? I hope that this explains how to fix your plugin but please understand that while it fixed my problem there could be something else in your plugin that might be causing it. Try my suggestion first as I think you changed your plugin to sort of replicate how mine is structured. Hopefully, your problem will also be fixed... :?:
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

erich.einfalt
Posts: 32
Joined: Mon Oct 29, 2012 2:27 pm

Re: C# .NET plugin fails when dragging it to an IA script

Postby erich.einfalt » Mon Mar 14, 2016 1:49 pm

Thanks for tearing apart your plug-in and finding this. I, too, found the trailing double-quote artifact in my plug-in but have opted to change my plug-in's behavior because the amount of data expected to be returned in the IA output variable is too large to display in a MessageBox and so I have no way to verify that it is working. Therefore, I have opted to change my plug-in's behavior to store that data in a temp file rather than storing it in the return variable. I will, however, incorporate your changes into any other plug-ins I write. Thanks again.

bokkie
Posts: 767
Joined: Sun Feb 08, 2009 6:30 am

Re: C# .NET plugin fails when dragging it to an IA script

Postby bokkie » Mon Mar 14, 2016 3:30 pm

Erich, I'm happy to read you found the same problem independently. I also noticed the variables and values don't fit comfortably in a message box and I too added a new method to append them to a log file because +/- 1400 bytes is unwieldly to look at on the screen. There is one useful thing about seeing all the variables and their values and that is it helps to see what default values propogate through the project. The internal/project variables that IA sets for you when the installation runs provide useful insights into what you can use elsewhere.
Peter. Smartly dressed, he still resembles an unmade bed.
InstallAware MVP

erich.einfalt
Posts: 32
Joined: Mon Oct 29, 2012 2:27 pm

Re: C# .NET plugin fails when dragging it to an IA script

Postby erich.einfalt » Wed Mar 16, 2016 12:52 am

I added logging functionality to capture (to a file in the User's %TEMP% folder) the arguments passed into the public methods of the Accessory project at the beginning and end of each method (so I can capture and compare the changes made to those arguments that are passed in by reference). What I found was that the RunTimeExecute() method is never getting called. I understand that RunTimeExecute() is only being called when the the installer that contains references to my plug-in is installing/uninstalling but my log file shows no entries for calls to RunTimeExecute(). In fact, when the installer that contains references to my plug-in runs, no log file entries are written at all. This behavior occur whether the installer that contains references to my plug-in is built as Uncompressed or Single Executable. Is this normal behavior?

My aforementioned test jig app provides a mock bridge RunTimeExecute() call and when the mock call is there, the plug-in executes as expected and all expected actions occur.

This would imply that either:
a. IA is not calling the Runtime bridge dll, or,
b, the Runtime bridge dll is not calling the plug-in.

The bridge dlls are those that were provided by the X3 (20.11Update) .Net Plugin Wizard.

Any thoughts?

erich.einfalt
Posts: 32
Joined: Mon Oct 29, 2012 2:27 pm

Re: C# .NET plugin fails when dragging it to an IA script

Postby erich.einfalt » Wed Mar 16, 2016 1:34 am

Disregard my post dated Tue Mar 15, 2016 10:52 pm

There was an errant namespace entry in the runtime bridge .ini file.

The plug-in is now working as expected.

Thanks for all your help.

glenharvy
Posts: 167
Joined: Fri Sep 14, 2007 3:53 am

Re: C# .NET plugin fails when dragging it to an IA script

Postby glenharvy » Wed Jan 03, 2018 8:58 pm

erich.einfalt wrote:This would imply that either:
a. IA is not calling the Runtime bridge dll, or,
b, the Runtime bridge dll is not calling the plug-in.

The bridge dlls are those that were provided by the X3 (20.11Update) .Net Plugin Wizard.

Any thoughts?


I now have the same issue - well almost.

Can you be more precise as to how you fixed it?

glenharvy
Posts: 167
Joined: Fri Sep 14, 2007 3:53 am

Re: C# .NET plugin fails when dragging it to an IA script

Postby glenharvy » Sun Jan 28, 2018 9:38 pm

In my case the entries in the registry were incorrect.


Return to “Plug-In Development”

Who is online

Users browsing this forum: No registered users and 42 guests