Solved

Calling a DLL

Posted on 2002-04-16
12
205 Views
Last Modified: 2010-04-04

The function you are looking for is called cq_calc, and you can find it
by looking for "cq_calc" in that case.

This function has the exact (C/C++) prototype:  char* cq_calc(char*);

You send cq_calc a pointer to a character.


I can't call that routine without getting a mem error

the Dll if you would like to try for yourself is here:

http://geocities.com/tcjdelphi/tjc.zip
0
Comment
Question by:craig_capel
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
Comment Utility
The conversion is
function cq_calc(Param: PChar): PChar; stdcall; external 'TheDll.DLL';
or
function cq_calc(Param: PChar): PChar; cdecl; external 'TheDll.DLL';

Call it like this:

var
  Buffer: array[0..SomeMaxValue] of Char;
  RetVal: PChar;
begin
  // initialize buffer if needed
  RetVal := cq_calc(@Buffer[0]);

I would guess that RetVal points somewhere into the buffer.

0
 
LVL 2

Author Comment

by:craig_capel
Comment Utility
how do you conver it to a string?

Function CqCalc(calcst: String): String;
var
 Buffer: array[0..255] of Char;
 RetVal: PChar;
begin
  // initialize buffer if needed
    Buffer:=CalcSt;
    RetVal := cq_calc(@Buffer[0]);
End;


Thanks for your help so far, nearly got it :)
0
 
LVL 2

Author Comment

by:craig_capel
Comment Utility
how do you conver it to a string?

Function CqCalc(calcst: String): String;
var
 Buffer: array[0..255] of Char;
 RetVal: PChar;
begin
  // initialize buffer if needed
    Buffer:=CalcSt;
    RetVal := cq_calc(@Buffer[0]);
End;


Thanks for your help so far, nearly got it :)
0
 
LVL 2

Author Comment

by:craig_capel
Comment Utility
Function CqCalc(calcst: String): String;
var
 Buffer: array[0..255] of Char;
 RetVal: PChar;
 I: Integer;
begin
  // initialize buffer if needed
    CalcSt:=CalcSt+#$0;
    move(calcst[1],buffer,length(calcst));
    I:=Length(CalcSt);
    RetVal := cq_calc(@Buffer[0]);
    move(RetVal,Result,Length(CalcSt));
    Result:=Copy(Result,0,pos(#$0,Result)-1);
End;

ok that works for me, but is there a better way, when i try setlength like SetLength(Buffer,Length(CalcSt)); it says something about not able to pass a Delphi  object?

ok can you improve that code? - the points are yours anyhow, i just want to help me understand this better... thank you

-

Craig C.
0
 
LVL 2

Author Comment

by:craig_capel
Comment Utility
oops that code gives me an error :((
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
var
  s: string;
begin
  ...
  S := RetVal;

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
or simply do :

Function CqCalc(calcst: String): String;
begin
  SetLength(Result, 255);  
  Result := string(cq_calc(PChar(calcst)));
End;
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
String := StrPas(PChar)

So:

Function CqCalc(calcst: String): String;
begin
   Buffer:=CalcSt;
   Result := StrPas(cq_calc(pChar(CalcST)));
end;

Easy! :)

Stu
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
stuart: StrPas is fo backward compatibility
you should use typecasting now
and what  is buffer in your case ? :)
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
Well I'll be!  I've been using that function for so many years I don't think I could stand to be without it!  It's the first time that I've looked at in the help file.

Can you tell me why it's been made redundant?

Stu
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
Comment Utility
Strangely enough, Lee, you're comment wasn't even there when I posted mine.  Even though there was almost an hour between the postings.
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
The assignment of a PChar to a string nowadays takes care of converting.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now