Reading an Executable Function from a SmartDongle

This walk-through will help you to understand how to read a compiled function off of the SmartDongle to use in your application.

To find out how to write a function to a SmartDongle, check out our walk-through here.


You can download the source code for this example here. This example is written in C.


The process to read a function from a SmartDongle is quite simple. Let’s start with the beginning of the main() function

This is pretty standard. The one line that you should note is the definition of the pRelocatableFunction variable.


This is the signature for the function stored on the SmartDongle. The one in the example defines a function that takes an unsigned integer parameter and returns an unsigned integer value. In your own application this should be changed to reflect the function you have stored on the SmartDongle.


The next section of code uses the P1 and P2 variables to pass the keys
required to access the SmartDongle. The values loaded into P1 and P2 are
those allocated for SmartDongles used as demos. Each customer will have
their own unique key values.


Now we get to the core of this example. In our previous example, showing how to write a function to the SmartDongle, we reserved the first 4 bytes on the SmartDongle to store the size of the stored function. This code reads those bytes and places them in the iSize variable. Now we know how much data to read from the SmartDongle to load the function into memory.


Next we allocate the memory for the function, we use the pRelocatableFunction pointer here because that is where we want the function to go. The only reason that the malloc() function will fail is if there is insufficient memory, so there is no need to print out the return code. Now that the memory is ready, we can begin to load the function from the SmartDongle.


Here we read the function from the SmartDongle. We read the data into the pRelocatableFunction pointer, using the iSize value that we filled earlier. If there are no problems the function is now in memory and ready to be used!


This is the rest of the main() function. You can call the loaded function just like any other function. This also checks to see if we get a valid result and turns the LED on the SmartDongle red if the function returns 0. Then it prints the output to the stdout.


As you can see, it is quite easy to store executable code on a SmartDongle to help protect your software. You can also add extra layers of protection, such as encrypting the function on the dongle, so it is not read raw into memory. This technique can also be applied to other languages, such as .NET languages or Java. Check out our developer resources to see more examples on how you can use a SmartDongle in your own applications.