?
Solved

CPP code & Delphi

Posted on 2007-08-08
20
Medium Priority
?
504 Views
Last Modified: 2013-11-23
Hi,

I need to use  some code written in cpp for a Delphi7 project. Never mixed pascal and C source before.

CBulider6 and Delphi7 are close relatives and Cbuilder can use VCL components, but I need the other direction, and I am not familiar with CBuilder at all.

My guess is that maybe there is a way to create a component or package in CBulider and somehow install it to the Delphi IDE. Is this possible? If yes, how?

An ActiveX control could be an other way to do it, but I have never made one before. Escpecially not in CBuilder.

A third attempt could be building a DLL in CBuilder.

What I need is a fast way to make the functionality of the CPP code avaible in Delphi7.

Any help is appreciated. :))

CC
0
Comment
Question by:cc042297
  • 9
  • 5
  • 5
  • +1
20 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 19652671
do you actually need vcl or can you just make a normal old fashioned dll?
0
 
LVL 21

Expert Comment

by:ziolko
ID: 19652712
well delphi can generate C like files (projects -> options -> linker -> linker output) so maybe delphi can accept cbuilder output? something like cbuilder's version of dcu then maybe you could use it like any other unit (without source code), but i never done it before so it's just a guess

ziolko.
0
 

Author Comment

by:cc042297
ID: 19652756
MerijnB

A DLL would be fine for now. :)

If it is possible to use components written in CPP in Delphi that would be perfect. :))

ziolko

It is really urgent, so I am after a fast working solution.  Thanks for your time anyway.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:MerijnB
ID: 19652782
there is tons of info to be found on this topic.
See for example:

http://delphi.about.com/od/objectpascalide/a/dlldelphi.htm (especially http://delphi.about.com/gi/dynamic/offsite.htm?zi=1/XJ&sdn=delphi&cdn=compute&tm=54&f=11&su=p284.8.150.ip_&tt=2&bt=0&bts=0&zu=http%3A//www.drbob42.com/delphi/headconv.htm)

I think it's best to start and get back with questions as you encounter problems.
0
 

Author Comment

by:cc042297
ID: 19653252
MerijnB

You are right, normaly I would do much more searching before asking. Yet right now I have zero time for that, so I ask, even if I know it is not a difficult question:

I have never used CBuilder before, havent programed in C for 10 years ... Right now all I need is to tell CBulider to put a function into the DLL's "public" section. I thought "_export" should do the job, yet when I run the the Delphi app, I get the function cannot be located in dll error. So I must be doing something wrong.

Could you help me with this?
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19653428
you could check your DLL with depency walker: http://www.dependencywalker.com/
to find out at what end things are going wrong.
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19653459
iirc, cbuilder didn't use export, that is plain C.
Maybe this can help you: http://www.functionx.com/bcb/libraries/staticdll.htm
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 19653925
You need to ensure its marked for export, eg:

// This is needed: "extern "C" __declspec(dllexport)"
extern "C" __declspec(dllexport) int YourFunction(double x, bool y)
{

  if (y) {
      return x * 1.0;
  } else {
        return x * -1.0;
  }

}

which will let you call the function using stdcall within Delphi.

Regards,
Russell
0
 

Author Comment

by:cc042297
ID: 19654297
Still missing something:

Here is the code:

CBulider

//  OSEA.CPP  ---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop

#pragma argsused

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
        return 1;
}

//  qrsdet2.cpp  ---------------------------------------------------------------------------

#include <mem.h>
#include <math.h>
#include "qrsdet.h"

// External Prototypes.
...

// Local Prototypes.
...

extern "C" __declspec(dllexport) int QRSDet( int datum, int init )
      {
        ...
      }

//---------------------------------------------------------------------------

Delphi

procedure QRSDet(Ekg_Value, Param: Integer); stdcall ; external 'OSEA.DLL';


The  DLL is  compiled to the dir of the Delphi app. The QRSDet function cannot be loacted in OSEA.DLL .

Any Idea what is wrong?  This Q has turned into a CBuilder Q ...still hoping, that some of you may help. :)  
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19654329
try making it a function in delphi just like in C

function QRSDet(Ekg_Value, Param: Integer): integer; stdcall ; external 'OSEA.DLL';

also check the DLL with the depency viewer I posted before.
0
 

Author Comment

by:cc042297
ID: 19654414
MerijnB

I am using it (: thanx :).

It found 2 problems:

Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.

DWMAPI.DLL and EFSADU.DLL are the missing ones. I thought these missing files had nothing to do with  the absence of the exported files. The exported functions should be listed in the  top right panel when the dll is selected, yet it is empty.


0
 

Author Comment

by:cc042297
ID: 19654499
QRSDet was declared as procedure instead of function. Changed it, but the oiginal problem still remains.

function QRSDet(datum,  Ini: Integer):integer; stdcall ; external 'OSEA.DLL';
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 19654622
Try the following for the export (worked for me in C++ Builder)

#if defined(__cplusplus)
      #define DLLENTRY extern "C" __declspec(dllexport)
#else
      #define DLLENTRY __declspec(dllexport)
#endif
#define STDENTRY DLLENTRY int WINAPI
#define STDENTRY_(type) DLLENTRY type WINAPI

// External Prototypes.
// ...

// Local Prototypes.
// ...

STDENTRY QRSDet( int datum, int init )
{
  return 0;
}

---

Russell

0
 

Author Comment

by:cc042297
ID: 19654751
rllibby

STDENTRY QRSDet( int datum, int init ) ---> STDENTRY int QRSDet( int datum, int init )

I think you have left out the return value type. When I add it, a syntax error occurs when compiling.



0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 19654821
I did not leave out the return value, check the define:

#define STDENTRY DLLENTRY int WINAPI

its defined to return an int.
0
 

Author Comment

by:cc042297
ID: 19655028
Oh, Yes! Sorry, should have read much more carefully.

I suggest a possible shortcut, If anyone is willing to try it:

The cpp files can be downloaded here.

http://www.eplimited.com/_vti_bin/shtml.exe/software.htm

Just push submit without filling out the form.

Select  "Beat Detection and Classification Source Code".

The only function I need (QRSDet) is in the  "QRSDET2.CPP" file.

If anyone is willing to try create a DLL project ...

I hope I am not asking too much, I really think this would be the most efficient way to help. :)





 
0
 

Author Comment

by:cc042297
ID: 19655072
rllibby

Pasted everything from your comment into my code. Got syntax error here:

STDENTRY QRSDet( int datum, int init )
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 1000 total points
ID: 19656257
Download the zip from here:

http://users.adelphia.net/~rllibby/downloads/qrsapi.zip

I created a sample library called QRSApi (.mak project) which exports the desired function. You will find the needed coding changes in the QRSDET2.CPP file, should you wish to implement this in something else (shows how to correctly export the function)

Russell
0
 

Author Comment

by:cc042297
ID: 19656399
rllibby

I can't tell you how thankful I am. I truly appreciate your time and effort. :))
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 19657400
No problem,
Russell
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question