• Welcome to Powerbasic Museum 2020-B.
 

Calling C++ Dll with export class from PB 10 Host

Started by Heinz Grandjean, September 05, 2013, 06:47:55 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Heinz Grandjean

Hello,
I try to call a C++ Dll that has an export class from PB10.


#ifdef DLL_Test1_EXPORTS
#define DLL_Test1_API __declspec(dllexport)
#else
#define DLL_Test1_API __declspec(dllimport)
#endif


namespace CL_Export
{
     // This class is exported from the DLL_Test1.dll
     class CL_Export
{
public:
         static DLL_Test1_API int Calculate(int a);
};
}



#include "stdafx.h"
#include "DLL_Test.h"
#include <stdexcept>

using namespace std;

namespace CL_Export
{
    int CL_Export::Calculate(int a)
{
return 13;
}
}



Naturally the traditional PB- declare doesn't work:

DECLARE FUNCTION Calculate STDCALL LIB "DLL_Test1" ALIAS "Calculate" (BYVAL LONG) AS LONG
   

Do I have to use a special class definition in PB??
Maybe as Com?

Thank You
Heinz Grandjean

José Roca


Patrice Terrier

Yes, write procedural code, that would work with everything.

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Heinz Grandjean

PB-Classes are restricted to Com???

On the procedural side connecting to a C++ -DLL is ok. Tested it.
But then I have to make a procedural interface dealing internally with the Classes inside the DLL.
Coming very close to a philosophical discussion about future.

Thanks for the replies
Heinz Grandjean

P.S.
Maybe there is a chance if the C++ DLL works as a Com-Server, hopefully unregistered....

Frederick J. Harris

Sorry I'm coming late to the discussion Heinz.

The whole reason for COM was to create a standard binary footprint in memory so that various programming languages could interoperate at a binary level instead of a source code level.  Therefore, given a C++ class (not a COM based class), that class will only, with 100% certainty of success, be able to be accessed by a client written with the exact same version of C++.  In other words, there is no guarantee that an ordinary C++ class in a dll written in Microsoft C++ will be able to be accessed correctly by GNU C++.  In fact, there is no absiolute guarantee that a Microsoft Version 6 C++ class will be accessable from a version 7 client.  While it likely will, there is no guarantee.  The reason for that is that the C++ standard specifies all kinds of things related to syntax and what must compile and what must not compile, and behaviors, but does not specify how any C++ language implementor must layout binary objects. 

COM was designed to overcome that hurdle by specifying a particular binary layout.  Any language can then create COM objects that can be used by any other language.  For example, I freely interoperate between C, C++ and PowerBASIC in my work.  But the objects I create in C or C++ or low level PowerBASIC adhere to the COM memory layout for objects.  I hope this philosophical point of view helps clear it up for you.  This is a point that confuses a lot of folks.  They think that now that PowerBASIC supports objects, it should freely interoperate with C++.  It will, if the C++ class one wishes to interoperate with was built according to the COM specification, which involves a particular memory setup involving the virtual function table and virtual function table pointers.

Heinz Grandjean

I apologize  for late answering, Frederick!

I have not checked the thread any longer, thinking it had become inactive.
But in the meantime I have tested Your helpful advices concerning the call to C++ DLLs.
(I love PB, but I have realized the fact to be in a certain dead end street.)
Therefore I started to learn C++, following Patrice Terrier.

By the way I think it to be not only  a question of translating PB to C++
It is a different approach and more than "simply interpreter's work"...

Please allow me some additional sentences.
Detecting the impossibility to access C++ classes (inside C++ DLLs) via PB,  I focused the Com-aspect,
because I want to avoid a (historical) procedural export-interface (what I got working...).

I now can reach my PB!!!-compiled DLLs via late com-binding (PB-style and without registry).
I did that in the hope to do it in the same way with C++.
......??
Thanks,
Heinz Grandjean