Solved

Calling a DLL

Posted on 2002-04-16
12
211 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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