red-langred-system

Is it possible to write a Windows DLL in Red?


I'd like to write a plugin for some Windows application, and it must be a DLL. I'd really love to try doing it in a mix of Red & Red/System. But asking on Rebol&Red chatroom here on SO, I got mixed responses as to whether it is currently possible in both Red and Red/System, or in Red/System only. What's the definitive answer?


Solution

  • Yes, it is possible. You can check the announcement on the Red-Blog for 0.3.3

    First of all, here is a short snippet describing the process for Red/System:

    Shared lib generation

    Since a year, we were working on bringing shared library generation, now it is available in the main branch. New features were added to support library generation like a way to declare the exported symbols and special callback functions when loading and freeing the library. Here is a simple example of a Red/System library:

    Red/System [
        File: %testlib.reds
    ]
    
    inc: func [n [integer!] return: [integer!]][n + 1] 
    
    #export [inc]
    

    You compile such shared library using the new -dlib command-line option:

    do/args %rsc.r "-dlib testlib.reds"

    The output binary name will have a platform-specifc suffix (.dll, .so or .dylib).

    Secondly, I was finally able to get a single simple Red script to compile to a .dll. The #export directive needs to be in a Red/System context as you can see the #system-global directive provides. Any function you have in Red needs to be wrapped with a Red/System wrapper. You can do this using #call as done below:

    Red []    
    
    hello: does [print "hello"]    
    
    #system-global [
            hellosystem: does [
                #call [hello]
            ]    
    
            #export cdecl [hellosystem]
    ]