Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: exception mixing VCL and DLL in c++11


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Apr 5, 2017 3:36 AM Last Post By: Martin Stratmann
Martin Stratmann

Posts: 18
Registered: 5/26/00
exception mixing VCL and DLL in c++11  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 2, 2017 6:47 AM
I get an exception when I mixing VCL and DLL in CLang-Compiler bcc32c in RAD Studio Tokyo 10.2

On my MainForm::ButtonClick I do the following:
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonClick(TObject *Sender)
	{
	std::vector<int> v;
	dll_vector(v);
        // ...
        }


In my DLL, I do the following:
#include <vector>
//---------------------------------------------------------------------------
extern "C" int _libmain(unsigned long reason)
	{
	return 1;
	}
//---------------------------------------------------------------------------
void __export dll_vector(std::vector<int>& v)
	{
	v.push_back(1);
	}


The second click on Button throws the exception:
$C0000005 'access violation at 0x50039f41: write of address 0x00000001'. Prozess cpp11.exe (9204)

The last stack trace is
void deallocate(pointer _Ptr, size_type _Count)
{	// deallocate object at _Ptr
	_Deallocate(_Ptr, _Count);
}

in xmemory0

In my DLL-Project, I've include the library usebormm.lib from RAD Studio \lib\win32c\debug\usebormm.lib

Is it not impossible to mix VCL with DLL in bcc32c-Compiler?

Regards,
Martin

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: exception mixing VCL and DLL in c++11  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 2:12 PM   in response to: Martin Stratmann in response to: Martin Stratmann
Martin wrote:

I get an exception when I mixing VCL and DLL in CLang-Compiler bcc32c
in RAD Studio Tokyo 10.2

That is not surprising. In general, it is not safe to pass non-POD data
over the DLL boundary at all. IOW, don't pass a std::vector between EXE
and DLL! Even if they are compiled with the same compiler, it is still not
safe. Just don't do it.

In your example, it would be safe to do something more like this instead:

MyDll.h:

#ifndef MyDLLH
#ifndef MyDLLH
 
#ifdef __cplusplus
extern "C" {
#endif
 
#ifdef COMPILING_MY_DLL
#define MY_DLL_EXPORT __declspec(dllexport)
#else
#define MY_DLL_EXPORT __declspec(dllimport)
#endif
 
typedef void __stdcall (*dll_vector_callback)(int Value, void *UserData);
 
MY_DLL_EXPORT void __stdcall dll_vector(dll_vector_callback AddCallback, 
void *UserData);
 
#ifdef __cplusplus
extern "C" {
#endif
 
#endif

MyDll.cpp:

#define COMPILING_MY_DLL
#include "MyDll.h"
 
extern "C" int _libmain(unsigned long reason)
{
    return 1;
}
 
void __stdcall dll_vector(dll_vector_callback AddCallback, void *UserData)
{
    AddCallback(1, UserData);
}

MainForm.cpp:

#include "MyDll.h"
 
void __stdcall MyAddCallback(int Value, void *UserData)
{
    static_cast<std::vector<int>*>(UserData)->push_back(Value);
}
 
void __fastcall TForm1::ButtonClick(TObject *Sender)
{
    std::vector<int> v;
    dll_vector(&MyAddCallback, &v);
    // ...
}


Is it not impossible to mix VCL with DLL in bcc32c-Compiler?

Not the way you are attempting to, no. And that goes for any compiler, not
just bcc32c.

--
Remy Lebeau (TeamB)
Martin Stratmann

Posts: 18
Registered: 5/26/00
Re: exception mixing VCL and DLL in c++11  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 5, 2017 3:36 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
thank for the clarification, Remy.

Remy Lebeau (TeamB) wrote:
Martin wrote:

I get an exception when I mixing VCL and DLL in CLang-Compiler bcc32c
in RAD Studio Tokyo 10.2

That is not surprising. In general, it is not safe to pass non-POD data
over the DLL boundary at all. IOW, don't pass a std::vector between EXE
and DLL! Even if they are compiled with the same compiler, it is still not
safe. Just don't do it.

In your example, it would be safe to do something more like this instead:

MyDll.h:

#ifndef MyDLLH
#ifndef MyDLLH
 
#ifdef __cplusplus
extern "C" {
#endif
 
#ifdef COMPILING_MY_DLL
#define MY_DLL_EXPORT __declspec(dllexport)
#else
#define MY_DLL_EXPORT __declspec(dllimport)
#endif
 
typedef void __stdcall (*dll_vector_callback)(int Value, void *UserData);
 
MY_DLL_EXPORT void __stdcall dll_vector(dll_vector_callback AddCallback, 
void *UserData);
 
#ifdef __cplusplus
extern "C" {
#endif
 
#endif

MyDll.cpp:

#define COMPILING_MY_DLL
#include "MyDll.h"
 
extern "C" int _libmain(unsigned long reason)
{
    return 1;
}
 
void __stdcall dll_vector(dll_vector_callback AddCallback, void *UserData)
{
    AddCallback(1, UserData);
}

MainForm.cpp:

#include "MyDll.h"
 
void __stdcall MyAddCallback(int Value, void *UserData)
{
    static_cast<std::vector<int>*>(UserData)->push_back(Value);
}
 
void __fastcall TForm1::ButtonClick(TObject *Sender)
{
    std::vector<int> v;
    dll_vector(&MyAddCallback, &v);
    // ...
}


Is it not impossible to mix VCL with DLL in bcc32c-Compiler?

Not the way you are attempting to, no. And that goes for any compiler, not
just bcc32c.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02