How to make a loadable dll to use tcl code functio

2019-08-12 01:45发布

问题:

I have created a GUI using tcl. I want to make some of the core functionalities of the tcl code available to be used by any program which supports dll. For that i have taken a very simple tcl code example, which adds two integer numbers and i have written a c wrapper function to use this functionality. This is working for me. Now how can i create a dll for these two c and tcl files, so that any program can use this addition functionality by simply loading the dll.

Here is my simple tcl code :

/* Filename : simple_addition.tcl */

#!/usr/bin/env tclsh8.5

proc add_two_nos { } {

set a 10

set b 20

set c [expr { $a + $b } ]

puts " c is $c ......."

}

And here is my c wrapper function which uses the above tcl addition functionality :

#include <tcl.h>
#include <tclDecls.h>
#include <tclPlatDecls.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main (int argc, char **argv) {
    Tcl_Interp *interp;
    int code;
    char *result;

    printf("inside main function \n");
    Tcl_FindExecutable(argv[0]);
    interp = Tcl_CreateInterp();
    code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");

    /* Retrieve the result... */
    result = Tcl_GetString(Tcl_GetObjResult(interp));

    /* Check for error! If an error, message is result. */
    if (code == TCL_ERROR) {
    fprintf(stderr, "ERROR in script: %s\n", result);
    exit(1);
    }

    /* Print (normal) result if non-empty; we'll skip handling encodings for now */
    if (strlen(result)) {
    printf("%s\n", result);
    }

    /* Clean up */
    Tcl_DeleteInterp(interp);
    exit(0);
}

This c wrapper is working fine for me and gives correct results.

Now I want to create a dll file, so that if i include that dll to any program that supports dll, it should be able to use this addition functionality of the above tcl code. Can anybody please tell me the way i can do it. Please help me. I am new to this dll concept.

回答1:

In order to create the .dll you'll have to use something like Visual Studio and C or C++ to create the .dll (there are lots of other tools out there that can create .dll files but VS is easy to get hold of and to use.) So in VS create a new project, this needs to be a C++ WIN32 project. Select the DLL application type and the Export Symbols additional option.

VS will create a basic .dll that you can then amend to do what you want. I short I'd look at putting the creating/destruction of the intrepter into the dllmain:

BOOL APIENTRY DllMain( HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
    )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        {
            Tcl_FindExecutable(NULL);
            interp = Tcl_CreateInterp();
        }
    case DLL_THREAD_ATTACH:
        break ;
    case DLL_THREAD_DETACH:
        break ;
    case DLL_PROCESS_DETACH:
        {
            Tcl_DeleteInterp(interp);
            break;
        }
    }
    return TRUE;
}

and then create functions exported by the .dll that make use of the interpreter. If you aren't familiar with the concept of shared libaries then I'd suggest spending a little time reading up on them, try here and here for some background reading.



标签: dll tcl