News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

msvcrt undefined function

Started by jimg, April 12, 2020, 01:26:09 AM

Previous topic - Next topic

jimg

What is required to use a function from the c runtime lib that is not declared in masm32's msvcrt.inc?
Specifically, I would like to use rand_s (because it returns 32 bits rather than 31 bits like nrandom and rand).
Or if anyone can recommend a full 32 bit random number generator, that would work also.  Thanks.

Vortex

Hi Jim,

The version of msvcrt.dll shipped with Windows 7 exports rand_s :

G:\PellesC\Bin>podump.exe /exports C:\Windows\System32\msvcrt.dll | findstr "rand"
            496   495  000007FF756A1980  rand
            497   496  000007FF756E5D74  rand_s
            4AB   4AA  000007FF756A40BC  srand


Creating a new msvcrt.lib :

G:\PellesC\Bin>polib.exe /OUT:msvcrt.lib /MACHINE:x86 C:\Windows\System32\msvcrt.dll

rand_s PROTO C :DWORD

jimg

Thanks for that.  I was hoping it was just a matter of definition in masm32's msvcrt.inc to use the one in msvcrt.lib.   Guess I have to make a new msvcrt.lib for masm32?

Vortex

Hi Jim,

You can use polib.exe to create a new copy of msvcrt.lib :

\masm32\bin\polib.exe /OUT:msvcrt.lib /MACHINE:x86 C:\Windows\System32\msvcrt.dll

Pelle's librarian polib comes also with the Pelles C installation.

jj2007

LoadLibrary and GetProcAddress are your friends, too.

jimg

Well...  I wouldn't call them my friends, but we can get along with a little effort.

mineiro

Quote from: jimg on April 12, 2020, 01:26:09 AM
Or if anyone can recommend a full 32 bit random number generator, that would work also.  Thanks.
I was looking to this, maybe can be usefull to you:
https://www.agner.org/random/
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

Vortex

Quoterand_s depends on the RtlGenRandom API, which is only available in Windows XP and later.

https://docs.microsoft.com/en-us/previous-versions/sxtz2fa8(v%3Dvs.140)

About the RtlGenRandom function :

QuoteNote  This function has no associated import library. This function is available as a resource named SystemFunction036 in Advapi32.dll. You must use the LoadLibrary and GetProcAddress functions to dynamically link to Advapi32.dll.

https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom?redirectedfrom=MSDN

Here is a quick RtlGenRandom test :

include     \masm32\include\masm32rt.inc

include     invoke.inc

.data

kernel32        db 'kernel32.dll',0
advapi32        db 'advapi32.dll',0
SystemFn036     db 'SystemFunction036',0
RtlSecZeroMem   db 'RtlSecureZeroMemory',0
crlf            db 13,10,0   
s1              db 'Random Byte = %u',13,10,0
s2              db 'Random Dword = %u',13,10,0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC USES esi ebx

LOCAL hDll:DWORD
LOCAL RtlGenRandom:DWORD
LOCAL buffer[12]:BYTE

    lea     esi,buffer
    invoke  LoadLibrary,ADDR advapi32
    mov     hDll,eax

    invoke  GetProcAddress,eax,\
            ADDR SystemFn036
           
    mov     RtlGenRandom,eax

    push    esi
   _invoke  RtlGenRandom,esi,12

;   Print 12 random BYTE values

    mov     ebx,12
@@:
    movzx   eax,BYTE PTR [esi]
    invoke  crt_printf,ADDR s1,eax
    inc     esi
    dec     ebx
    jnz     @b

    pop     esi
   _invoke  RtlGenRandom,esi,12

;   Print 3 random DWORD values

    invoke  crt_printf,ADDR crlf

    mov     ebx,12/4
@@:
    invoke  crt_printf,ADDR s2,\
            DWORD PTR [esi]
           
    add     esi,4
    dec     ebx
    jnz     @b

    invoke  FreeLibrary,hDll

    ret
   
main ENDP

END start

nidud

#8
deleted

jimg