The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Ar-ras on April 19, 2006, 01:23:54 AM

Title: MASM32DLL used in C++,VB, Delphi
Post by: Ar-ras on April 19, 2006, 01:23:54 AM
Hello
I started to write a dll.
procedures are working fine, but I dont know how functions work
How can I send the the result to the calling program?

thx Ar-ras
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: hutch-- on April 19, 2006, 01:46:56 AM
With the exception of floating point data, the normal return value for almost all languages in 32 bit is EAX so as long as you place the value you want to return in EAX, it should work fine.


YourProc proc args etc ....

  ; code

    mov eax, your_return_value
    ret

YourProc endp
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: Ar-ras on April 19, 2006, 04:35:59 AM
 :cheekygreen:
thx :U
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: zooba on April 19, 2006, 08:54:49 PM
VB can get more complicated if you're returning thing other than 8-, 16- or 32-bit integers.

Just yell if you're returning something more complex (like a structure or string) :U

Cheers,

Zooba
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: eliran on April 19, 2006, 09:00:20 PM
zooba I loved the attitude  :thumbu  :wink
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: asmfan on April 20, 2006, 07:16:37 PM
Try to look at Agner Fog site (agner.org) calling conventions and other documets, that describe how procedure should correctly return different values
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: Ar-ras on April 21, 2006, 12:27:02 AM
Now I have a different question
How can I input structures?
like
RECT STRUCT
x     DWORD  ?
y     DWORD  ?
RECT ENDS

test proc Var1:int, Var2:RECT
.
.
.
test endp

is this correct?

EDIT:
What means in asm the $ in Variable$ ?
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: hutch-- on April 21, 2006, 12:40:08 AM
You can pass structure in a couple of different ways. If you pass the data type as a structure,

tstproc proc var:DWORD,items:RECT    etc ....


items is effectively 4 * DWORD arguments passed on the stack.

The alternative is to pass the ADDRESS of the structure as a single DWORD argument on the stack which involves a bit more work in dereferencing it within the proc but the structure remains after the proc has closed. This is typically how you get a number of return values from a procedure.

MASM has no intrinsic data type by trailing designation, the var$ is simply what you attribute to it as a normal variable. I have personally used such names as string variables and procedures and macros because it is easily recognised by many higher level programmers as a string.
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: Ar-ras on April 22, 2006, 10:40:21 AM
Quote from: hutch-- on April 21, 2006, 12:40:08 AM
The alternative is to pass the ADDRESS of the structure as a single DWORD argument on the stack which involves a bit more work in dereferencing it within the proc but the structure remains after the proc has closed. This is typically how you get a number of return values from a procedure.

how can i do this ?
Title: Re: MASM32DLL used in C++,VB, Delphi
Post by: MichaelW on April 22, 2006, 11:21:21 AM
There are probably other methods of doing this, but the two that occur to me are to place the pointer in a register and use the structure as a template, or place the pointer in a register and use ASSUME to specify the register as a pointer to the structure. For the second method you need to remember to cancel the assumption when it is no longer needed.

testproc proc uses ebx pRECT:DWORD
    mov   ebx, pRECT

    print ustr$([ebx].RECT.left),13,10
    print ustr$([ebx].RECT.top),13,10
    print ustr$([ebx].RECT.right),13,10
    print ustr$([ebx].RECT.bottom),13,10

    ASSUME ebx:PTR RECT
    print ustr$([ebx].left),13,10
    print ustr$([ebx].top),13,10
    print ustr$([ebx].right),13,10
    print ustr$([ebx].bottom),13,10
    ASSUME ebx:NOTHING

    ret
testproc endp


If you are passing the structure from a HLL, depending on the structure you may need to consider how the HLL aligns, pads, or packs the structure or union members.

Windows Data Alignment (http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vcconWindowsDataAlignmentOnIPFX86X86-64.asp?frame=true)