Jump to content

Calling Random number generator srand in msvcrt.dll


Recommended Posts

I'm trying to call the srand function in the msvcrt.dll.

It is defined as 

void srand(
   unsigned int seed 
);

My sample code is:

Local $aResult
Local $h_DLL = DllOpen("msvcrt.dll")
If $h_DLL <> -1 Then
    Local $uint = DllStructCreate("uint")
    DllStructSetData($uint, 1, 54321)

    ConsoleWrite('@@'  & @ScriptLineNumber & ' uint[' & DllStructGetSize($uint) & ']:' & DllStructGetData($uint, 1) & @CRLF)
    $aResult = DllCall($h_DLL, "none", "srand", "int", $uint)
    ConsoleWrite('@@'  & @ScriptLineNumber & ' srand done' & @CRLF)

    If @error Then
        ConsoleWrite('@@'  & @ScriptLineNumber & ' srand Error:' & @error & @CRLF)
    EndIf
Else
    ConsoleWrite('@@'  & @ScriptLineNumber & ' srand DLL Error' & @CRLF)
EndIf

When I run it I get:

>Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\AutoIt3\PasswordTool\Tweaks\srand.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
@@7 uint[4]:54321
!>15:15:08 AutoIt3.exe ended.rc:-1073741819
+>15:15:08 AutoIt3Wrapper Finished.
>Exit code: 3221225477    Time: 0.6205

Which indicates an Access violation.  Using windbg, I can see in the assembly that it is trying to do a PTR on a 0x00 value.

Searching the web, I found a reference to what the srand routine is trying to do:

 
void __cdecl srand (unsigned int seed)
{
    _getptd()->_holdrand = (unsigned long)seed;
}

int __cdecl rand (void)
{
    _ptiddata ptd = _getptd();
    return ( ((ptd->_holdrand = ptd->_holdrand * 214013L + 2531011L) >> 16) & 0x7fff );
}

Do I need to do something to the msvcrt.dll library before it can reference any global pointers.  I'm assuming that _getptd() must reference some global pointer?

Link to comment
Share on other sites

This seems to work better:

Local $aResult
Local $h_DLL = DllOpen("msvcrt.dll")
If $h_DLL <> -1 Then
    Local $uint = 54321
    $aResult = DllCall($h_DLL, "none:cdecl", "srand", "uint", $uint)
    If @error Then
        ConsoleWrite('@@'  & @ScriptLineNumber & ' srand Error:' & @error & @CRLF)
    Else
        ConsoleWrite('@@'  & @ScriptLineNumber & ' srand done' & @CRLF)
    EndIf
Else
    ConsoleWrite('@@'  & @ScriptLineNumber & ' msvcrt.dll Error' & @CRLF)
EndIf

Pass a uint direct, not within a struct, specify cdecl calling convention and beware that ConsoleWrite would reset @error.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...