Solved

A DLL dummy asks a question

Posted on 2000-02-18
6
150 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

12 Experts available now in Live!

Get 1:1 Help Now