Click here to Skip to main content
14,980,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 24-Aug-10 0:46am
v4

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
   
v2
Comments
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.
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
   
Comments
nereone 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
Nyarost 20-Aug-10 15:43pm
   
Can you please post Delphi code too?
nereone 21-Aug-10 10:39am
   
ok, I added some delphi code
Hi, checked both C++ and Delphi code. Don't see anything :( 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)


?
   
Comments
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.
Nyarost 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.
Nyarost 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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900