JohnWIlling

Calling Random number generator srand in msvcrt.dll

4 posts in this topic

#1 ·  Posted

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?

Share this post


Link to post
Share on other sites



#2 ·  Posted

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)

Share this post


Link to post
Share on other sites

#3 ·  Posted

That worked great.

So when does one use just "none" vs. "none:cdecl"?

Share this post


Link to post
Share on other sites

#4 ·  Posted

Found it in the documentation for DllCall...

By default, AutoIt uses the 'stdcall' calling method. To use the 'cdecl' method place ':cdecl' after the return type.

Share this post


Link to post
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