Ecere SDK/eC Forums http://ecere.org/community/ Print view |
|
Writing an export function in ec, accepting a pointer http://ecere.org/community/viewtopic.php?f=10&t=50 |
Page 1 of 1 |
Author: | fedor_bel [ Mon Mar 15, 2010 1:41 pm ] |
Post subject: | Writing an export function in ec, accepting a pointer |
Hello, Let me start. I need to write a dll with an export function to be called from another C/C++ program. I write following: Code: Select all
The crashed program report is following Code: Select all
Code: Select all
The program documentation says that May you could advice some debugging programs, information where I can learn how to debug the problem. |
Author: | jerome [ Mon Mar 15, 2010 6:46 pm ] |
Post subject: | Re: Writing an export function in ec, accepting a pointer |
Hi Fedor, In this respect eC is no different than C. You will need a proper 'matching' prototype to call it from the C/C++ program. Are you linking the DLL with the linker, or are you loading the functions yourself with the Windows API? eC has the 'stdcall' keyword to help you use stdcall convention. This will make your function use stdcall calling convention: public dllexport int stdcall f2(MqlStr * pStr){ return 1; } However, the only important thing is that the prototypes match those in your program, whether it's stdcall or not. Is your other program C or C++? If you want to call a function in a C or eC DLL from C++ you should declare it as such: extern "C" __declspec(dllexport) int __stdcall f2(MqlStr * pStr); Also the function you pasted here only returns 1 and doesn't do anything. Is that exactly the function that is crashing? You will have to be aware at one point of how eC treats diffrent types of data objects... struct and class are treated differently in eC (To my recent astonishment, much in the same way they are different in C#!). For example eC structs are always passed by references, and although you don't see the * symbol classes instances always are pointers. Furthermore, of course eC and C++ classes are different kind of objects. (We need to develop a compatibility layer.) Best of luck! Jerome |
Author: | fedor_bel [ Tue Mar 16, 2010 9:23 am ] |
Post subject: | Re: Writing an export function in ec, accepting a pointer |
Hello Jerome, Thank you very much for your quick, smart and informative answer! Unfortunately, the new information, you provided, did not help me to manage to transmit a parameter into a function. 1. If I try to use extern "C" __declspec(dllexport) int __stdcall f2(MqlStr * pStr); the ec compiler produces an error "Library.ec:12:8: error: syntax error" The compiler does not seem to like extern "C" construct. Is it a bug or I am not using it right? 2. If I try to use public dllexport int stdcall f2(MqlStr * pStr); the calling program can not find the function in the dll. Although if I use public dllexport int f2(MqlStr * pStr); the calling program finds the function in the dll ok. Why is that? 3. The example functions are empty just for the sake of simplicity, of course. What I try to achieve is to be able to receive a parameter into my export function. The calling program does not link my DLL with the linker, it loads the functions using the Windows API and late binding. Let me provide some more description regarding the requirement to the DLLs: Some more notes I made during my vane attempts: - public int f1(); - works great! - public int f1(int a1); - fails miserably! WTF? sorry for bad language Or could you just advise a good book or tutorial on the subject, but not too long, because I am pretty much tired of this trial and error approach - I definitely lack knowledge in this low level stuff. I cant even confidently define the topic of my misknowledge: could it be "a linking for dummies", or maybe "dissassembling a dll or c/c++ program for dummies". |
Author: | fedor_bel [ Tue Mar 16, 2010 10:43 am ] |
Post subject: | Re: Writing an export function in ec, accepting a pointer |
Sorry guys for me being so stupid! The problem is in the decorated function names or mangled function names. I either have to 1. use a .def file when linking my dll written in ec language. or 2. lookup the decorated function names using for example the dumpbin.exe program with the /exports option on my dll and use the decorated names when calling my functions from dll. This seem to work! That is why I the program could not find my functions in the dll when I used the __stdcall convention. |
Author: | jerome [ Tue Mar 16, 2010 11:17 am ] |
Post subject: | Re: Writing an export function in ec, accepting a pointer |
Dear fedor, If you are loading a method from a DLL, then I assume you are using function pointers? I am not sure if there are other ways to do it in C++. Loading functions from DLLs is a common topic for which Google will find you tons of tutorials. Just keep in mind eC definitions works just like C, with some convenience keyword like 'stdcall' and 'dllexport' allowing for cleaner and cross-platform syntax. The extern "C" example was code to write in your C++ file, not in eC code. extern "C" __declspec(dllimport) int __stdcall f2(MqlStr * pStr); This would let you link with an eC function defined with dllexport. But this is for doing it with the linker. To do it with the win32 loader, you would usually define it with a function pointer prototype and use GetProcAddress. stdcall functions have a declarations that varies with the number of parameters, e.g.: Code: Select all
Jerome |
Author: | fedor_bel [ Thu Mar 18, 2010 3:50 pm ] |
Post subject: | Re: Writing an export function in ec, accepting a pointer |
Thank you very much, Jerome. That helps! The picture becomes slowly more clear. |
All times are UTC-05:00 | Page 1 of 1 |
Powered by phpBB® Forum Software © phpBB Limited |