Help solve a problem with importing a function from a dll - page 2

 

I sort of wrote something in an h-file


extern __declspec( dllexport) int __stdcall GetIntValue(int);

then in the cpp just


int __stdcall GetIntValue(int)
 
Perhaps the keyword extern will help.
 
alsu >> :

...


...and I prefer not to deal with strings at all... Why do you need them in a dll at all??? is it just for beauty?

And Borland itself recommends to use char* or char[], Metatrader-style, if necessary.

 

I think the correct way to declare it in the builder is as follows (I don't remember exactly)

extern "C" __declspec(dllexport) int __stdcall GetIntValue();

 
GarF1eld >> :

I think it is correct to declare in the builder something like this (I don't remember exactly)

extern "C" __declspec(dllexport) int __stdcall GetIntValue();

there! exactly!

 

Thank you very much for the tips! It worked with this example after extern "C" int __declspec(dllexport) __stdcall GetIntValue().

I tried it with double and string functions, and it worked too. I decided to apply the knowledge I've acquired from you to a practical task. But alas... I didn't succeed, unfortunately.

If you can, please help me one more time.

There are files in the Bilder project for DLL creation: UHsvd.h, Usvd.cpp and others.

The UHsvd.h file has a function that should be exported later:

bool rmatrixsvd(ap::real_2d_array a,
int m,
int n,
int uneeded,
int vtneeded,
int additionalmemory,
ap::real_1d_array& w,
ap::real_2d_array& u,
ap::real_2d_array& vt);

The Usvd.cpp file contains the same function, which should then be exported:

bool rmatrixsvd(ap::real_2d_array a,
int m,
int n,
int uneeded,
int vtneeded,
int additionalmemory,
ap::real_1d_array& w,
ap::real_2d_array& u,
ap::real_2d_array& vt)

{

........

}

I start building a DLL in Bilder in this variant, everything "builds", everything works, no errors.

If I start making changes in UHsvd.h, Usvd.cpp like

extern "C" bool __declspec(dllexport) __stdcall rmatrixsvd(ap::real_2d_array a,

.....), then errors from 2 to 19 appear with different variants, i.e. I made this declaration either in header file or in cpp... although as I understand it should be in the header file (then there are only 2 errors):

[BCC32 Error] Usvd.cpp(128): E2356 Type mismatch in redeclaration of '__stdcall rmatrixsvd(ap::real_2d_array,int,int,int,int,ap::real_1d_array &,ap::real_2d_array &,ap::real_2d_array &)'
[BCC32 Error] UHsvd.h(111): E2344 Earlier declaration of '__stdcall rmatrixsvd(ap::real_2d_array,int,int,int,int,ap::real_1d_array &,ap::real_2d_array &,ap::real_2d_array &)'

As I understand it, there's a discrepancy... But I don't understand it completely... :-(

Really hoping for your help and support...

 

boysn писал(а) >> {...}

If you can, please help one more time.

There are files in the Bilder project to create a DLL: UHsvd.h, Usvd.cpp and others.

The UHsvd.h file has a function that must then be exported:

bool rmatrixsvd(ap::real_2d_array a,
int m,
int n,
int uneeded,
int vtneeded,
int additionalmemory,
ap::real_1d_array& w,
ap::real_2d_array& u,
ap::real_2d_array& vt); {...}

If I understood correctly, ap::real_1d_array is a class. And ap::real_1d_array & is a reference to a class object.

But dear, you can only pass double myData[] as parameters to the Dll,

which will look like double * in functions.

 
jartmailru писал(а) >>

If I understand correctly, ap::real_1d_array is a class. And ap::real_1d_array & is reference to class object.

But dear, only double myData[] can be passed to the Dll as parameters,

which in functions will look like double *.

I agree with you completely. I'll declare it as double in MQL when importing it. But at present I'm having trouble creating the DLL itself in C++ Builder 2009. I get errors when constructing it. If I don't use declaration

extern "C" bool __declspec(dllexport) __stdcall rmatrixsvd(ap::real_2d_array a, ...., ....) then the library compilation in Bilder passes successfully without any problems. Now, I don't know how to correctly create declaration of export functions in the Bilder project :-(, in order to get

working DLL with ability to export rmatrixsvd function.

 
boysn >> :

I totally agree with you. I will be declaring it as double in MQL when importing it. But at the moment I'm having trouble creating the DLL itself in C++ Builder 2009. I get errors when constructing it. If I don't use declaration

extern "C" bool __declspec(dllexport) __stdcall rmatrixsvd(ap::real_2d_array a, ...., ....) then the library compiles in Bilder successfully, without problems. Now, I don't know how to correctly create declaration of export functions in the Bilder project :-(, in order to get

working DLL with ability to export rmatrixsvd function.

Brrr. You seem to agree with me strangely.

.

Actually we are talking about writing "adapter" or "bridge".

"Adapter" from the point of view that the parameters of the exported function

must be matched with the parameters the metatrader can pass,

. i.e. you need an extra function that passes the call on to where you want it to go.

And "bridge" from the point of view that it's not necessary for the function

that you declare will do the work itself.

And vice versa - it's not necessary to call the function that does the work.

.

That is, you need to separate the interface and the implementation, i.e. the implementation can be built on classes

. and functions that are outside the Dll no one will ever know about.

.

But let's consider an example.

.

You should declare function like this:

extern "C" bool __declspec(dllexport) __stdcall RMatrixSVD_DLL_ADAPTER(double * in1, double * in2, double * out)

// of course, you will also pass the number of rows and columns here.

{

//convert input parameters to ap::real_2d_array

ap::real_2d_array a1 = convertPDoubleToReal2DArray(in1);

ap::real_2d_array a2 = ;

.

//calculate

ap::real_2d_array a3;

rmatrixsvd(a1, a2, a3);

.

//translate result of a3 into out

convertReal2DArrayToPDouble(a3, out);

.

return;
}

.

Here - of course - the function rmatrixsvd

is not declared as extern "C" or __stdcall or __declspec etc.

Because it's part of the internal, hidden implementation.

 

Sorry, didn't quite get it, unfortunately...

From http://alglib.sources.ru/ I downloaded the C++ implementation (code) I need. If you don't mind, please have a look at the attached 100KB archive. This algorithm has many auxiliary functions, the total number of code lines is something like 3000 lines... I think so

It says so on the website, i.e. it's too much, and it's quite difficult to translate it to MQL... You have to understand the algorithm itself to correctly translate it. At the moment it's a bit complicated for me. That's why I'd like to use C++ Builder 2009 to make a dll with this ready-made code and call rmatrixsvd(...) function I need from the dll. That's where I got stumped... where and how to declare it correctly... With this code, without changing a line there, the DLL is built, without errors. But as soon as I put extern "C" bool __declspec(dllexport) __stdcall into it I get errors...

Thank you for your time.

Files:
svdy1e.cpp.zip  94 kb
Reason: