calling a c++ dll from a delphi vcl app crashes the app upon exit [Edit]

I am using RAD Studio 2010 and I downloaded the latest updates. Since I can't see how I can attach a file to the message, I included in this message the source code for each file:

To reproduce this problem follow these steps:

Create a project group with two projects, one c++ dll project and one a Delphi VCL forms project. Save all the files in the same folder.

1. Use the wizard to create the c++ dll project, select c++ & Use VCL checkboxes. Make sure Project Options->C++ Linker->Dynamic RTL is set to true. I also added memmgr.lib and sharemem.obj to the project but I believe they are not necessary.

Here is the source code of the file;


#include <vcl.h>
#include <windows.h>

#include <system.hpp>

#pragma hdrstop
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
	return 1;


__declspec(dllexport) __fastcall int someTest(System::UnicodeString inStr, System::UnicodeString& outStr)
	 outStr = inStr.UpperCase();
	 return 1;

2. Create the Delphi VCL Forms project. Here is the main unit/form:


object Form3: TForm3
  Left = 0
  Top = 0
  Caption = 'Form3'
  ClientHeight = 341
  ClientWidth = 643
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 56
    Top = 184
    Width = 89
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  object Edit1: TEdit
    Left = 56
    Top = 112
    Width = 273
    Height = 21
    TabOrder = 1
    Text = 'Edit1'


unit testdllunit;


  ShareMem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

  TForm3 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
    { Public declarations }

  Form3: TForm3;


function someTest(inputString: String; var outputString: String): integer; external 'debug\testdll.dll' name '@someTest$qqr20System@UnicodeStringr20System@UnicodeString';

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
 var inStr, outStr:string;
  inStr := Edit1.Text;
  someTest(inStr, outStr);
  Application.MessageBox(pwidechar(outStr), 'Message');


Now when I run the app and I click the button it displays the string entered in the tedit field nicely in upper case. The problem is that the app crashes upon exit somewhere in the borlndm.dll, and I don't understand why. What am I missing? I added sharemem on the uses list. What else do I have to do? I tried to add memmgr.lib and sharemem.obj to the delphi project however it doesn't make any difference, the app still crashes.

:212849da ; C:\Program Files\Embarcadero\RAD Studio\7.0\bin\borlndmm.dll
:32ad8b5f ; C:\WINDOWS\system32\CC32100MT.DLL
:32ad909d ; C:\WINDOWS\system32\CC32100MT.DLL
:003314ab ; C:\Documents and Settings\user\My Documents\RAD Studio\Projects\testcppdll\Debug\testdll.dll
2/8/2010 5:32:06 AM
embarcadero.delphi.rtl 934 articles. 0 followers. Follow

4 Replies

Similar Articles

[PageSpeed] 33
Get it on Google Play
Get it on Apple App Store

Costa Basil wrote:

> I included in this message the source code for each file:

Next time, please don't. Rather zip them up and post them to
(or: ). You can
then refer to the zip file in this forum.

Rudy Velthuis (TeamB)

"All natural institutions of churches, whether Jewish,
 Christian, or Turkish, appear to me no other than human
 inventions, set up to terrify and enslave mankind, and
 monopolize power and profit."
 -- Thomas Paine
2/8/2010 8:59:56 PM
Costa Basil wrote:

> __declspec(dllexport) __fastcall int someTest(System::UnicodeString
> inStr, System::UnicodeString& outStr)

If you export it as extern "C", you won't export a terribly mangled
name, but it will be a name like "_someTest" instead:

> Now when I run the app and I click the button it displays the string
> entered in the tedit field nicely in upper case. The problem is that
> the app crashes upon exit somewhere in the borlndm.dll, and I don't
> understand why. 

Perhaps because ShareMem is not the first unit in your project file?

Rudy Velthuis (TeamB)

"Computer dating is fine, if you're a computer."
 -- Rita May Brown.
2/8/2010 9:06:21 PM
> Perhaps because ShareMem is not the first unit in your project file?

Phew, that was it, it was the only place that was missing ShareMem.

You're da dentist !! :-)))
2/9/2010 4:24:50 AM
Costa Basil wrote:

> > Perhaps because ShareMem is not the first unit in your project file?
> Phew, that was it, it was the only place that was missing ShareMem.

And it is the only place where it should actually be. <g>
Rudy Velthuis (TeamB)

"Science is always discovering odd scraps of magical wisdom and
 making a tremendous fuss about its cleverness."
 -- Aleister Crowley
2/9/2010 11:56:03 AM

Similar Artilces:

Calling Microsoft c++ class from a Delphi app [Edit]
Hello all ! I'm trying to call a class written under Microsoft Visual C++ 2003 by a Delphi app, but there is something wrong with the stack. Here is the Microsoft Visual C++ 2003 class ---------------------------------------------------- class CPlugIn { public: virtual void GetArraySize ( int &XSize, // Number of array columns (X) int &YSize // Number of array rows (Y) ) = 0; .... ---------------------------------------------------- and many other methods in there that I have removed for more clarity. This class translate in to Delphi i...

Delphi 7 app to web app conversion [Edit]
My company has an old Windows engineering application that we would like to convert to a web application so that we can offer it as a software as a service. The application is written primarily in Delphi 7 with the heavy number crunching done in Fortran (I did say it was old!). We would like to reuse as much of the existing Delphi code as possible and only modify the Fortran enough so that it will run in the background on the web server. We have never developed a web application so we're looking for guidance on the best way to make this conversion. Our questions at this point: ...

Java app calling a Delphi DLL
Hello I have a Java application that must call functions inside a large D7 dll. For that, I used JNI calls. That part is OK and the delphi dll is receiving the function parameters smoothly. The Delphi dll has a couple Forms in it. And these Forms are unresponsive. Timers don't work. Buttons will not invoke OnClick handlers. Most important, the assync sockets in the forms, which the dll uses to deliver its work, are not functioning either. It seems that the dll is not receiving Windows messages. I tried to simulate the application loop, but failed. The same DLL works fine i...

How to Change Tablet App to Phone app in Delphi XE8? [Edit]
Hi, i've created an app more specifically for tablets. So if the screen is smaller than 7 inch not everything fits. I do work with layouts and Align properties so everything gets scaled accordingly to the screen size. I know a few options to make the app useable for phones ass well. But not sure what the best way is. Option1: Create multiple Views for each size screen. Option2: Use ScaledLayout to make everything smaller or bigger. Option3: Use scrollBox. Option4: Edit component sizes in FormCreate after checking screen size. Option5: Devide in more Forms. *So what&...

Passing array from delphi app to C++ dll
Hi I need to develop a C++ DLL to use with my Delphi application (The DLL must be in C++ because we are going to use a lot of objects from OpenCV library). In my DLL I need to create a routine that will receive an array of the following delphi record: TMyRecord = record Origin: HRGN; Destination: HRGN; end; I have an array of this record type: var MyArray: array of TMyRecord; begin SetLength(MyArray, 4); // Fill the array .. .. // Pass the data to my dll function MyCPPDll(MyArray, 4); end; This is how it should work, I need to pass My...

How to build enterprise-grade Delphi app
Hi It would be nice to build enterprise-grade appl with Delphi, but are the options or third party software (like in J2EE) to do this? e.g. 1) there is J2EE middleware for hosting components - developer is not required to handle all the issues that are connected with session handling and different contexts in which the components can be stored (like application, session, request context, etc.). I understand that some of this can be done in DataSnap or in Web Services (can be even PHP, Java or .NET) and Delphi client - but in both cases there are some drawbacks: - for DataSnap - much shou...

Calling C++ DLL call from Delphi
Hi All I have C++ DLL setup that exports the following function: bool ApplyRule(HANDLE DriverDevice, std::string *Path, PROTECTION_TYPE Type, PROTECTION_ACTION Action, bool Architecture64, bool AddRule) I am not sure how to convert it properly to delphi, inparticular the std::string parameter. ApplyRule : function (DriverDevice : THandle; Path : ????; Kind : PROTECTION_TYPE; Action : PROTECTION_ACTION; Architecture64 : Boolean; AddRule : Boolean) : boolean; stdcall; Thanks for the help. Chris Nillissen wrote: > I am not sure how to convert it properly to delphi,...

How to use Delphi object embeded in a DLL in a C++Builder app
Hi all, I've written a small DLL in Delphi and a test program in Delphi too and everything works fine but I need to write a C++Builder app that will use the DLL too and I don't know / understand how to get access to the object defined in the DLL. MyDLL.dpr {code} library MyDLL; { Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies ...

What advantages have to build one delphi xe7 app vs hybrid app as html5 bu? [Edit]
I need to know which mobile advanced hardware characteristics differences has delphi xe7 vs html5 builder for example. I need this characteristics: TEXT EDITING Text in different directions Changing Color On a picture Copy and paste Change Font delete uppercase lowercase Save phrases Select a saved phrase Bank of personal texts PHOTOS OR IMAGES Pulling photos or images already created cut paste Change in size turn Bank of personal images Drawings predefined Figures Lines or free strokes Change color and thickness Fillings Copy and paste delete Personal bank ...

Import C or C++ DLL in Delphi [Edit]
Hello, usually I can import a dll written in C in Delhi. To do that I write a unit (wrapper.pas) to link the dll with my VCL project. example: {code} function UCC_OpenCamera(CamId: LongWord; SpeedLimit: integer): UNI_RETURN_TYPE; stdcall external 'UniControl.DLL'; {code} This way works fine for me but it requires writing a lot of code to translate all calls and types in delphi. There isn't another way to "convert/import automaticcally" the .dll? 1) Can I create a package inside C++ Builder and use it inside Delphi? 2) The command line tools IMPLIB.EXE (for ...

VCL app occasionally not exiting upon closing the main form [Edit]
Hello: I have to maintain an existing app, and I noticed during development that sometimes the application doesn't exit when I close its main form. It's VCL a app that uses dbexpress and the devart oracle driver. In the form close event it closes the connection to the database. I didn't find anything on the quality central site related to this... Do you guys have any idea what could it be? Have you seen this before? Thanks Update: I found this post:!topic/borland.public.delphi.nativeapi.win32/QDOdG1sDQNE. It seems similar to what...

Need info about calling managed C# Win app from unmanaged Delphi code
Hello experts! I've recently learned enough C# & ASP.NET to be dangerous, and I need some help with an integration project. My shop is moving from Delphi 6 to Visual Studio 2005. I have no Delphi background, but I'm the lead dot Net guy because of my alleged expertise. I have written a Windows app in C# which prints a Crystal Reports form. This app has to be called from an existing Delphi application. My app just needs a string from the calling app, and a boolean is returned to the caller, indicating success or failure. My app has a public boolean method which takes an input stri...

[SOLVED] Delphi XE5
Hello, I have a very simple program that works perfectly. But if I add a TIdTcpClient component on my form, with no change in the properties or event, just add it, then application crash when exiting with the hardware return button. What is wrong ? thank you Franck {code} unit FormTestModbusXE; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.TabControl, FMX.StdCtrls, FMX.Edit, FMX.Layouts, FMX.ListBox, IdBaseComponent, IdComponent, IdTCPConnection, Id...

Re: calling a c++ function from Delphi [Edit] [Edit]
costa basil wrote: > Sorry, I posted it here because different people read different lists > and c++ guys might not read the delphi lists and vice-versa and I > want to pick-up all brains... And yet, it is not allowed. -- Rudy Velthuis (TeamB) "This isn't right, this isn't even wrong." -- Wolfgang Pauli (1900-1958), upon reading a young physicist's paper ok, understood. ...