Consultor Eletrônico



Kbase 18036: How to Call WIN32 API Function: MulDiv
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   19/06/1998
How to Call WIN32 API Function: MulDiv

DISCLAIMER:
===========
The code example in this knowledgebase is for informational purposes
only. If you have specific questions about which API calls would be
best suited to your design goals, please consult your Microsoft
documentation.

INTRODUCTION:
=============
The following sample code shows how to call the Win32 API function
called MulDiv. This function multiplies two 32-bit values and then
divides the 64-bit result by a third 32-bit value. The return value
is rounded up or down to the nearest integer. Use MulDiv() for
calculations whee the result of multiplying will overflow the 32-bit
precision.

This code has been tested on Windows NT 4.0 Workstation only.


DEFINE VARIABLE intMultiplicand AS INTEGER NO-UNDO INITIAL 200000.
DEFINE VARIABLE intMultiplier AS INTEGER NO-UNDO INITIAL 300000.
DEFINE VARIABLE intDivisor AS INTEGER NO-UNDO INITIAL 100000.
DEFINE VARIABLE intDoneWrong AS INTEGER NO-UNDO.
DEFINE VARIABLE intDoneRight AS INTEGER NO-UNDO.

PROCEDURE MulDiv EXTERNAL "KERNEL32.DLL":
DEFINE INPUT PARAMETER intMultiplicand AS LONG.
DEFINE INPUT PARAMETER intMultiplier AS LONG.
DEFINE INPUT PARAMETER intDivisor AS LONG.
DEFINE RETURN PARAMETER intResult AS LONG.
END PROCEDURE.

ASSIGN intDoneWrong = (intMultiplicand * intMultiplier) / intDivisor.

RUN MulDiv (INPUT intMultiplicand,
INPUT intMultiplier,
INPUT intDivisor,
OUTPUT intDoneRight).

MESSAGE "Answer Done Wrong = " intDoneWrong SKIP
"Answer Done Right = " intDoneRight VIEW-AS ALERT-BOX.