ID:156736
 
i've seen in the discussions and whatnot where people say they can use a Dll with byond. Now i know what Dll files and and how they're used in c++, but i have no idea how to call a certain function from the Dll using byond. So can someone help me with this?
In response to UmbrousSoul
thanks :D
mmm, well having this compiles fine and all in Dev c++, and anyway i literally JUST started learning about DLLs yesterday and im having trouble with using a test dll in byond D:. Looks like this

yea ima use <dm></dm> tags

dllmain.cpp
#include "dll.h"
#include <windows.h>

__declspec (dllexport) float Math(int x)
{
return (x/2*((7*9+394+9/864*4)%9)*394/495+4-3945+59684)+(x%3+x%494+x*394%x+3849+x+x/x+(43%x));
}

BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;

case DLL_PROCESS_DETACH:
break;

case DLL_THREAD_ATTACH:
break;

case DLL_THREAD_DETACH:
break;
}

/* Returns TRUE on success, FALSE on failure */
return TRUE;
}


dll.h
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */


__declspec (dllexport) float Math(int x);


#endif /* _DLL_H_ */


and this is the byond code ime using

DllTest.dm
mob
verb
ComplexMath(n as num)
var/s=call("math.dll","Math")(n)
if(s==null)
world<<"could not use Math() proc in pixelmove.dll"
return
else
world<<"[s]"


and apparently it cant find the function :P
In response to Masschaos100
DLLs BYOND uses requires a very specific format for functions:

extern "C" __declspec(dllexport) char * my_function(int argc, char ** argv);

BYOND will pass all arguments to you functions as C strings. argc will tell you how many were passed, and argv is an array of C strings which is the arguments passed. You must also return a C string. For your example, you will need to scan the strings in argv into your int argument, do that math calculation you want to do, then print the resulting float into a string, to return to BYOND. Also be aware, you will need to convert to and from text on BYOND's side as well.
In response to Stephen001
Out of curiosity as i dont know much about dll's
What kind of game related things could you use dll's for? and what is the benefits of using a dll for those kind of functions? can you achieve things not possible through byond programming through a dll?
In response to Tonyth
well as i've heard(and its true) c++ is hundreds of times faster than byond, so you can use it to do things faster than you normally would using straight DM
In response to Masschaos100
You mistyped thousands.

Edit:

DM Code:
mob
verb/test()
for(var/i = 0; i<10; i++)
P()
proc/P()
for(var/i = 0; i<1000000; i++)
var/b = sqrt(i*i+i*i)

This took 8 seconds to complete.


C++ code:
void P(){
for(_int64 i = 0; i<1000000; i++)
_int64 b = sqrt((double)i*i+i*i);
}
int main(){
CStopWatch *timer = new CStopWatch();
int calls = 0;
timer->startTimer();
for(_int64 i = 0; i<10; i++){
P();
calls++;
}
timer->stopTimer();
double d = timer->getElapsedTime();
cout << "Total elapsed time: " << d << endl;
cout << "Average elapsed time: " << d/calls << endl;
system("pause");
return 0;
}

It wasn't able to measure time (says 0). So I increased loop 100 times:
    for(_int64 i = 0; i<1000; i++){
P();
calls++;
}

Now whole operation takes 1.91998e-006 seconds, that's 0.0000019 seconds if I'm right.

8 / 0.0000019 = 4,210,526 times faster. But it also had 100 times bigger loop, so we multiply that too: 4,210,526 * 100 = 421,052,600
Guess I mistyped 100s millions too.
In response to Ripiz
Ripiz wrote:
8 / 0.0000019 = 4,210,526 times faster. But it also had 100 times bigger loop, so we multiply that too: 4,210,526 * 100 = 421,052,600
Guess I mistyped 100s millions too.

wow that fast..
I can see how that could help.

So is it possible to call on a dll within a proc?
example i have a battle system with a more complex of a formula. Im pretty sure with the byond language this could cause lag issues. If a lot of people are using the fight proc throughout the world.
If i was able to call this forumla from a dll, could it help the problem?
In response to Ripiz
Just to get this right, you're telling me that your computer tackles on 1,000,000,000 additions, 2,000,000,000 multiplications and 1,000,000,000 square root function calls in 1.9 microseconds.
That is leaving everything else on the program aside.

Are you sure that your C++ compiler didn't optimise a LOT in this case and thus leaving the comparison horribly unfair?

Edit: Sorry, mixed up the prefix, thanks for correcting me there Stephen.
In response to Schnitzelnagler
1.9 micro-seconds, not nano-seconds, and 10,000,000 20,000,000 and 10,000,000 respectively, not including incrementers.
In response to Stephen001
Thanks for correcting me on the prefix, got that screwed up.

Though, I do not see how 1,000 * 1,000,000 is 10,000,000 (as you can see he edited the program to run the outer loop 1000 times, instead of 10 and profiled that to 1.9*106 seconds, according to his post).
In response to Tonyth
It wouldn't. I doubt your formula complex enough to be worth to send it to .dll
In response to Ripiz
Those numbers would put you approximately on par with an IBM Blue Gene supercomputer, if you counted the sqrt() function as a single floating-point operation (which it isn't). The compiler likely optimized away the entire loop, as it wasn't actually doing anything.
In response to Ripiz
mmmm, im still having problems with the dlls cause i dont get what stephen meant xD. Damn.
In response to Masschaos100
BYOND can call only C functions, not C++, C#, VB or etc. If you want other language you have to specify it's C either call it differently from DM, so you have to use this format:

extern "C" __declspec(dllexport) char * my_function(int argc, char ** argv);

extern "C" tells it's C code, not C++
__declspec(dllexport) tells it's accessible from outside
char* function will return char array
my_function is name of function
(int argc, char ** argv) function arguments (argc argument count, argv 2 dimensional array of data)