Click here to Skip to main content
11,932,446 members (54,652 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ Windows MFC DLL Delphi
Hello, I am stucked for few days and dont know what to do.

I am working on a library written in c++ (the result is dll), which contains several functions. The DLL itself isnt using MFC, and is singlethread. These functions are then called from delphi application. Example of a function:
extern "C" __declspec(dllexport) int open(void)

All the functions have to return int, because it is used to detect, wheather the function failed. Although, result of some functions has to be string also. This string is passed as a function parameter. Example:
extern "C" __declspec(dllexport) int getName(int index, char* &name)

And here comes the problems. After the whole function is executed, the string variable contains correct data (which is good), but somewhere (I dont know where) is being memory corrupted, which also corrupts some variables in delphi app.
I blanked all the functions like this:
extern "C" __declspec(dllexport) int getName(int index, char* &name)
   return 0;
The memory is being corrupted still. (Unwanted change of delphi variables occures by this "string" type of functions).
I will appreciate any type of help, because I really dont know, what could cause this problem.
Thank you.

Edit: Added some delphi code:
n := getCount; //calling dll function
if n<0 then exit;
if n=0 then //throw exception
if n>1 then //throw exception
status := getName(0,tmp);//after executing this function is n modified, however there are no operations with n
if status<0 then exit;
status := getTown(0,tmp);
if status<0 then exit;
n is the mystery modified variable.

Edit2: just tried to create a c++ console app, where could I test correct calling of methods from dll and everything works fine, so problem seems to be in delphi, maybe in calling and allocating memory for methods or whole dll.

FINAL EDIT: The problem is solved: you have to define def files and use stdcall
Posted 20-Aug-10 6:44am
Edited 24-Aug-10 1:46am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I know nothing about Delphi but are you sure it can cope with a reference parameter? None of the other languages I use regularly (Java, Python) can handle C++ references so it might be same for Delphi and that's what's causing the problem.


nereone 21-Aug-10 10:19am
But the referencing is working
Aescleal 21-Aug-10 12:39pm
Okay, just thought I'd throw that comment in there! Sorry it wasn't any help.
nereone 21-Aug-10 13:47pm
Anyway, thanks for your effort.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

1. String in C++ = PChar in Deplhi.
2. extern "C" means C-style function calling. For delphi it is cdecl as soon as I remember.

nereone 20-Aug-10 12:42pm
1. yes in delphi i am using pchar
2. cdecl is default calling convention ( )
Thanks for your effort
Kostoprav_gka 20-Aug-10 15:43pm
Can you please post Delphi code too?
nereone 21-Aug-10 10:39am
ok, I added some delphi code
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Hi, checked both C++ and Delphi code. Don't see anything Frown | :( However this looks a little bit interesting:
extern "C" __declspec(dllexport) int getName(int index, char* &name)

Is there such thing as * & specifier in C++? Shouldn't it be
extern "C" __declspec(dllexport) int getName(int index, char* name)

nereone 21-Aug-10 11:55am
& is dereference. So, after executing whole method, variable name will keep changes. And its actually working. I do appreciate your help, Thank you.
Kostoprav_gka 21-Aug-10 12:07pm
Hmm, actually for me it look like reference to pointer to char array. May be you can try *name?
nereone 23-Aug-10 6:01am
well, that might work also. Anyway, this is not my problem, passing string from dll to delphi is working, What is not working, is correct operating with memory. However, I blanked all the methods, so there is no memory allocation and the problem remains still.
Kostoprav_gka 23-Aug-10 6:04am
Strange only idea is that in DLL code returned variable is declared as static.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 24 Aug 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100