Click here to Skip to main content
Rate this: bad
good
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;
else
if n=0 then //throw exception
else
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
nereone173
Edited 24-Aug-10 1:46am
v4
Rate this: bad
good
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.
 
Cheers,
 
Ash
  Permalink  
v2
Comments
nereone at 21-Aug-10 10:19am
   
But the referencing is working
Aescleal at 21-Aug-10 12:39pm
   
Okay, just thought I'd throw that comment in there! Sorry it wasn't any help.
nereone at 21-Aug-10 13:47pm
   
Anyway, thanks for your effort.
Rate this: bad
good
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.
 
Cheers
  Permalink  
Comments
nereone at 20-Aug-10 12:42pm
   
1. yes in delphi i am using pchar
2. cdecl is default calling convention ( http://msdn.microsoft.com/en-us/library/zkwh89ks(v=VS.100).aspx )
Thanks for your effort
Kostoprav_gka at 20-Aug-10 15:43pm
   
Can you please post Delphi code too?
nereone at 21-Aug-10 10:39am
   
ok, I added some delphi code
Rate this: bad
good
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)
 
?
  Permalink  
Comments
nereone at 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 at 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 at 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 at 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
0 Marcin Kozub 275
1 OriginalGriff 249
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 24 Aug 2010
Copyright © CodeProject, 1999-2014
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