I've had good results with other P/Invoke'd entry points and the
StringBuilder class.

For example, with a slight modification to ReadEventLog's lpBuffer
parameter, the following is an alternative (warning uncompiled code follows):

// create reference to ReadEventLog in advapi32.dll
[DllImport("advapi32.dll", EntryPoint="ReadEventLog")]
public static extern bool ReadEventLog(
   int iBackupLogRef,
   int iReadFlags,
   int iRecordOffset,
   [Out] StringBuilder lpBuffer,
   int iNumberOfBytesToRead,
   out int pnBytesRead,
   out int pnMinNumberOfBytesNeeded);

// ...
StringBuilder BufferString = new StringBuilder(1024);
String ReadString = "";

// ...
// call ReadEventLog in advapi32.dll
bool success = ReadEventLog(
   iBackupLogRetValue,
   EVENTLOG_SEQUENTIAL_READ,
   iRecordOffset,
   BufferString,
   BufferString.Capacity,
   out iBytesRead,
   out iMinNumberOfBytesNeeded);

// ...
ReadString = BufferString.ToString();
// ...

-- Peter
http://www.peterRitchie.com/

On Tue, 26 Apr 2005 05:05:16 -0400, Pete Lutwyche
<[EMAIL PROTECTED]> wrote:

>Thanks Peter,
>
>I successfully used [DLLImport] with entry points of OpenBackupEventLog,
>GetNumberOfEventLogRecords and CloseEventLog. When it comes to
>ReadEventLog, my (failing) code is as follows:
>
>// create reference to ReadEventLog in advapi32.dll
>[DllImport("advapi32.dll", EntryPoint="ReadEventLog")]
>public static extern bool ReadEventLog(
>   int iBackupLogRef,
>   int iReadFlags,
>   int iRecordOffset,
>   [MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
>   int iNumberOfBytesToRead,
>   out int pnBytesRead,
>   out int pnMinNumberOfBytesNeeded);
>
>// call ReadEventLog in advapi32.dll
>bool success = ReadEventLog(
>   iBackupLogRetValue,
>   EVENTLOG_SEQUENTIAL_READ,
>   iRecordOffset,
>   myByteArray,
>   myByteArray.Length,
>   out iBytesRead,
>   out iMinNumberOfBytesNeeded);
>
>// output some data
>string strByteArrayAsString = System.Text.ASCIIEncoding.ASCII.GetString
>(myByteArray);
>System.Console.WriteLine("strByteArrayAsString = " + strByteArrayAsString);
>
>Unfortunately, the boolean "success" is always false and myByteArray is
>always empty. Can you see anything obviously wrong?
>

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to