简体   繁体   中英

Declare OpenEventLog() in Lib advapi32.dll as Integer or Long?

Only Long works on both W2K8R2 and W2K12 for my application.

I have an existing application that saves the NT Event Logs by using the three calls:

Declare
FunctionOpenEventLog Lib"advapi32.dll"Alias"OpenEventLogA"(ByValServerName AsString, ByValSourceName AsString) AsInteger

Declare
FunctionBackupEventLog Lib"advapi32.dll"Alias"BackupEventLogA"(ByValEventLogHandle AsInteger, ByValBackupFileName AsString) AsBoolean

Declare
FunctionCloseEventLog Lib"advapi32.dll"(ByValEventLogHandle AsInteger) AsBoolean

After building the application in VS 2012, the code runs fine on Windows Server 2008R2 and on Windows Server 2012 when it is in a small test application.

I have inherited a large application that also had the above declarations. There many be any litany of settings applied in the large application - I'm just coming up to speed on VB. The application runs fine on Windows Server 2008 R2, but on Windows Server 2012 the handle returned by OpenEventLog() was bad - resulting in a bad handle error when it was passed to BackupEventLog(). The handle was a large negative number, so I tried declaring the Handle parameters and return values in the above declarations "As Long" instead of "As Integer". The large VB application now works on both W2K8 and W2K12.

This is a bit concerning as it is not expected behavior (from my perspective) and implies we need to fully test all features on all supported versions of Windows.

Which declaration is correct - "As Integer" or As Long"?

Is there a better way to code this?

Is there a "As Handle" way to code this?

Since the small stand alone test application uses "As Integer" and it working on W2k8R2 and W2K12, could there be a project setting that is causing the problem?

The value in question is a HANDLE . As documented in MSDN , this is a pointer, so it is 32 bits long (Integer) in an x86 application and 64 bits long (Long) in an x64 application.

By default, .NET applications run as x64 on 64-bit operating systems and as x86 on 32-bit operating systems. So the size of the handle depends on what operating system you're running on.

Instead of using Integer or Long, neither of which will be correct in all situations, use IntPtr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM