Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DLL Talk / Can a DLL call-by-refrrence?

Author
Message
GatorHex
19
Years of Service
User Offline
Joined: 5th Apr 2005
Location: Gunchester, UK
Posted: 22nd Jun 2007 17:16 Edited at: 22nd Jun 2007 17:19
So I don't waste time tying to do it does anyone know if you can pass values to and from a DLL by reference?..


[in DBP]
a = 1
DLLfunction(a)
print a
[results in printing 2]


[in DLL]
void DLLfunction (&a)
{
a = a + a
}

how would you tell commands.rc its a ref?

Obviously i'm asking because i want to return two values from a function.

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Jun 2007 17:56
No, not in the way you want (going by your example code).

Strings are passed as a pointer to the character information, so you can overwrite existing characters, or shorten a string by writing a nul byte, but you can't extend the string.

Arrays are passed in the same way - a pointer to the array information, except it's possible to get access to the data the array holds.

Everything else is passed by value.

jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 30th Jun 2007 05:08
However... if you know your datatypes well - you could pass pointers in theory - though I'm not sure where dark basic stands on that - nor whether or not it stores its datatypes non-standard internally somehow - (for example byte order).

Typically - when you compile the DLL the byte order is set then - however in languages like Pascal - FreePascal specifically - you can create your (or DLL calling code) to handle either pascal or c style byte order/calling convention.

Unfortuanately - C/C++ both store things on the stack in rev order - which is actually not ideal for intel chips. Pascal calling conventions (pascal program to pascal dll) use the same byte order that the intel uses at the machine code level. This means that EVERY C/C++ call on intel - whether to a subroutine/function or dll swaps bytes around to prepare for the call - and then swaps them back in the call. (If people only knew how many clockcyles we are unknwoningly robbed of)

Jason

Know way too many languages - Master of none
the_winch
21
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 30th Jun 2007 14:18
C calling convention just puts the arguments on the stack in reverse order. It does that as C allows functions to accept a variable number of arguments.

The byte order of the arguments is whatever the host processor uses, they are not swapped. Swapping would be pointless as you can't call a function on another machine.

By way of demonstration, he emitted a batlike squeak that was indeed bothersome.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Jun 2007 14:28
Everything is in machine code is little-endian in Intel/Windows. DBPro compiles to machine code - it uses the MOV statement to read/write values to variables.

There's also no difference in speed in the way that C pushes arguments on the stack and the way that Pascal does it. The only difference in the two is that Pascal pushes the first argument first, while C pushes the last argument first (ie, they are the reverse of each other). It doesn't matter which stack-based processor you use to run your code - it's the same amount of reading/writing to the stack.

In fact, there are two differences between Pascal calling convention and the C one. Pascal produces slightly smaller code, while C can allow an unlimited number of arguments to be passed.

Here's a high-level example of how Pascal and C call a function that accepts 2 arguments

Pascal machine code form:


C machine code form:


For Pascal, if you call the function several times, you save the space of a single instruction (removing old args from the stack) every time you call it after the first. But you lose the ability to allow unlimited arguments (ala printf and associated C functions)

jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 30th Jun 2007 16:40
Yup - You are correct. I stand corrected - but I do know that the intel architecture at the assembly level does it the pascal way. However on other paltforms the C/C++ "reverse" order is the native calling convention. You are right about the bytes - it is the stack order.

Thanks for correcting me - I was a little off - however I still think calling a DLL with pointers should give the "by reference" functionality sought in this thread... after all that's really what by reference is.

Best Regards,
Jason P Sage

Know way too many languages - Master of none
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Jun 2007 21:49
Quote: "however I still think calling a DLL with pointers should give the "by reference" functionality sought in this thread"


Yes, but the problem with that is that you can't get the address of a plain variable.

Xolatron
18
Years of Service
User Offline
Joined: 12th Mar 2006
Location: The Star Forge Language: DBpro
Posted: 8th Jul 2007 02:09 Edited at: 8th Jul 2007 02:09
But you could use pointer variables like this, using Ian M's Matrix1Utils, available in his sig above :



It's painful to write (it would be nice if DBpro would jsut give us the addresses of it's variables), but if it's necessaty, it should work.

-Xol

Login to post a reply

Server time is: 2024-05-23 14:54:52
Your offset time is: 2024-05-23 14:54:52