Core Function DLLCall

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> DLLImport( <DLLName>, <FunctionName/Alias>, <ReturnType>, <ParameterType>, <CharSet/CallingConvention> ) </pre> === Description === See [[Core Function DLLImport|DLLImpor...")
 
Line 1: Line 1:
 
<pre>
 
<pre>
DLLImport( <DLLName>, <FunctionName/Alias>, <ReturnType>, <ParameterType>, <CharSet/CallingConvention> )
+
DLLCall( <Dll/DLLName>, <FunctionName/Alias>, <ReturnType>, <ParameterType>, <CharSet/CallingConvention>, <Parms> )
 
</pre>
 
</pre>
  
 
=== Description ===
 
=== Description ===
  
See [[Core Function DLLImport|DLLImport( <varies...> )]] for description
+
Dynamically call a function in a DLL
  
 
=== Parameters ===
 
=== Parameters ===
Line 11: Line 11:
 
==== DLLName ====
 
==== DLLName ====
  
The name of the DLL you want to import a function from
+
See [[Core Function DLLImport|DLLImport( <varies...> )]] for DLLName
  
==== FunctionName/Alias ====
+
You can optionally insert the return value from a DLLOpen() here instead of the DLLName()
  
The function you wish to import from the DLL.
+
In which case you ignore all other parameters until you hit the <Params>
  
You can add an alias to the function for example if you want to import printf from msvcrt.dll but Sputnik already contains a printf you must alias the function by entering it like so:
+
==== FunctionName/Alias ====
  
"printf:myprintf" // This will import printf as normal but it will callable only by using the name myprintf
+
See [[Core Function DLLImport|DLLImport( <varies...> )]] for FunctionName/Alias
  
 
==== ReturnType ====
 
==== ReturnType ====
  
What type of return does this function do choices are:
+
See [[Core Function DLLImport|DLLImport( <varies...> )]] for ReturnType
 
+
<pre>
+
void
+
null
+
pointer
+
byte
+
sbyte
+
char
+
wchar
+
int16
+
int32
+
int64
+
uint16
+
uint32
+
uint64
+
float
+
double
+
string
+
*string
+
char*
+
</pre>
+
  
 
==== ParameterType ====
 
==== ParameterType ====
  
How many and what type of parameters does this function do choices are:
+
See [[Core Function DLLImport|DLLImport( <varies...> )]] for ParameterType
  
<pre>
+
==== CharSet/CallingConvention ====
[TYPE] [Description]
+
n NULL
+
b SByte
+
B Byte
+
s Int16
+
S UInt16
+
i Int32
+
I UInt32
+
l Int64
+
L UInt64
+
f Float
+
d Double
+
p String
+
t (IntPtr) Pointer (A numeric value but is an address of something)
+
T (UIntPtr) Pointer (A numeric value but is an address of something)
+
* Pointer READ BELOW:
+
% Array READ BELOW:
+
// Examples
+
ippi // Send Int32, String, String Int32
+
ip*pi // Send Int32, String, POINTER String Int32
+
fid // Send Float, Int32, Double
+
fi*d // Send Float, Int32, POINTER Double
+
i%pp // Send Int32, ARRAY String, String
+
  
About pointers:
+
See [[Core Function DLLImport|DLLImport( <varies...> )]] for CharSet/CallingConvention
The variable is passed as a POINTER and if the variable is modified in the DLL it modifies the actual Sputnik variable at the same time.
+
  
So if you send a $var as a *p string pointer then the DLL function runs lower case on the string the string in your $var will have changed too (Same for all data types including doubles, ints etc).
+
=== Return Value ===
  
About arrays:
+
See [[Core Function DLLImport|DLLImport( <varies...> )]] for return value
Currently arrays CANNOT also be passed as pointers (modifying the array in your DLL does not modify the array in Sputnik but evenutally I will make it so it does).
+
  
This allows you to send arrays of objects for example %p sends an array of strings such as array("one", "two", "three");
+
=== Params ===
  
You can send arrays of everything even doubles and ints etc.
+
None, one or more than one parameter to pass to the DLL function.
  
You cannot use ARRAY and POINTER at to the same variable example: %*p but you can use one or the other
+
=== Remarks ===
  
Your $variable data is converted into the type you specify upon calling the function so if you put "d" for double and your variable contains a string it will convert it to a double.
+
DLLCall() is basically a wrapper for DLLImport() it will import the DLL function and immediately call it.
</pre>
+
  
==== CharSet/CallingConvention ====
+
You can use DLLOpen() to store a previously opened DLL function so you can call it over and over again with DLLCall which is significantly after than just using DLLCall() alone.
 
+
Here you define what char set and optionally what calling convention to use.
+
 
+
Choices are:
+
<pre>
+
"" // No char set will be used
+
"Void" // No char set will be used
+
"Ansi"
+
"Unicode"
+
"Auto" // It will try decide what it wants to use
+
// Now you can optionally decide if you want a
+
// calling convention for example if you are using a
+
// C/C++ DLL you need to use cdecl as the calling convention
+
// Choices of calling convention are:
+
cdecl
+
fastcall
+
stdcall
+
thiscall
+
winapi
+
// You must add them to the string using the : for example:
+
"Ansi:cdecl" // Use Ansi char set and cdecl calling convention
+
"Unicode:stdcall" // Use Unicode char set and stdcall calling convention
+
</pre>
+
 
+
=== Return Value ===
+
 
+
See [[Core Function DLLImport|DLLImport( <varies...> )]] for return value
+
 
+
=== Remarks ===
+
  
None.
+
The reason it is faster to open it with DLLOpen() first (or just use DLLImport instead) is because since Sputnik is made in C# there is no physical way for it to dynamically call DLLs at runtime without using a C/C++ DLL. (which I could include with Sputnik but choose not to to keep the amount of DLLs needed to a minimal) So Sputnik has to physically compile the DLLCall internally in its RAM as an object it can call and the compiler take takes a fraction of a second but obviously too slow for thousands of calls a second. Therefor it's best to use DLLOpen() or DLLImport() so the compile only takes place ONCE making the Call part extremely fast just like any other function.
  
 
=== Example ===
 
=== Example ===

Revision as of 14:25, 12 September 2013

DLLCall( <Dll/DLLName>, <FunctionName/Alias>, <ReturnType>, <ParameterType>, <CharSet/CallingConvention>, <Parms> )

Contents

Description

Dynamically call a function in a DLL

Parameters

DLLName

See DLLImport( <varies...> ) for DLLName

You can optionally insert the return value from a DLLOpen() here instead of the DLLName()

In which case you ignore all other parameters until you hit the <Params>

FunctionName/Alias

See DLLImport( <varies...> ) for FunctionName/Alias

ReturnType

See DLLImport( <varies...> ) for ReturnType

ParameterType

See DLLImport( <varies...> ) for ParameterType

CharSet/CallingConvention

See DLLImport( <varies...> ) for CharSet/CallingConvention

Return Value

See DLLImport( <varies...> ) for return value

Params

None, one or more than one parameter to pass to the DLL function.

Remarks

DLLCall() is basically a wrapper for DLLImport() it will import the DLL function and immediately call it.

You can use DLLOpen() to store a previously opened DLL function so you can call it over and over again with DLLCall which is significantly after than just using DLLCall() alone.

The reason it is faster to open it with DLLOpen() first (or just use DLLImport instead) is because since Sputnik is made in C# there is no physical way for it to dynamically call DLLs at runtime without using a C/C++ DLL. (which I could include with Sputnik but choose not to to keep the amount of DLLs needed to a minimal) So Sputnik has to physically compile the DLLCall internally in its RAM as an object it can call and the compiler take takes a fraction of a second but obviously too slow for thousands of calls a second. Therefor it's best to use DLLOpen() or DLLImport() so the compile only takes place ONCE making the Call part extremely fast just like any other function.

Example

Example of using DLLCall() with all its parameters

// Setup a few variables
$MB_YESNO = 0x04;
$MB_ICONINFORMATION = 0x40;
$IDYES = 6;
// Make the call
$RetVal = DLLCall(	'User32.dll', 'MessageBox', 'Int32', 'ippi', 'Unicode',
					0, "Hello There", "Title", $MB_YESNO | $MB_ICONINFORMATION
				);
If ( $RetVal == $IDYES )
{
	println("YES was pressed");
}
else
{
	println("NO was pressed");
}

Example of using DLLCall() with minimal parameters by using DLLOpen() to avoid having to redefine the function

// Store the DLL function to call later
$Call = DLLOpen('User32.dll', 'MessageBox', 'Int32', 'ippi', 'Unicode');
// Setup a few variables
$MB_YESNO = 0x04;
$MB_ICONINFORMATION = 0x40;
$IDYES = 6;
// Make the call
$RetVal = DLLCall($Call, 0, "Hello There", "Title", $MB_YESNO | $MB_ICONINFORMATION);
If ( $RetVal == $IDYES )
{
	println("YES was pressed");
}
else
{
	println("NO was pressed");
}

Example of using DLLCall() with the Library class "API" from "Win32" folder this class provides a wrapper for DLLOpen() DLLImport() DLLClose() and DLLCall() all rolled into one it works similar to how Perl calls APIs.

// Include the API class
Include('Win32/API.spk');
 
// Store the DLL function to call later
my $Call = new API('User32.dll', 'MessageBox', 'Int32', 'ippi', 'Unicode');
// Setup a few variables
$MB_YESNO = 0x04;
$MB_ICONINFORMATION = 0x40;
$IDYES = 6;
// Make the call
$RetVal = $Call->Call(0, "Hello There", "Title", $MB_YESNO | $MB_ICONINFORMATION);
If ( $RetVal == $IDYES )
{
	println("YES was pressed");
}
else
{
	println("NO was pressed");
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox