Extending Small Basic
The Small Basic compiler is designed to allow external libraries to be plugged in that enable it to be extended in interesting ways. These libraries can be built using any .Net based language and compiled to a .Net assembly. There are a few rules that the Small Basic compiler expects for a type to be identified as a Small Basic “object.”
- The Type should be declared static
- The Type should be adorned with SmallBasicTypeAttribute
- Properties should be of type Microsoft.SmallBasic.Library.Primitive
- All the input and output parameters for Methods should be of type Microsoft.SmallBasic.Library.Primitive
- All events should be of type Microsoft.SmallBasic.Library.SmallBasicCallback
Once these conditions are met, you can compile your assembly and put it in a folder named “lib” in the Small Basic’s install location. For example, if Small Basic was installed on your “c:” drive and your library was called “myextensions”, you’d have to put myextensions.dll inside “c:\program files\microsoft\small basic\lib” folder.
Optionally, you can enable XML documentation in your build and copy over the Doc Xml file along with the library. This will automatically enable the help text inside Intellisense and the context help pane.
Here’s a sample extension (written in C#) that exposes a Settings object to Small Basic and lets you store and retrieve name value pairs specific to a program.
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.SmallBasic.Library;
namespace MyExtensions
{
/// <summary>
/// The Settings library consists of helpers that allow programs to
/// store and retrieve user settings.
/// </summary>
[SmallBasicType]
public static class Settings
{
static Primitive _filePath = new Primitive();
/// <summary>
/// Gets the file path for the settings file.
/// </summary>
public static Primitive FilePath
{
get
{
if (string.IsNullOrEmpty(_filePath))
{
_filePath = Path.ChangeExtension(
Assembly.GetEntryAssembly().Location,
".settings");
}
return _filePath;
}
}
/// <summary>
/// Gets the value for the setting identified by the specified name.
/// </summary>
/// <param name="name">
/// The Name of the setting.
/// </param>
/// <returns>
/// The Value of the setting.
/// </returns>
public static Primitive GetValue(Primitive name)
{
if (System.IO.File.Exists(FilePath))
{
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Open))
{
Dictionary<string, string> contents = ReadContents(stream);
if (contents.ContainsKey (name)) { return contents[name]; }
}
}
return "";
}
/// <summary>
/// Sets a value for a setting identified by the specified name.
/// </summary>
/// <param name="name">
/// The Name of the setting.
/// </param>
/// <param name="value">
/// The Value of the setting.
/// </param>
public static void SetValue(Primitive name, Primitive value)
{
Dictionary<string, string> contents = null;
if (System.IO.File.Exists(FilePath))
{
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Open))
{
contents = ReadContents(stream);
}
}
else
{
contents = new Dictionary<string, string>();
}
contents[name] = value;
using (Stream stream = System.IO.File.Open(FilePath,
FileMode.Create))
{
WriteContents(stream, contents);
}
}
static Dictionary<string, string> ReadContents(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
return (Dictionary<string, string>)formatter.Deserialize(stream);
}
static void WriteContents(Stream stream, Dictionary<string, string> map)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
formatter.Serialize(stream, map);
}
}
}
Share your extensions in our forum.
Comments
Anonymous
October 27, 2008
The comment has been removedAnonymous
October 27, 2008
Thanks Mehdi - good catch. I've fixed it now.Anonymous
October 28, 2008
Can you provide a compiled version of the dll for those of us who do not use C#? Thank you.Anonymous
October 30, 2008
This following peace of code in Introducing Small Basic: For i = 10 To 1 Step -1 TextWindow.WriteLine(i) EndFor is not working! thanks!Anonymous
October 30, 2008
Thanks for reporting that Thiago. That's a bug in the compiler which we'll fix for the next release.Anonymous
November 16, 2008
(1) Small Basicでもお話ししましたが、Small Basic が公開されたのは 20...Anonymous
January 02, 2009
DOES SMALL BASIC SUPPORT INTERACTION WITH MS OFFICE 2003 FILES ? (EXEL, WORD, ETC ?) NOT IN THEORY, BUT IN PRACTICE, I MEAN. THANKSAnonymous
January 04, 2009
I can't find Microsoft.SmallBasic.Library anywhere. Search in MSDN has no result. How an where can I get this? HorstAnonymous
January 04, 2009
Microsoft.SmallBasic.Library is the namespace and you'll find the assembly containing the namespace (SmallBasicLibrary.dll) in your computer where Small Basic is installed. Typically it's "c:program filesmicrosoftsmall basicsmallbasiclibrary.dll"Anonymous
January 08, 2009
how do i do that in vb.net 1. The Type should be declared static 2. The Type should be adorned with SmallBasicTypeAttribute 3. Properties should be of type Microsoft.SmallBasic.Library.Primitive 4. All the input and output parameters for Methods should be of type Microsoft.SmallBasic.Library.Primitive 5. All events should be of type Microsoft.SmallBasic.Library.SmallBasicCallbackAnonymous
January 08, 2009
how do i do that in vb.net 1. The Type should be declared static 2. The Type should be adorned with SmallBasicTypeAttribute 3. Properties should be of type Microsoft.SmallBasic.Library.Primitive 4. All the input and output parameters for Methods should be of type Microsoft.SmallBasic.Library.Primitive 5. All events should be of type Microsoft.SmallBasic.Library.SmallBasicCallbackAnonymous
February 14, 2009
The comment has been removedAnonymous
February 18, 2009
I'm having trouble linking to the xml library (dll) generated in visual studio. What to do? Exemple XMl: <?xml version="1.0"?> <doc> <assembly> <name>PrevisaoTempo</name> </assembly> <members> <member name="T:PrevisaoTempo"> <summary> Metodo de Previsão do tempo. </summary> </member> <member name="M:PrevisaoTempo.Amanha"> <summary> Amanha. </summary> </member> </members> </doc>Anonymous
February 19, 2009
Hi All! I'd like to play with FC.DLL, i've made a "lib" directory under "c:program filesmicrosoftsmallbasic" and put there fc.dll and and fc.xml. after that i'd liked to run FremyCompany's example, but it didn't run. errors were e.g.: cannot find operation 'LoadTheme' in 'Shapes' any suggestion? thxAnonymous
January 26, 2010
hi , where can i find fremycompany extension source code? i need to compile it on my system because otherwise it does not work . thankyouAnonymous
June 21, 2010
first you download the extension. next you open the small basic file then copy and paste all the extension files into the small basic folder in a new folder called "lib" open small basic and the extension will apearAnonymous
June 01, 2011
Hello, can you give me that project?(With that code in it) I didn't understand how to change type to SmallBasicTypeAttribute. Properties to Microsoft.SmallBasic.Library.Primitive. Parametrs of methods to Microsoft.SmallBasic.Library.Primitive. And events to Microsoft.SmallBasic.Library.SmallBasicCallback.Anonymous
January 23, 2012
Is it possible to make an extension using VB2010? If so can you please provide a sample?Anonymous
November 27, 2012
Here are articles in TechNet Wiki about extending Small Basic. With C# : social.technet.microsoft.com/.../14025.small-basic-how-to-create-an-extension-using-c.aspx With VB.NET : social.technet.microsoft.com/.../13340.small-basic-how-to-create-an-extension-using-vb-net.aspxAnonymous
December 03, 2012
Thanks Nonki!Anonymous
July 10, 2013
Dear Ed Price, I'm new with Small Basic and I think that is a fantastic way to start to programming :) I'm trying to make a extensión with a external DLL, like this: using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters.Binary; using Microsoft.SmallBasic.Library; namespace PoKeys56U { [SmallBasicType] public static class PoKeys56 { BullTDevice_DLL.BullTDevice device = new BullTDevice_DLL.BullTDevice(); public static void OnOut1() { } } } But when I make the compilation appear this: Error 1 BullT.device': no se pueden declarar miembros de instancia en una clase estática Do you know how I can use this DLL? ThanksAnonymous
March 27, 2015
Sorry Bull! If anyone has any questions, please ask in the Small Basic forum: social.msdn.microsoft.com/.../threads