News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

trouble in covert WriteLog from c++ to masm

Started by laomms, June 12, 2007, 02:54:10 AM

Previous topic - Next topic

laomms

void WriteLog(char *fmt,...)
{
    va_list args;
    char modname[200];

    char temp[5000];
    HANDLE hFile;

    GetModuleFileName(NULL, modname, sizeof(modname));
    if((hFile =CreateFile("c:\\test.log", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) <0)
    {
        return;
    }
   
    _llseek((HFILE)hFile, 0, SEEK_END);

    wsprintf(temp, "modname:%s:", modname);
    DWORD dw;
    WriteFile(hFile, temp, strlen(temp), &dw, NULL);
   
    va_start(args,fmt);
    vsprintf(temp, fmt, args);
    va_end(args);

    WriteFile(hFile, temp, strlen(temp), &dw, NULL);

    wsprintf(temp, "\r\n");
    WriteFile(hFile, temp, strlen(temp), &dw, NULL);

    _lclose((HFILE)hFile);
}


WriteLog proc  C p:VARARG
LOCAL modname[200]:byte
LOCAL temp[5000]:byte
LOCAL hFile:HANDLE
LOCAL lpNumberOfBytesRead:DWORD
LOCAL len:dword

    invoke GetModuleFileName,0,addr modname, sizeof modname
    invoke CreateFileA,CTEXT("c:\\test.log"),GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,NULL, OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        .if eax != INVALID_HANDLE_VALUE
            mov hFile,eax
        .endif
    invoke _llseek,hFile,0,SEEK_END
    invoke wsprintf,temp,CTEXT("modname:%s:"),modname
    invoke lstrlen,addr temp
    mov len,eax
    invoke WriteFile,hFile,addr temp,len,addr lpNumberOfBytesRead,NULL

   ;vsprintf(temp, fmt, args);       ???

    invoke WriteFile,hFile,addr temp,len,addr lpNumberOfBytesRead,NULL
    invoke wsprintf,addr temp,CTEXT("\r\n")
    invoke WriteFile,hFile,addr temp,len,addr lpNumberOfBytesRead,NULL
    invoke WriteFile,hFile,addr temp,len,addr lpNumberOfBytesRead,NULL
WriteLog endp


laomms

I have solution it, but it's only one arg, how to use p:VARARG

WriteLog proc pText:DWORD
LOCAL hLogFile:dword
LOCAL DidWriteLog:dword
LOCAL len:dword
    invoke CreateFileA,CTEXT("c:\test.log"),GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,NULL, OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        .if eax != INVALID_HANDLE_VALUE
            mov hLogFile,eax
        .endif
            invoke lstrlen,pText
            mov len,eax
@@:    push eax
            invoke WriteFile,hLogFile,pText,len, addr DidWriteLog,0
            pop eax
            sub eax,DidWriteLog
            .if eax>0
                  jmp @B
            .endif
            ret
WriteLog endp

BogdanOntanu

Try something like:

WriteLog PROC C format_ptr:dword  var_arg_ptr:DWORD:?
...

Please notice the "C" calling convention and the "?" at the end of the last parameter.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

laomms

.data
szStr        db "test",0

.code
WriteLog proc C args:VARARG
LOCAL hLogFile:dword
LOCAL pText:dword
LOCAL DidWriteLog:dword
LOCAL len:dword
         mov eax,[esp+4]   
         mov edx,args[eax]
         mov   pText,edx       
         invoke CreateFileA,CTEXT("c:\test.log"),GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,NULL, OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
         .if eax != INVALID_HANDLE_VALUE
                  mov hLogFile,eax
         .endif
         invoke lstrlen,pText
         mov len,eax
@@:      push eax
         invoke WriteFile,hLogFile,pText,len, addr DidWriteLog,0
         pop eax
         sub eax,DidWriteLog
         .if eax>0
                  jmp @B
         .endif
         ret
WriteLog endp

Start:

    invoke    WriteLog,addr szStr,addr szStr,addr szStr          ;??????
    invoke    ExitProcess,0
End    Start

I modified it ,but only one "test"  in test.log

drizz

WriteLog proc c fmt:PTR SBYTE, args:VARARG

    local hFile:HANDLE,nbr:DWORD,modname[200]:byte,temp[5000]:byte

    invoke GetModuleFileName,NULL,addr modname,sizeof modname
    invoke CreateFile,T("c:\test.log"),GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
    mov hFile,eax
    .if sdword ptr eax <0
        ret
    .endif
    invoke SetFilePointer,hFile,0,0,FILE_END;
    invoke wsprintf,addr temp,T("modname:%s:"),addr modname
    invoke lstrlen,addr temp
    mov edx,eax
    invoke WriteFile,hFile,addr temp,edx,addr nbr,NULL
    invoke wvsprintf,addr temp,fmt,addr args
    invoke lstrlen,addr temp
    mov edx,eax
    invoke WriteFile,hFile,addr temp, edx, addr nbr, NULL
    invoke lstrcpy,addr temp,T(13,10)
    invoke lstrlen,addr temp
    mov edx,eax
    invoke WriteFile,hFile,addr temp, edx, addr nbr, NULL
    invoke CloseHandle,hFile;
    ret
   
WriteLog endp

start:
invoke WriteLog,T('[[ %s %s %s ]]'),T('bla1'),T('bla2'),T('bla3')
if anything is unclear, just ask
The truth cannot be learned ... it can only be recognized.