Solved

A DLL dummy asks a question

Posted on 2000-02-18
6
153 Views
Last Modified: 2010-04-04
Hello Experts;

Trying to use a simple DLL function (written in C) that sets a voltage through a serial port.

The text from the documentation file reads:

=====================================================
Name: SetVoltage
Description: Sets the Voltage.
Entry Parameters: dV is the Value required. nMaxV is the known maximum for the voltage unit, which is planned  to be 2000
Returns:          0 = failed
                  1 = success
                  -1 = range error
                  -2 = no COM1 available

int SetVoltage ( double dV, int nMaxV );

Example:
         int nSuccess = SetVoltage ( 750.0, 2000 );

=====================================================

I've put the following into an application:

///////////////////////////////////
{$R *.DFM}

Function SetVoltage(var dV:double; nMaxV:integer):integer; stdcall; external 'THE.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
  temp, nMaxV : integer;
  dV : double;
begin
  dV := 750.0;
  nMaxV := 2000;
  Temp := SetVoltage(dV, nMaxV);
  Label.Caption := inttostr(temp);
end;
///////////////////////////////////

I can't seem to make this function return anything other than -1 for range error.   I believe this should work but I'm getting nowhere.  I think it's time to ask people who are smarter than me...

Thanks in advance...

Paul
0
Comment
Question by:prstein
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2537372
Try cdecl instead of stdcall.
0
 
LVL 1

Author Comment

by:prstein
ID: 2537379
Hello Epsylon;

Thanks, but tried that.  Gives an access violation and brings up that ugly CPU window.

Paul
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 2537390
And removing 'var' in the declaration?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

Author Comment

by:prstein
ID: 2537416
Tried that, and it more-or-less worked.  Now it returns 0 for "failed."

If you can tell me that the reason for this is either with the device or the dll and NOT my code, I'll gladly give you the points.

And if it's not too much, why would removing "var" from the declaration do the trick?

Paul
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2537456
>Tried that, and it more-or-less worked.  Now it returns 0 for "failed."

Does the dll receive the correct values dV and nMaxV values? Did you use stdcall or cdecl?

>If you can tell me that the reason for this is either with the device or the dll and NOT my code, .......

ehhh I can see the device nor the dll and except for the 'var' and 'cdecl' stuff I can't see any other flaw.

>And if it's not too much, why would removing "var" from the declaration do the trick?

Var means that the parameter is passed by reference (pointer) and not as a value.
0
 
LVL 1

Author Comment

by:prstein
ID: 2537501
> Does the dll receive the correct values dV and nMaxV values?

As far as I know.  How could it not?

> Did you use stdcall or cdecl?

It worked with stdcall.  Going without "var" and with "cdecl" crashed Delphi spectacularly.

> Var means that the parameter is passed by reference (pointer) and not as a value.

Thank you!

I think at this point I can safely go to the generator of the code and complain to him.  Thanks much and the points are yours.

Paul
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

9 Experts available now in Live!

Get 1:1 Help Now