According to Michael Howard of Microsoft, you can obtain true random numbers under Windows XP without having to deal with their full-blown CryptAPI.

Using XS, I have written a function to return an array ref of random ints using the referenced API.

#include <windows.h> SV * random(count) int count CODE: /* REFERENCE: http://blogs.msdn.com/michael_howard/archive/2005/01/14/353379 +.aspx */ /* Open the library */ HMODULE lib = LoadLibrary("ADVAPI32.DLL"); if (! lib) { Perl_croak(aTHX_ "ADVAPI32.DLL not found"); } /* 'Extract' the function */ BOOLEAN (APIENTRY *RtlGenRandom)(void*, ULONG) = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(lib, "SystemFunct +ion036"); if (! RtlGenRandom) { FreeLibrary(lib); Perl_croak(aTHX_ "RtlGenRandom() not found in ADVAPI32.dll"); } /* Set up a temporary buffer */ U32 *buff = (U32 *)malloc(count * sizeof(U32)); /* Get the random data */ if (! RtlGenRandom(buff, (ULONG)(count * sizeof(U32)))) { free(buff); FreeLibrary(lib); Perl_croak(aTHX_ "RtlGenRandom() failed"); } /* Copy the data to a Perl array */ AV *array = newAV(); int ii; for (ii=0; ii<count; ii++) { av_push(array, newSVuv(buff[ii])); } /* Cleanup */ free(buff); FreeLibrary(lib); /* Return an array ref */ RETVAL = newRV((SV *)array); OUTPUT: RETVAL
I would like to have a Perl version of this that uses Win32::API. The problem is that I use Perl under Cygwin, and Win32::API won't compile under Cygwin. Would someone be so good as to assist in translating this for me? Thank you.

(Yes, I know that /dev/random is available under Cygwin. I want to provide the code to others that are using Perl on PCs, but are not using Cygwin.)


Remember: There's always one more bug.

In reply to Random Numbers under XP: Translating XS to Win32::API by jdhedden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.