jdhedden has asked for the wisdom of the Perl Monks concerning the following question:
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
(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.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Random Numbers under XP: Translating XS to Win32::API
by BrowserUk (Patriarch) on Jun 29, 2005 at 19:34 UTC | |
by jdhedden (Deacon) on Jun 29, 2005 at 20:05 UTC | |
by BrowserUk (Patriarch) on Jun 29, 2005 at 20:20 UTC |