locked
[Tutorial] How To Write A SmallBasic Extension RRS feed

  • General discussion

  • So I am sure many here are familiar with Oskariok's Data Extension, and others like it. A few of you have probably wondered how you would make an extension. That is what this thread will answer.

    First, go grab yourself a Visual Studio distribution. Personally, I use Professional, but any of the Express versions will do. Any of the .NET languages will also work, so you could grab VB Express or C# Express. In this, I will be using C#.

    Next, you will need to open up Visual Studio. Create a new project, and select "Class Library" as your project type. Once loaded, go into the project's properties (Double click "Properties" under the Solution Explorer) and change the .NET version to 3.5. After this, you will need to reference the SmallBasic Library. Right-click References (also under Solution Explorer) and click Add Reference. Select the Browse tab and navigate to the Small Basic folder (Program Files (x86)\Microsoft\Small Basic\) and double-click SmallBasicLibrary.dll.

    Now, you will need to create a class/class file if one has not already been created for you. You will need to add SmallBasicType to the code, so your code should look a little like this:

    using Microsoft.SmallBasic.Library;
    
    namespace Your_Extension
    {
        [SmallBasicType]
        public static class YourClass
        {
            
        }
    }

    Note that you will need to declare all usable methods and classes as static for them to be recognised in the SmallBasic IDE.

    After seeing the above, it is a simple matter of writing C# into the space to attain functionality. Here is an example of what you could write, and I believe is a function included in the Data Extension:

    using Microsoft.SmallBasic.Library;
    using System.Windows.Forms;
    
    namespace Your_Extension
    {
        [SmallBasicType]
        public static class YourClass
        {
            public static void ShowMessage(Primitive content, Primitive title)
            {
                MessageBox.Show(content.ToString(), title.ToString());
            }
        }
    }

    Next comes the explanation of your classes. This can be achieved in two ways. The first way, which is the most annoying but also the easiest to correct if you make a mistake, is using an XML file, detailed below.

    <?xml version="1.0"?>
    <doc>
        <assembly>
            <name>Your_Extension</name>
        </assembly>
        <members>
            <member name="M:Your_Extension.YourClass.ShowMessage(Microsoft.SmallBasic.Library.Primitive, Microsoft.SmallBasic.Library.Primitive)">
                <summary>
                    Shows a message.
                </summary>
                <param name="content">
                The message in the message box.
                </param>
                <param name="title">
                The title of the message box.
                </param>
                <returns>
                Nothing.
                </returns>
            </member>
        </members>
    </doc>

    Note that the XML above is untested, but it should work, and that the XML file should have the same name as the extension so, if your extension was FooBar.dll, the XML would be FooBar.xml.

    The second method is using the functionality built into .NET and C# by using (not the official name, just something I call them) meta-comments. These "meta-comments" add metadata about the classes and functions, and can be done by adding a triple-forward-slash (as opposed to the normal double-forward-slash for regular comments).

    using Microsoft.SmallBasic.Library;
    using System.Windows.Forms;
    
    namespace Your_Extension
    {
        [SmallBasicType]
        /// <summary>
        /// Your class.
        /// </summary>
        public static class YourClass
        {
            /// <summary>
            /// Shows a message.
            /// </summary>
            /// <param name="content">
            /// The message in the message box.
            /// </param>
            /// <param name="title">
            /// The title of the message box.
            /// </param>
            /// <returns>
            /// Nothing.
            /// </returns>
            public static void ShowMessage(Primitive content, Primitive title)
            {
                MessageBox.Show(content.ToString(), title.ToString());
            }
        }

    While this may seem easier, it means you have to recompile your entire library instead of just rewriting something in an XML file to change the description. It may be worth the thought.

    • Edited by LMCSHERRY Monday, March 26, 2012 7:23 PM Improved IntelliSense-section (Thanks litdev)
    Friday, March 23, 2012 4:03 PM

All replies

  • Great tutorial! Thanks, it really helped me. Just a note: you forgot the semicolon after MessageBox.Show(content.ToString(), title.ToString())

    Saturday, March 24, 2012 12:11 AM
    Answerer
  • You're welcome, and thanks for the heads-up on that. The semi-colon has been added.
    Saturday, March 24, 2012 10:17 AM
  • You can get the intellisense xml to be automatically generated by typing /// before classes and methods and filling in the details - also need to set the project to output the xml and it will appear in the compilation directory.

    Also instructions for this can be found in a getting started guide I wrote a while back called 'Guide to writing extensions' in the 'Other Resource Downloads' section (expand it), website here.

    Monday, March 26, 2012 12:27 PM
  • You can get the intellisense xml to be automatically generated by typing /// before classes and methods and filling in the details - also need to set the project to output the xml and it will appear in the compilation directory.

    Also instructions for this can be found in a getting started guide I wrote a while back called 'Guide to writing extensions' in the 'Other Resource Downloads' section (expand it), website here.

    Ah, I was not aware SB supported that.

    I also forgot about setting it to appear in the compilation directory.

    Monday, March 26, 2012 3:42 PM
  • I have discovered another requirement for Small Basic extensions. You MUST compile with "Any CPU" as the platform target.

    I discovered this when I was trying to DLLImport in TWAIN imaging libraries for scanning support in my IO Extension and the thing would work fine as a Windows Forms app but SB would never accept my DLL. Unfortunately, that means I will have to remove scanning support until I get something else figured out: maybe compile the x86 DLL and wrap it with an Any CPU wrapper to please SB.

    Just figured I better bring this up so nobody else spends the better part of an hour trying to get their extension to work =)


    Please mark any answers and "vote as helpful" any posts that help you!

    Thursday, December 27, 2012 2:58 AM
    Answerer