call function inside a c++ file from bcx environment.

Started by Saleh, April 09, 2025, 05:52:26 PM

Previous topic - Next topic

Saleh

hi all,
is it possible or have any solution like compile c code and linked statically or any other solution please.??
thank you

MrBcx

Hi Saleh,

I don't recall anyone sharing BCX code for doing so but I know it
is possible with certain conditions, notably if you have the code to
the C++ DLL and can re-compile it, per the (Option 1) steps below.
(Option 3) is also possible but it looks more complicated.

In my estimation, the amount of usefulness that one might obtain from a C++ DLL
via BCX / C is likely to be quite limited, given that many C++ developers are using
C++ with classes (OOP) and other features that do not exist in plain C.

Here is what ChatGPT says on the subject:

It is possible to call a function inside a C++ DLL from an application compiled in plain C, with some conditions.
🔑 Key Requirements

C++ function names are mangled to support function overloading, but C99 expects C-style names. To bridge this gap:

Option 1: Use extern "C" in the C++ DLL

If you control the C++ DLL code:

// CppDll.cpp
extern "C" __declspec(dllexport) int Add(int a, int b)
{
    return a + b;
}

    extern "C" tells the compiler not to mangle the function name.

    __declspec(dllexport) makes the function visible from outside the DLL.

Option 2: Use a .def file to export unmangled names

If you can't use extern "C", or the source isn't yours, you can still export functions with unmangled names via a module-definition file:

LIBRARY MyCppDll
EXPORTS
    AddFunctionName

But this only works if the function has C-compatible calling conventions and signature.

Option 3: Use a C-compatible wrapper DLL

If the C++ function uses classes, templates, references, or overloaded functions, you need a wrapper that provides C-callable interfaces.

Example wrapper (C++):

extern "C" __declspec(dllexport) void* CreateObject()
{
    return new MyCppClass();
}

extern "C" __declspec(dllexport) void CallMethod(void* obj)
{
    ((MyCppClass*)obj)->SomeMethod();
}

Your C99 app can call CreateObject() and get a void* handle, and then call CallMethod(handle).
📌 C99 App Side (BCX or plain C)

typedef int (__cdecl *AddFunc)(int, int);

HINSTANCE hDLL = LoadLibrary("MyCppDll.dll");
if (hDLL)
{
    AddFunc Add = (AddFunc)GetProcAddress(hDLL, "Add");
    if (Add)
        printf("Result = %d\n", Add(2, 3));
    FreeLibrary(hDLL);
}


MrBcx

Hi again Saleh,

I should probably state at this point that my previous reply ASSUMED that you
wanted to call a c++ DLL from Lcc-Win32.  It eventually occurred to me that you
might be using Microsoft Visual C++ or Mingw (g++)
, in which case things
become much easier.

For example, a couple of years ago I wanted to see if I could create a static c++
library and DLL using BCX.  I started with this BCX code to create a C++ DLL:


$DLL
 
CLASS __declspec(dllexport) bcx_vector

   PROTECTED:
   
   DIM RAW AS DOUBLE x
   DIM RAW AS DOUBLE y

   PUBLIC:
   
   METHOD area () AS DOUBLE
      DIM RAW AS DOUBLE s
      s = x * y
      IF s < 0 THEN s = -s
      METHOD = s
   END METHOD

   PROPSET Set_x (n AS INT)
      x = n
   END PROPSET

   PROPSET Set_y (n AS INT)
      y = n
   END PROPSET

   PROPGET Get_x
      PROPGET = x
   END PROPGET

   PROPGET Get_y
      PROPGET = y
   END PROPGET
END CLASS



Then I created this test app in BCX:



$HEADER
class __declspec(dllimport) bcx_vector
  {
  protected:
    double x;
    double y;
  public:
    double area();
    void Set_x(int n);
    void Set_y(int n); 
    int Get_x();
    int Get_y();
    };
$HEADER



SUB MAIN
    DIM RAW AS bcx_vector a

    a.Set_x(3)
    a.Set_y(4)

    PRINT "The area is :", a.area()

    PRINT "x =", a.Get_x()
    PRINT "y =", a.Get_y()
    PAUSE
END SUB



Finally, this is the build.bat to assemble all the parts.


bc myclass -c
bc test    -c

call vd64 myclass
call vc64 test myclass.lib

del *.lib
del *.exp
del *.obj


What I ended up with was test.exe that was statically linked to myclass.dll

OUTPUT:

The area is : 12
x = 3
y = 4

Press any key to continue . . .