Solved

Calling a DLL

Posted on 2002-04-16
12
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
ID: 6945483
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
ID: 6945550
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
ID: 6945597
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
Technology Partners: 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 2

Author Comment

by:craig_capel
ID: 6945673
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
ID: 6945821
oops that code gives me an error :((
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6945826
var
  s: string;
begin
  ...
  S := RetVal;

0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6946265
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
ID: 6946531
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
ID: 6946994
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
ID: 6947003
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
ID: 6947010
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
ID: 6947111
The assignment of a PChar to a string nowadays takes care of converting.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 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