Solved

A DLL dummy asks a question

Posted on 2000-02-18
6
157 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
[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
  • 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
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!

 
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

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

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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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