[racket] OpenEventLog in ffi

From: heraklea at gmx.de (heraklea at gmx.de)
Date: Mon Nov 26 20:34:58 EST 2012

Hello,
it´s me again.

After your suggestions (shame on me) I try to go on and add these lines:
(define EVENTLOG_SEQUENTIAL_READ            #x0001)
(define EVENTLOG_SEEK_READ                  #x0002)
(define EVENTLOG_FORWARDS_READ              #x0004)
(define EVENTLOG_BACKWARDS_READ             #x0008)
(define-cstruct _EVENTLOGRECORD ([Length 		_DWORD]
                                 [Reserved 		_DWORD]
                                 [RecordNumber 		_DWORD]
                                 [TimeGenerated 	_DWORD]
                                 [TimeWritten 		_DWORD]
                                 [EventID 		_DWORD]
                                 [EventType 		_WORD]
                                 [NumStrings 		_WORD]
                                 [EventCategory 	_WORD]
                                 [ReservedFlags 	_WORD]
                                 [ClosingRecordNumber 	_DWORD]
                                 [StringOffset 		_DWORD]
                                 [UserSidLength 	_DWORD]
                                 [UserSidOffset 	_DWORD]
                                 [DataLength 		_DWORD]
                                 [DataOffset 		_DWORD]))
(define m (make-EVENTLOGRECORD 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))

(define-advapi ReadEventLogW (_fun #:abi winapi
                                  _HANDLE
                                  _DWORD
                                  _DWORD
                                  (m : (_ptr o _EVENTLOGRECORD))
                                  _DWORD
                                  (type : (_ptr o _DWORD))
                                  (type : (_ptr o _DWORD))
                                  -> (r : _BOOL)))

(define dwBytesToRead MAX_RECORD_BUFFER_SIZE)
(define dwBytesRead 0)
(define pnMinNumberOfBytesNeeded 0)
(define hEventLog  (OpenEventLogW MACHINENAME 
                                  PROVIDER_NAME))
(define bReadEventlog (ReadEventLogW hEventLog 
                                    (bitwise-ior EVENTLOG_SEQUENTIAL_READ                        EVENTLOG_BACKWARDS_READ)
                                    0
                                    dwBytesToRead
                                    ))

I get this error in the last define:
ptr-ref: contract violation
  expected: cpointer?
  given: #???
  argument position: 1st
  other arguments...:
   #<ctype>

in ReadEventLogW.

I realy try to understand and read alot in the docu, but the output arguments confuses me a little bit. I have to declare those with o, but what is when it comes to call the function.

The C syntax of the functions is:
BOOL ReadEventLog(
  _In_   HANDLE hEventLog,
  _In_   DWORD dwReadFlags,
  _In_   DWORD dwRecordOffset,
  _Out_  LPVOID lpBuffer,
  _In_   DWORD nNumberOfBytesToRead,
  _Out_  DWORD *pnBytesRead,
  _Out_  DWORD *pnMinNumberOfBytesNeeded
);

Do I have to put the output variables in the functions call?

Thanks.
Yours,
-------- Original-Nachricht --------
> Datum: Sun, 25 Nov 2012 21:00:57 -0700
> Von: Matthew Flatt <mflatt at cs.utah.edu>
> An: heraklea at gmx.de
> CC: "racket " <users at racket-lang.org>
> Betreff: Re: [racket] OpenEventLog in ffi

> You want `OpenEventLogW'. (In C, `OpenEventLog' is a macro that expands
> to either `OpenEventLogW' or `OpenEventLogA'.)
> 
> At Mon, 26 Nov 2012 04:00:38 +0100, heraklea at gmx.de wrote:
> > Hello friends,
> > 
> > I try to write the OpenEventLog:
> > HANDLE OpenEventLog(
> >   _In_  LPCTSTR lpUNCServerName,
> >   _In_  LPCTSTR lpSourceName
> > );
> > 
> > in Racket ffi:
> > 
> > #lang racket
> > (require ffi/com
> >          ffi/unsafe
> >          ffi/unsafe/define
> >          ffi/winapi)
> > 
> > (define _HANDLE (_cpointer/null 'HANDLE))
> > (define advapi-dll (and (eq? (system-type) 'windows)
> >                         (ffi-lib "Advapi32.dll")))
> > 
> > (define-ffi-definer define-advapi advapi-dll
> >   #:default-make-fail make-not-available)
> > 
> > (define PROVIDER_NAME  "Application")
> > (define MACHINENAME "MyMachine")
> > (define-advapi OpenEventLog (_fun #:abi winapi
> >                                   _string/utf-16
> >                                   _string/utf-16
> >                                   -> (r : _HANDLE)))
> > 
> > After all, eval this: >(OpenEventLog MACHINENAME PROVIDER_NAME)
> > brings an error:
> > OpenEventLog: implementation not found; arguments: "MyMachine"
> "Application"
> > 
> > These steps in Racket which I defined above are programmed as I
> understand it.. 
> > 
> > So is there any misunderstanding in usind the ffi.
> > Or do I something wrong?
> > 
> > The type understanding is a little bit confusing.
> > 
> > 
> > The first questions that comes to my mind is:
> > 
> > What is a const char*, LPCTSTR, BSTR, or HANDLE etc
> > There are some instructions on creating these types ( as far as I can
> found it) 
> > but how to use it in Racket?
> > Ok back to my problem.
> > 
> > I would like to write the openeventlog, closeevntlog, readeventlog etc 
> > functions but I fail at openeventlog.
> > 
> > Could anybody hold my hands?;O)
> > 
> > Yours,
> > 
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users

Posted on the users mailing list.