Calling DLL written in C++ 
Author Message
 Calling DLL written in C++

I found a couple tcl packages that will make calls to DLL's written in C,
but haven't found any that will call C++ DLL's. Does anyone know if this is
possible with Tcl?


Fri, 30 Jul 2004 20:12:08 GMT  
 Calling DLL written in C++

Quote:

> I found a couple tcl packages that will make calls to DLL's written in C,
> but haven't found any that will call C++ DLL's. Does anyone know if this is
> possible with Tcl?

I'd guess you can't call C++ directly due to name mangling, but if you
provide a simple C wrapper for your library, everything should work.

Andre'

--



Fri, 30 Jul 2004 20:27:47 GMT  
 Calling DLL written in C++

Quote:

>I found a couple tcl packages that will make calls to DLL's written in C,
>but haven't found any that will call C++ DLL's. Does anyone know if this is
>possible with Tcl?

Very much.  It's the same language except for the name mangling and OO
extensions.  Could you be specific, please.  In what way would you like to call
a function in a c++ module?  Something like this:

#include "TclAdapter.hpp"

class MyExtension : public Tcl::Adapter<MyExtension>
{
public:
    MyExtension(Tcl_Interp *interp)
        : Tcl::Adapter<MyExtension>(interp)
    {
        NewTclCmd(interp, "test", &MyExtension::TestCmd);
    }
    virtual ~MyExtension() {};

    // The adapter base class is telling us we are about to go away and it is
    // safe to use the interp pointer to do any needed cleanup.
    virtual void DoCleanup () {};

private:
    int TestCmd (int objc, struct Tcl_Obj * CONST objv[])
    {
        Tcl_SetResult(m_interp, Tcl_NewStringObj("Hello, World!", -1));
        return TCL_OK;
    }

Quote:
};

// tell the EXTERN macro we want to declare functions for export.
#undef TCL_STORAGE_CLASS
#define TCL_STORAGE_CLASS DLLEXPORT

EXTERN int
Myextension_Init (Tcl_Interp *interp)
{
#ifdef USE_TCL_STUBS
    if (Tcl_InitStubs(interp, "8.1", 0) == 0L) {
        return TCL_ERROR;
    }
#endif
    new MyExtension(interp);
    Tcl_PkgProvide(interp, "myextension", "1.1");
    return TCL_OK;

Quote:
}

--

[species: human; planet: earth,milkyway,alpha sector]
Please be aware of the 7.5 year ping times when placing a call from alpha centari

  TclAdapter.hpp
4K Download


Fri, 30 Jul 2004 20:29:50 GMT  
 Calling DLL written in C++
After you compile you can look at what mangled name C++ gives what ever
you want to call (via a link map) or you can at run time get the memory
address where the routines exist, either way you can call the C++
method.  Other problems you will run into are exceptions and handling
the this pointer.  For an example of how to deal with these questions

on my DLL caller in a while and not sure it works with version
"whatever" of Tcl but you can see the code and figure it out.

Earl Johnson

Quote:

>I found a couple tcl packages that will make calls to DLL's written in C,
>but haven't found any that will call C++ DLL's. Does anyone know if this is
>possible with Tcl?



Sat, 31 Jul 2004 01:13:56 GMT  
 Calling DLL written in C++

Quote:

> I found a couple tcl packages that will make calls to DLL's written in C,
> but haven't found any that will call C++ DLL's. Does anyone know if this is
> possible with Tcl?

 All you need is to declare your _Init function as `extern "C"':

extern "C" {

  DLLEXPORT int Package_Init (Tcl_Interp * interp)
  {
    ...
  }

Quote:
}

        Frank

--

Experience is something you never have until just after you need it.
- Alfred E. Neuman



Fri, 30 Jul 2004 22:26:27 GMT  
 Calling DLL written in C++

Quote:
> I found a couple tcl packages that will make calls to DLL's written in
C,
> but haven't found any that will call C++ DLL's. Does anyone know if
this is
> possible with Tcl?

You should look at SWIG (http://www.swig.org/) which is the Simplified
Wrapper Interface Generator.  Not only can you call C++ functions, but
SWIG can even create Tcl commands to create and manipulate C++ objects.
You would essentially be creating a Tcl extension which calls the DLL
functions.

Pretty cool stuff. :-)

Bob
--

Mayo Foundation                                 (507) 538-5495
200 First St. SW                            FAX (507) 284-9171
Rochester MN, 55901  USA            http://www.mayo.edu/sppdg/



Fri, 30 Jul 2004 23:22:34 GMT  
 Calling DLL written in C++

Quote:

>    Tcl_SetResult(m_interp, Tcl_NewStringObj("Hello, World!", -1));

        Tcl_SetObjResult(m_interp, Tcl_NewStringObj("Hello, World!", -1));

Silly me..
--

[species: human; planet: earth,milkyway,alpha sector]
Please be aware of the 7.5 year ping times when placing a call from alpha centari



Sat, 31 Jul 2004 10:12:41 GMT  
 Calling DLL written in C++
David,

In the h file you sent, there is a line of code that has "namespace" in it.
Here's the context:

*******************
// short cut
#define NewTclCmd(a,b,c) \
 Tcl_CreateObjCommand((a), (b), CmdDemux, CmdInfo((c), this), CmdDelete)

namespace Tcl {
    template <class T>
 class Adapter
    {
***************

While compiling in Microsoft C++ the namesapce is giving and error.
Any thoughts as to why?

Thanks,
Don



Sat, 31 Jul 2004 20:30:13 GMT  
 Calling DLL written in C++

Quote:

>David,

>In the h file you sent, there is a line of code that has "namespace" in it.
>Here's the context:

>*******************
>// short cut
>#define NewTclCmd(a,b,c) \
> Tcl_CreateObjCommand((a), (b), CmdDemux, CmdInfo((c), this), CmdDelete)

>namespace Tcl {
>    template <class T>
> class Adapter
>    {
>***************

>While compiling in Microsoft C++ the namesapce is giving and error.
>Any thoughts as to why?

If you had put the code sample from the post into a .c instead of a .cpp, the
compiler would be in c mode and that's the first c++ keyword.

#ifndef __cplusplus
#error Boom!
#endif

Just guessing..
--

[species: human; planet: earth,milkyway,alpha sector]
Please be aware of the 7.5 year ping times when placing a call from alpha centari



Sat, 31 Jul 2004 21:51:01 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. C++ Call to DLL made in LabVIEW Fails the second time it is called

2. how do I pass an array to a dll written in visual c++

3. writing dll in visual c++ and importing it to labview

4. how to import code written in c++ into labview by dll

5. How to write Win32 DLL callable by smalltalk in C or C++

6. Write delphi dll, call from clarion

7. Writing C DLL & calling from REXX

8. Calling an activeX-DLL written in Delphi.

9. c++ calling fortran and fortran calling C++

10. How to call a delphi written DLL from rmcobol

11. C++ calling Fortran calling C++

12. calling Visual C++ or Prolog calling Visual C++

 

 
Powered by phpBB® Forum Software