Core Function DLLStructCreate

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
Line 40: Line 40:
 
<pre>
 
<pre>
 
[TYPE] [WHAT IT RETURNS WITH DLLSTRUCTGETDATA] [WHAT IS IT IN C++]
 
[TYPE] [WHAT IT RETURNS WITH DLLSTRUCTGETDATA] [WHAT IS IT IN C++]
ubyte unsigned 8 bit integer char
+
ubyte unsigned 8 bit integer unsigned char
 
byte signed 8 bit integer char
 
byte signed 8 bit integer char
 
char a single ASCII character char
 
char a single ASCII character char
 +
wchar a single UNICODE character wchar
 
short signed 16 bit integer short
 
short signed 16 bit integer short
 
int16 signed 16 bit integer short
 
int16 signed 16 bit integer short
Line 48: Line 49:
 
int32 signed 32 bit integer int
 
int32 signed 32 bit integer int
 
int64 signed 64 bit integer __int64
 
int64 signed 64 bit integer __int64
 +
long signed 64 bit integer __int64
 
ushort signed unsigned 16 bit integer unsigned short
 
ushort signed unsigned 16 bit integer unsigned short
 
uint16 signed unsigned 16 bit integer unsigned short
 
uint16 signed unsigned 16 bit integer unsigned short
Line 53: Line 55:
 
uint32 signed unsigned 32 bit integer unsigned int
 
uint32 signed unsigned 32 bit integer unsigned int
 
uint64 signed unsigned 64 bit integer unsigned __int64
 
uint64 signed unsigned 64 bit integer unsigned __int64
 +
ulong signed unsigned 64 bit integer unsigned __int64
 
float signed 32 bit floating point float
 
float signed 32 bit floating point float
 
double signed 64 bit floating point double
 
double signed 64 bit floating point double
 +
ptr a pointer void*
  
 
Note - There is no *string* type instead;
 
Note - There is no *string* type instead;
 
You must create an array of chars example "char mystring[500]"
 
You must create an array of chars example "char mystring[500]"
 
then you can use it like a string.
 
then you can use it like a string.
 +
 +
Or if you wish to use UNICODE its "wchar mystring[500]"
 
</pre>
 
</pre>
  

Revision as of 15:47, 7 December 2011

DLLStructCreate( <def string> )

Contents

Description

Creates a C/C++ style structure to be used with DLLCall

Parameters

def string

A string representing the structure to create (See Remarks).

Return Value

Success - Returns the DLLStruct.

Failure - Returns 0 and most likely throws exception.

Remarks

To delete a DLLStruct you must unset it example:

unset( $dllStruct );

Note - If you do not unset the DLLStruct and instead just change the value of the variable then the garbage collector will deal with the DLLStruct *evenutally* but that could be today, tomorrow you never know so always unset() the struct when your finished with it.

DEFS

Each data type must be separated by a semi-colon ';'.

Create arrays by adding '[size]' after the data type.

DllStructCreate("int;char[128]").

An elementname can be added similar to a C-style declaration DllStructCreate("int n;char buffer[128]").

def string settings

[TYPE]		[WHAT IT RETURNS WITH DLLSTRUCTGETDATA]		[WHAT IS IT IN C++]
ubyte		unsigned 8 bit integer				unsigned char
byte		signed 8 bit integer				char
char		a single ASCII character			char
wchar		a single UNICODE character			wchar
short		signed 16 bit integer				short
int16		signed 16 bit integer				short
int		signed 32 bit integer				int
int32		signed 32 bit integer				int
int64		signed 64 bit integer				__int64
long		signed 64 bit integer				__int64
ushort		signed unsigned 16 bit integer			unsigned short
uint16		signed unsigned 16 bit integer			unsigned short
uint		signed unsigned 32 bit integer			unsigned int
uint32		signed unsigned 32 bit integer			unsigned int
uint64		signed unsigned 64 bit integer			unsigned __int64
ulong		signed unsigned 64 bit integer			unsigned __int64
float		signed 32 bit floating point			float
double		signed 64 bit floating point			double
ptr		a pointer					void*

Note - There is no *string* type instead;
You must create an array of chars example "char mystring[500]"
then you can use it like a string.

Or if you wish to use UNICODE its "wchar mystring[500]"

Example

/*=========================================================
   Create the struct
   struct {
       int             var1;
       unsigned char   var2;
       unsigned int    var3;
       char            var4[128];
   }
=========================================================*/
 
$str		= "int var1;ubyte var2;uint var3;char var4[128]";
$a			= DllStructCreate($str);
if ( !$a )
{
	MsgBox("Error in DllStructCreate");
	exit();
}
 
/*=========================================================
	Set data in the struct
	struct.var1	= -1;
	struct.var2	= 255;
	struct.var3	= 777;
	strcpy(struct.var4,"Hello");
	struct.var4[0]	= 'h';
=========================================================*/
DllStructSetData($a,"var1",-1);
DllStructSetData($a,"var2",255);
DllStructSetData($a,"var3",777);
DllStructSetData($a,"var4","Hello");
DllStructSetData($a,"var4","G",0);
 
/*=========================================================
	Display info in the struct
;=========================================================*/
MsgBox("Struct Size: " . DllStructGetSize($a) . @CRLF .
		"Struct pointer: " . DllStructGetPtr($a) . @CRLF .
		"Data:" . @CRLF .
		DllStructGetData($a,"var1") . @CRLF .
		DllStructGetData($a,"var2") . @CRLF .
		DllStructGetData($a,"var3") . @CRLF .
		DllStructGetData($a,"var4"),
		"DllStruct");

Heres an example of using DLLStruct

// Create the struct
$a = DLLStructCreate("int x; int y");
// Print its info just for sake of it
println( "DLLStruct info: " . $a );
 
// Call "GetCursorPos" API and send the struct to it using the "t" param
$ret = DLLCall("", "GetCursorPos", "Int32", "t", $a);
 
if($ret)
{
	// Get the X and Y from the struct
	$x = DLLStructGetData($a, "x");
	$y = DLLStructGetData($a, "y");
	// Print it
	println("X '$x' Y '$y'");
}
else
{
	println("GetCursorPos() api failed...");
}

Another DLLStruct example

// Create the struct
$a = DLLStructCreate("char x[500]; char y[500]; char z[500]");
// Print its info just for sake of it
println( "DLLStruct info: " . $a );
 
// Zero term the strings just to be sure
DLLStructSetData($a, "x", "\0", 0);
DLLStructSetData($a, "y", "\0", 0);
DLLStructSetData($a, "z", "\0", 0);
 
// Call a few apis
DLLCall("", "wsprintf", "", "tpii", DLLStructGetPtr($a, "x"), "Number is '%d' in hex '%x'", 100, 200);
DLLCall("", "wsprintf", "", "tpii", DLLStructGetPtr($a, "y"), "Number is '%d' in hex '%x'", 1337, 4242);
DLLCall("", "wsprintf", "", "tpp", DLLStructGetPtr($a, "z"), "STR '%s'", DLLStructGetData($a, "x"));
// This time lets use lstrcat to append text to our strings
DLLCall("", "lstrcat", "", "tp", DLLStructGetPtr($a, "z"), " | Added to string");
DLLCall("", "lstrcat", "", "tp", DLLStructGetPtr($a, "z"), "; Also added...");
 
// Print it
println(   DLLStructGetData($a, "x")   );
println(   DLLStructGetData($a, "y")   );
println(   DLLStructGetData($a, "z")   );
println(   "Length of X is: " . DLLCall("", "lstrlen", "Int32", "t", DLLStructGetPtr($a, "x"))   );
println(   "Length of Y is: " . DLLCall("", "lstrlen", "Int32", "t", DLLStructGetPtr($a, "y"))   );
println(   "Length of Z is: " . DLLCall("", "lstrlen", "Int32", "t", DLLStructGetPtr($a, "z"))   );

Yet Another DLLStruct example

This time 2 ToolTips just around the screen randomly

// Spawn 2 tooltips
$a = ToolTip("Hello World!!!!");
$b = ToolTip("Hello World!!!! FROM ME TOO!!!");
 
// Create a struct to handle a GetWindowRect api call
$Rect = DLLStructCreate("int left; int top; int right; int bottom");
 
// Infinite loop
while(true)
{
	// Move the first tooltip
	DLLCall("", "GetWindowRect", "", "tt", $a, $Rect);
	$Width = DLLStructGetData($Rect, "right") - DLLStructGetData($Rect, "left");
	$Height = DLLStructGetData($Rect, "bottom") - DLLStructGetData($Rect, "top");
	$NewX = Random(1, @DesktopWidth);
	$NewY = Random(1, @DesktopHeight);
	DLLCall("", "MoveWindow", "", "tiiiii", $a, $NewX, $NewY, $Width, $Height, 1);
 
	// Move the first second
	DLLCall("", "GetWindowRect", "", "tt", $b, $Rect);
	$Width = DLLStructGetData($Rect, "right") - DLLStructGetData($Rect, "left");
	$Height = DLLStructGetData($Rect, "bottom") - DLLStructGetData($Rect, "top");
	$NewX = Random(1, @DesktopWidth);
	$NewY = Random(1, @DesktopHeight);
	DLLCall("", "MoveWindow", "", "tiiiii", $b, $NewX, $NewY, $Width, $Height, 1);
 
	// Sleep a little
	sleep(500);
}

A very large DLLCall + DLLStruct example :

$myString = WSPrintf('Testing "%c%c%c" %d | %u hehe %d....%s %f', 'A', 'B', 'B', 12, 131, 200, "omg", 133.77);
println($myString);
 
// Use the sprintf from msvcrt as an example for DLLCall
// Havent added uint64/int64 support yet
// Of course we could have just built the new string as we found params
// but that wouldnt be using dll then would it?
// Also this demonstrates the Eval() function to dynamically create Sputnik code
Function WSPrintf( $Format )
{
	$Args = array();
	$Len = StrLen($Format);
	$EstMaxLen = $Len;
	$LastC = '';
	$DLLParDef = "";
	$DLLPars = "";
	$b64Bit = false;
	for(my $i = 0, my $j = 1; $i < $Len; $i++)
	{
		$char = $Format[$i];
		switch ($char)
		{
			case '%':
			{
				if($LastC == "\\") break;
				$i++;
				if($i >= $Len)
				{
					throw("Format string is invalid");
					return 0;
				}
				$char = $Format[$i];
				switch ($char)
				{
					case '%':
						{
							$b64Bit = false;
						}
						break;
					case 'l':
					case 'L':
						{
							$b64Bit = true;							
						}
						break;
					case 'c':
						{
							$EstMaxLen += 2;
							$DLLParDef .= "b";
							$DLLPars .= ', (Char)@args[' . $j . ']';
							$j++;
							$b64Bit = false;
						}
						break;
					case 'd':
					case 'i':
					case 'o':
					case 'p':
						{
							if($b64Bit)
							{
								$EstMaxLen += 20;
								$DLLParDef .= "l";
								$DLLPars .= ', (Int64)@args[' . $j . ']';
							}
							else
							{
								$EstMaxLen += 11;
								$DLLParDef .= "i";
								$DLLPars .= ', (Int32)@args[' . $j . ']';
							}
							$j++;
							$b64Bit = false;
						}
						break;
					case 'o':
					case 'u':
					case 'x':
					case 'X':
						{
							if($b64Bit)
							{
								$EstMaxLen += 20;
								$DLLParDef .= "L";
								$DLLPars .= ', (UInt64)@args[' . $j . ']';
							}
							else
							{
								$EstMaxLen += 11;
								$DLLParDef .= "I";
								$DLLPars .= ', (UInt32)@args[' . $j . ']';
							}
							$j++;
							$b64Bit = false;
						}
						break;
					case 'f':
					case 'g':
					case 'e':
					case 'E':
						{
							$EstMaxLen += 20;
							$DLLParDef .= "d";
							$DLLPars .= ', (double)@args[' . $j . ']';
							$j++;
							$b64Bit = false;
						}
						break;
					case 's':
						{
							$EstMaxLen += StrLen(@Args[$j] + 1);
							$DLLParDef .= "p";
							$DLLPars .= ', (string)@args[' . $j . ']';
							$j++;
							$b64Bit = false;
						}
						break;
					default:
						{
							throw("Invalid specifier '$char'");
							return 0;
						}
					break;
				}
			}
			break;
		}
		$LastC = $char;
	}	
	// Create the correct size struct which will hold the newly made string
	// It should have been calculated to a good degree of accuracy so we
	// Dont really expect any buffer overflows here
	$Struct = DLLStructCreate("char buf[$EstMaxLen];");
	// Create the DLL call string
	$CallString = 'DLLCall("msvcrt.dll", "sprintf", "cdecl Int32", "tp' . $DLLParDef . '", $Struct, $Format' . $DLLPars . ');';
	$RetVal = Eval($CallString); // Dynamically call the dll using the string as if it was physical Sputnik code
	if(Eval($CallString) == 0)
	{
		unset($Struct); // Cleanup the Struct
		return 0;
	}
	$newStr = DLLStructGetData($Struct, "buf");
	unset($Struct); // Cleanup the Struct
	return $newStr;
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox