News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

sprintf_s in masm ?

Started by ewok, May 01, 2024, 02:15:05 AM

Previous topic - Next topic

ewok

Hello, i'm working with dll and i'm trying to recode this c++ to masm:
typedef struct test {
int a;
int b;
int c;
int d;
int e;
char* string;
} hello_struct;

extern "C" DllExport BOOL _cdecl myHelloAPI(hello_struct *hello) {
sprintf_s(hello->string, 12, "Hello World");
return 0;
}

i did my include to msvcrt, and did something like this:
.data
    dataString  db "Hello World", 0
    bufferSize  equ 12
   
    hello_struct     STRUCT
        a1           dd ?
        b2           dd ?
        c3           dd ?
        d4           dd ?
        e5           dd ?
        stringhw     db bufferSize DUP(?)
    hello_struct     ENDS
   
.code
myHelloAPI PROC, dataz:PTR hello_struct
;mov eax, [ebp+dataz]
;push offset dataString ;
;push bufferSize
;push dword ptr [eax+14h] ; Buffer
;call sprintf_s
;add esp, 0Ch
mov eax,0
    ret
myHelloAPI ENDP

I couldn't progress much beside that, forget the code inside the myHelloAPI proc, those are desperate broken attempts with copilot ai, cody ai, and chatgpt. it drive me crazy wasting time on this that looks simple.. .if anyone know how can i reproduce that using  sprintf_s or an equivalent.

sudoku

Is myHelloAPI a function that you want to export in a dll?
Could you post the full source of what you have already done?
Include your definition file (xxx.def) as well if that is the case. And batch file to create the dll.
Zip your project and attach it.
:eusa_naughty:

NoCforMe

One thing wrong: in the original structure definition,
    char* string;
is a pointer to a character string, not a string itself as you coded.

You should have this for that member instead:
     stringPtr  dd ?
Assembly language programming should be fun. That's why I do it.

HSE

Hi ewok!

I'm not sure sprintf_s function is in Masm32 SDK libraries, but at list will work the more simple "crt_sprintf".

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                    Build this console app with
                  "MAKEIT.BAT" on the PROJECT menu.
        ----------------------------------------------------- *

    .data
      format1 db " There are %i things", 0
    .code

start:
 
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc
    local buffer[248]:byte

    cls
    invoke crt_sprintf, addr buffer, addr format1, 15
    print addr buffer, 13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

Regards, HSE
Equations in Assembly: SmplMath

ewok

>Is myHelloAPI a function that you want to export in a dll?
Yes!

i couldn't find where to attach a zip so here is it hosted on workupload https://workupload.com/file/G7PsKrNHudD
Thanks HSE, if i remember i tried also to implement crt_sprintf but it wasn't good.

NoCforMe

Quote from: ewok on May 01, 2024, 03:07:30 AMi couldn't find where to attach a zip

If you don't see "Click or drag files here to attach them." at the bottom of your post when you're writing it, click "Preview" and it shall magically appear.
Assembly language programming should be fun. That's why I do it.

sudoku

#6
Quote from: ewok on May 01, 2024, 03:07:30 AMi couldn't find where to attach a zip so here is it hosted on workupload
I have attached it to my post here. attachment no longer needed here.
At the bottom of the edit window (when making a post) you should see "Click or drag files here to attach them"

Your source code is below... see my comment there...
Also msvcrt.inc should be before msvcrt.lib, I changed it in the source below
The include (.inc) file should always be before the library (.lib) file

.486
option    casemap :none ; case sensitive


    include \masm32\include\masm32rt.inc
    include \masm32\include\windows.inc
    include \masm32\include\msvcrt.inc  ; Include the C runtime library
    includelib msvcrt.lib  ; Include the C runtime library

_DllMainCRTStartup PROTO :DWORD,:DWORD,:DWORD
myHelloAPI PROTO :DWORD,:DWORD

.data
    dataString  db "Hello World", 0
    bufferSize  equ 12
 
    hello_struct    STRUCT
        a1          dd ?
        b2          dd ?
        c3          dd ?
        d4          dd ?
        e5          dd ?
        stringhw    db bufferSize DUP(?)  ;;; should be a pointer to the string, as NoCforme mentioned above
    hello_struct    ENDS

.code

_DllMainCRTStartup proc instance:DWORD,reason:DWORD,unused:DWORD
    mov    eax,1
    ret
_DllMainCRTStartup endp

myHelloAPI proc q:DWORD,w:DWORD
        ;mov eax, [ebp+dataz]
        ;push offset dataString ;
        ;push bufferSize
        ;push dword ptr [eax+14h] ; Buffer
        ;call sprintf_s
        ;add esp, 0Ch
    mov    eax,0
    ret
myHelloAPI endp

end
Your definition file looks ok.

What are the arguments  "q:DWORD,w:DWORD" for?
At any rate, the dll builds with the batch file in the zip archive. Fix the structure member and put the .inc declaration before the .lib declaration for msvcrt to see if those are the only issues that you have.
:eusa_naughty:

sudoku

Quote from: HSE on May 01, 2024, 03:02:38 AMI'm not sure sprintf_s function is in Masm32 SDK libraries
Definitely not... sprintf_s  :cool:
:eusa_naughty:

sudoku

main.asm(38) : error A2006: undefined symbol : sprintf_sseems that sprintf_s is not in msvcrt.dll, at least on Windows 7. There are other alternatives as HSE mentioned above.
:eusa_naughty:

HSE

Quote from: sudoku on May 01, 2024, 04:01:56 AMDefinitely not... sprintf_s  :cool:

Is working here with:
    externdef _imp__sprintf_s:PTR c_msvcrt
    crt_sprintf_s equ <_imp__sprintf>

but I think the linker load that function from a newer msvcrt.lib (version 12), but that have to work in Win7
Equations in Assembly: SmplMath

sudoku

Quote from: HSE on May 01, 2024, 04:20:29 AMIs working here with:
    externdef _imp__sprintf_s:PTR c_msvcrt
    crt_sprintf_s equ <_imp__sprintf>

Okay, it does work here with that, on Windows 7. .
My mistake. It does not run in Windows 7 even with the exterdef, I was still using "printf" in the test code, forgot to change it back to sprintf_s.   :rolleyes:

So apparently sprintf_s is only on later versions of msvcrt than what is in Windows 7.

What version of Windows are you running, ewok?
:eusa_naughty:

NoCforMe

Hey, here's an idea: forget about sprintf(). Just use wsprintf() instead, which is a standard Win32 function, that (probably) does what you need it to do (i.e, format a string to a memory buffer).

Documentation here.
Assembly language programming should be fun. That's why I do it.

Vortex

Hello,

sprintf_s works on Windows 7. Attached is a quick example. The problem is that the import library msvcrt.lib built by the Masm32 package does not export this function :

\PellesC\bin\podump.exe /SYMBOLS G:\masm32\lib\msvcrt.lib | findstr sprintf
0003 00000000 SECT1  notype ()   external     | _sprintf
0006 00000000 SECT2  notype      external     | __imp__sprintf
0003 00000000 SECT1  notype ()   external     | _vsprintf
0006 00000000 SECT2  notype      external     | __imp__vsprintf

An easy solution is to rebuild the library.

.386
.model flat, stdcall
option casemap :none

include     \masm32\include\windows.inc
include     \masm32\include\user32.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\masm32.lib
includelib  msvcrt.lib

sprintf_s PROTO C :DWORD,:VARARG

SIZE_OF_BUFFER equ 64

.data

a1  db 'This is',0
a2  db 'a test.',0
a3  db '%s %s',0

.data?

buffer  db SIZE_OF_BUFFER dup(?)

.code

start:

    invoke  sprintf_s,ADDR buffer,\
            SIZE_OF_BUFFER,\
            ADDR a3,ADDR a1,ADDR a2

    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start

sudoku

Oh, Vortex.  :thumbsup:
I was correct in my initial assumption that hutch missed that one in the include file for msvcrt. Bravo.
I had not thought about rebuilding the library, leading me to believe it did not exist in Windows 7 msvcrt.dll.

Under the hood, msvcrt calls "vsprintf_s" in case anyone is interested


:eusa_naughty:

jj2007

Works like a charm on Windows 10:
include \masm32\MasmBasic\MasmBasic.inc
.DATA?
buffer db 10 dup(?)
  Init
  Dll "msvcrt"
  Declare sprintf_s, C:?
  Print Str$("%i characters copied", sprintf_s(addr buffer, sizeof buffer, "%i", 123456789))
  MsgBox 0, offset buffer, "Result:", MB_OK
EndOfCode