I am converting this C code to asm.

I will need some help starting with void wmain(void).

I made a required DLL as well as a required register subkey.

// Example shows how to use the ReportEvent function to write the events defined
// in the above message text file.

#define UNICODE
#include <windows.h>
#include <stdio.h>
#include "MyEventProvider.h"

#pragma comment(lib, "advapi32.lib")

;#define PROVIDER_NAME L"MyEventProvider"
Provider_Name  dw  "MyEventProvider",0
// Hardcoded insert string for the event messages.

CONST LPWSTR pBadCommand = L"The command that was not valid";
pBadCommand WORD "T","h","e"," ","c","o","m","m","a","n","d"," ","t","h","a","t"," ","w","a","s"," ","n","o","t"," ","v","a","l","i","d",".",0

CONST LPWSTR pFilename = L"c:\\masm32\source\\Events_Log.txt";
pFilename WORD "c",":","\","\","m","a","s","m","3","2","\","s","o","u","r","c","e","\","\","E","v","e","n","t","s","_","L","o","g",".","t","x","t",0

CONST LPWSTR pNumberOfRetries = L"3";


CONST LPWSTR pSuccessfulRetries = L"0";


CONST LPWSTR pQuarts = L"8";


CONST LPWSTR pGallons = L"2";

void wmain(void)
{
    HANDLE hEventLog = NULL;
    LPWSTR pInsertStrings[2] = {NULL, NULL};
    DWORD dwEventDataSize = 0;

    // The source name (provider) must exist as a subkey of Application.
    hEventLog = RegisterEventSource(NULL, PROVIDER_NAME);
    if (NULL == hEventLog)
    {
        wprintf(L"RegisterEventSource failed with 0x%x.\n", GetLastError());
        goto cleanup;
    }

    // This event includes user-defined data as part of the event. The event message
    // does not use insert strings.
    dwEventDataSize = (wcslen(pBadCommand) + 1) * sizeof(WCHAR);
    if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, UI_CATEGORY, MSG_INVALID_COMMAND, NULL, 0, dwEventDataSize, NULL, pBadCommand))
    {
        wprintf(L"ReportEvent failed with 0x%x for event 0x%x.\n", GetLastError(), MSG_INVALID_COMMAND);
        goto cleanup;
    }

    // This event uses insert strings.
    pInsertStrings[0] = pFilename;
    if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, DATABASE_CATEGORY, MSG_BAD_FILE_CONTENTS, NULL, 1, 0, (LPCWSTR*)pInsertStrings, NULL))
    {
        wprintf(L"ReportEvent failed with 0x%x for event 0x%x.\n", GetLastError(), MSG_BAD_FILE_CONTENTS);
        goto cleanup;
    }

    // This event uses insert strings.
    pInsertStrings[0] = pNumberOfRetries;
    pInsertStrings[1] = pSuccessfulRetries;
    if (!ReportEvent(hEventLog, EVENTLOG_WARNING_TYPE, NETWORK_CATEGORY, MSG_RETRIES, NULL, 2, 0, (LPCWSTR*)pInsertStrings, NULL))
    {
        wprintf(L"ReportEvent failed with 0x%x for event 0x%x.\n", GetLastError(), MSG_RETRIES);
        goto cleanup;
    }

    // This event uses insert strings.
    pInsertStrings[0] = pQuarts;
    pInsertStrings[1] = pGallons;
    if (!ReportEvent(hEventLog, EVENTLOG_INFORMATION_TYPE, UI_CATEGORY, MSG_COMPUTE_CONVERSION, NULL, 2, 0, (LPCWSTR*)pInsertStrings, NULL))
    {
        wprintf(L"ReportEvent failed with 0x%x for event 0x%x.\n", GetLastError(), MSG_COMPUTE_CONVERSION);
        goto cleanup;
    }

    wprintf(L"All events successfully reported.\n");

cleanup:

    if (hEventLog)
        DeregisterEventSource(hEventLog);
Posted on 2011-02-01 10:06:44 by skywalker
Provider_Name  dw  "MyEventProvider",0

doesnt look right...

also tried formatting the code.. it messed up, but is readable.. so bleh... i aint doing everything ;p

and wstr usage might be wrong too.. anyway, its mostly there, so small fixes to do...you can do that i'm sure



.data

; use the WSTR macro from the masm32\include\ucmacros.asm to do unicode stuff easily

Provider_Name WSTR "MyEventProvider" ; #define PROVIDER_NAME L"MyEventProvider"
pBadCommand WSTR "The command that was not valid" ; CONST LPWSTR pBadCommand = L"The command that was not valid";
pFilename WSTR "C:\masm32\source\Events_Log.txt" ; pFilename = L"c:\\masm32\source\\Events_Log.txt"; ; << check your typing \\ is an escape code, its only one \ when compiled in c...you've mixed them
pNumberOfRetries WSTR "3" ; CONST LPWSTR pNumberOfRetries = L"3";
pSuccessfulRetries WSTR "0" ; CONST LPWSTR pSuccessfulRetries = L"0";
pQuarts WSTR "8" ; CONST LPWSTR pQuarts = L"8";
pGallons WSTR "2" ; CONST LPWSTR pGallons = L"2";


.code

wmainproc proc uses ebx ecx edx esi edi

    LOCAL hEventLog:HANDLE
    LOCAL dwEventDataSize:DWORD
    LOCAL pInsertStrings[2]:LPVOID

    mov , NULL ; HANDLE hEventLog = NULL;
    mov , NULL ; DWORD dwEventDataSize = 0;
    mov ], NULL ; first part of LPWSTR pInsertStrings[2] = {NULL, NULL};
    mov ], NULL ; second part of LPWSTR pInsertStrings[2] = {NULL, NULL};

    push offset Provider_name
    push NULL
    Call RegisterEventSource ; hEventLog = RegisterEventSource(NULL, PROVIDER_NAME);

    mov , eax

    cmp , NULL
    jne outofthisroutine

    ; register event failed.. getlasterror, do some logging

    jmp outofthisroutine

gotahandle:

    ; push    offset pBadCommand
    ; call    wcslen ; dwEventDataSize = (wcslen(pBadCommand) + 1) * sizeof(WCHAR);

    ; lea    eax,

    ; mov    , eax

    ; actually, that code isnt needed, we can get the compiler to do the work for us.. whee i optimised it ;p

    mov , (sizeof pBadCommand) ; or maybe lengthof.. can't remember.. experiment ;p

    push offset pBadCommand
    push NULL
    push ; could be optimised to push (sizeof pBadCommand) ; thus not needing the dwEventDataSize local at all..
    push NULL
    push MSG_INVALID_COMMAND ; will be in some header somewhere..
    push UI_CATEGORY
    push EVENTLOG_ERROR_TYPE
    push
    call ReportEvent

    cmp         eax, FALSE
    jne         reporteventworked

    ; reportevent failed.. do some logging

closehandleandgetout:

    push
    call DeregisterEventSource ; DeregisterEventSource(hEventLog);

    ; close the handle too (probably needed.. you can test this)

    push
    call CloseHandle

    mov , NULL

    jmp outofthisroutine

reporteventworked:

    mov ], offset pFileName ; pInsertStrings[0] = pFilename;

    lea eax, pInsertStrings[0]

    push NULL
    push eax
    push NULL
    push 1
    push NULL
    push MSG_BAD_FILE_CONTENTS
    push DATABASE_CATEGORY
    push EVENTLOG_ERROR_TYPE
    push
    call ReportEvent

    cmp eax, FALSE
    jne reporteventworked2

    ; reportevent failed.. do some logging

    jmp closehandleandgetout

reporteventworked2:

    mov ], offset pNumberOfRetries ;    pInsertStrings[0] = pNumberOfRetries;
    mov ], offset pSuccessfulRetries ;    pInsertStrings[1] = pSuccessfulRetries;

    lea eax, pInsertStrings[0]

    push NULL
    push eax
    push NULL
    push 2
    push NULL
    push MSG_RETRIES
    push NETWORK_CATEGORY
    push EVENTLOG_WARNING_TYPE
    push
    call ReportEvent

    cmp eax, FALSE
    jne reporteventworked3

    ; reportevent failed.. do some logging

    jmp closehandleandgetout

reporteventworked3:

    mov ], offset pQuarts ;    pInsertStrings[0] = pQuarts;
    mov ], offset pGallons         ;    pInsertStrings[1] = pGallons;

    lea eax, pInsertStrings[0]

    push NULL
    push eax
    push NULL
    push 2
    push MSG_COMPUTE_CONVERSION
    push UI_CATEGORY
    push EVENTLOG_INFORMATION_TYPE
    push
    call ReportEvent

    cmp eax, FALSE
    je wearealldoneithink

    ; reportevent failed.. do some logging

    jmp closehandleandgetout

wearealldoneithink:

    ; more?

    jmp closehandleandgetout

outofthisroutine:

    ; DONE...

    ret

wmainproc endp



hopefully you'll learn a bit from this and be able to do the next bit yourself...

i left some of the c code still in as comments, so you can perhaps understand them a bit better...

dont know if the code compiles, but im sure you can fix any of the asm errors that might crop up...
Posted on 2011-02-02 07:58:01 by evlncrn8
I need help converting this header file entry to
something that would work in an include file.

#define MSG_INVALID_COMMAND              ((DWORD)0xC0020100L)

This didn't work.

MSG_INVALID_COMMAND equ ((DWORD)0xC0020100L)
Posted on 2011-02-06 17:19:52 by skywalker
MSG_INVALID_COMMAND equ 0C0020100H
?
Posted on 2011-02-06 17:34:45 by Gunner
Got this with the code below it.

I don't know what's up with the symbol redefinition errors, I see only one instance of  FACILITY_IO_ERROR_CODE.

\masm32\source\provider.inc(39) : error A2005: symbol redefinition : FACILITY_IO_ERROR_CODE
\masm32\source\provider.inc(45) : error A2005: symbol redefinition : STATUS_SEVERITY_WARNING
\masm32\source\provider.inc(46) : error A2005: symbol redefinition : STATUS_SEVERITY_SUCCESS
\masm32\source\provider.inc(47) : error A2005: symbol redefinition : STATUS_SEVERITY_INFORMATIONAL
\masm32\source\provider.inc(48) : error A2005: symbol redefinition : STATUS_SEVERITY_ERROR
C:\masm32\SOURCE\WSTR.asm(111) : error A2206: missing operator in expression
C:\masm32\SOURCE\WSTR.asm(137) : error A2006: undefined symbol : pFileName
C:\masm32\SOURCE\WSTR.asm(146) : error A2206: missing operator in expression
C:\masm32\SOURCE\WSTR.asm(147) : error A2206: missing operator in expression
C:\masm32\SOURCE\WSTR.asm(161) : error A2006: undefined symbol : pNumberOfRetries
C:\masm32\SOURCE\WSTR.asm(162) : error A2006: undefined symbol : pSuccessfulRetries
C:\masm32\SOURCE\WSTR.asm(171) : error A2206: missing operator in expression
C:\masm32\SOURCE\WSTR.asm(172) : error A2206: missing operator in expression
C:\masm32\SOURCE\WSTR.asm(186) : error A2006: undefined symbol : pQuarts
C:\masm32\SOURCE\WSTR.asm(187) : error A2006: undefined symbol : pGallons
C:\masm32\SOURCE\WSTR.asm(195) : error A2206: missing operator in expression
C:\masm32\SOURCE\WSTR.asm(196) : error A2206: missing operator in expression


  • ; WSTR.asm Contributors: evlncrn8,R. Russel,
    ;
    .686                                     
    .model flat, stdcall                     
    option casemap :none                     

    include \masm32\include\windows.inc   
    include \masm32\include\masm32.inc   

    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\Comctl32.inc
    include \masm32\include\comdlg32.inc
    include \masm32\include\shell32.inc
    include \masm32\include\oleaut32.inc
    include \masm32\include\ole32.inc
    include \masm32\include\msvcrt.inc
    include \masm32\include\advapi32.inc

    ;include \masm32\source\MyEventProvider.h
    include \masm32\source\provider.inc

    include \masm32\include\dialogs.inc   
    include \masm32\macros\macros.asm     

    includelib \masm32\lib\masm32.lib     

    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\Comctl32.lib
    includelib \masm32\lib\comdlg32.lib
    includelib \masm32\lib\shell32.lib
    includelib \masm32\lib\oleaut32.lib
    includelib \masm32\lib\ole32.lib
    includelib \masm32\lib\msvcrt.lib
    includelib \masm32\lib\advapi32.lib

    .data

    RESOURCE_DLL  db  "c:\masm32\source\MyEventProvider.dll",0
    Provider_Name  db  "MyEventProvider",0
    pBadCommand    WORD "T","h","e"," ","c","o","m","m","a","n","d"," ","t","h","a","t"," ","w","a","s"," ","n","o","t"," ","v","a","l","i","d",".",0

    ; use the WSTR macro from the masm32\include\ucmacros.asm to do unicode stuff easily

    ;Provider_Name WSTR "MyEventProvider"
    ;pBadCommand WSTR "The command that was not valid"
    ; pFilename WSTR "C:\masm32\source\Events_Log.txt"
      ;pNumberOfRetries WSTR "3"
      ;pSuccessfulRetries WSTR "0"
      ;pQuarts WSTR "8"
      ;pGallons WSTR "2"

    .code

    start:

    wmainproc proc uses ebx ecx edx esi edi

        LOCAL hEventLog:HANDLE
        LOCAL dwEventDataSize:DWORD
        LOCAL pInsertStrings[2]:LPVOID

        mov , NULL ; HANDLE hEventLog = NULL;
        mov , NULL ; DWORD dwEventDataSize = 0;
        mov ], NULL ; first part of LPWSTR pInsertStrings[2] = {NULL, NULL};
        mov ], NULL ; second part of LPWSTR pInsertStrings[2] = {NULL, NULL};

        push offset Provider_Name
        push NULL
        Call RegisterEventSource ; hEventLog = RegisterEventSource(NULL, PROVIDER_NAME);

        mov , eax

        cmp , NULL
        jne outofthisroutine

        ; register event failed.. getlasterror, do some logging

    outofthisroutine:

        ; DONE...

      invoke ExitProcess,0

    ;end start

    ;jmp outofthisroutine

    gotahandle:

        ; push    offset pBadCommand
        ; call    wcslen ; dwEventDataSize = (wcslen(pBadCommand) + 1) * sizeof(WCHAR);

        ; lea    eax,

        ; mov    , eax

        ; actually, that code isnt needed, we can get the compiler to do the work for us.. whee i optimised it ;p

        mov , (sizeof pBadCommand) ; or maybe lengthof.. can't remember.. experiment ;p

        push offset pBadCommand
        push NULL
        push ; could be optimised to push (sizeof pBadCommand) ; thus not needing the dwEventDataSize local at all..
        push NULL
        push MSG_INVALID_COMMAND ; will be in some header somewhere..
        push UI_CATEGORY
        push EVENTLOG_ERROR_TYPE
        push
        call ReportEvent

        cmp        eax, FALSE
        jne        reporteventworked

        ; reportevent failed.. do some logging

    closehandleandgetout:

        push
        call DeregisterEventSource ; DeregisterEventSource(hEventLog);

        ; close the handle too (probably needed.. you can test this)

        push
        call CloseHandle

        mov , NULL

        jmp outofthisroutine

    reporteventworked:

        mov ], offset pFileName ; pInsertStrings[0] = pFilename;

        lea eax, pInsertStrings[0]

        push NULL
        push eax
        push NULL
        push 1
        push NULL
        push MSG_BAD_FILE_CONTENTS
        push DATABASE_CATEGORY
        push EVENTLOG_ERROR_TYPE
        push
        call ReportEvent

        cmp eax, FALSE
        jne reporteventworked2

        ; reportevent failed.. do some logging

        jmp closehandleandgetout

    reporteventworked2:

        mov ], offset pNumberOfRetries ;    pInsertStrings[0] = pNumberOfRetries;
        mov ], offset pSuccessfulRetries ;    pInsertStrings[1] = pSuccessfulRetries;

        lea eax, pInsertStrings[0]

        push NULL
        push eax
        push NULL
        push 2
        push NULL
        push MSG_RETRIES
        push NETWORK_CATEGORY
        push EVENTLOG_WARNING_TYPE
        push
        call ReportEvent

        cmp eax, FALSE
        jne reporteventworked3

        ; reportevent failed.. do some logging

        jmp closehandleandgetout

    reporteventworked3:

        mov ], offset pQuarts ;    pInsertStrings[0] = pQuarts;
        mov ], offset pGallons        ;    pInsertStrings[1] = pGallons;

        lea eax, pInsertStrings[0]

        push NULL
        push eax
        push NULL
        push 2
        push MSG_COMPUTE_CONVERSION
        push UI_CATEGORY
        push EVENTLOG_INFORMATION_TYPE
        push
        call ReportEvent

        cmp eax, FALSE
        je wearealldoneithink

        ; reportevent failed.. do some logging

        jmp closehandleandgetout

    wearealldoneithink:

        ; more?

        jmp closehandleandgetout
         
    wmainproc endp

    end start


Posted on 2011-02-06 18:02:58 by skywalker