Solved

Calling a DLL

Posted on 2002-04-16
12
206 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

14 Experts available now in Live!

Get 1:1 Help Now