简体   繁体   English

将Lib advapi32.dll中的OpenEventLog()声明为Integer还是Long?

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

Only Long works on both W2K8R2 and W2K12 for my application. 对于我的应用程序,只有Long可以同时在W2K8R2和W2K12上使用。

I have an existing application that saves the NT Event Logs by using the three calls: 我有一个使用三个调用保存NT事件日志的现有应用程序:

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. 在VS 2012中构建应用程序后,该代码在小型测试应用程序中时,可以在Windows Server 2008R2和Windows Server 2012上正常运行。

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. 在大型应用程序中可以应用许多设置-我只是想了解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(). 该应用程序在Windows Server 2008 R2上运行良好,但是在Windows Server 2012上,OpenEventLog()返回的句柄不正确-当传递给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". 该句柄是一个很大的负数,因此我尝试在上述声明中声明“ Handle”参数并返回值“ As Long”而不是“ As Integer”。 The large VB application now works on both W2K8 and W2K12. 大型VB应用程序现在可以在W2K8和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. 这有点令人担忧,因为这不是预期的行为(从我的角度来看),这意味着我们需要在所有受支持的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? 是否有“ As Handle”方式对此进行编码?

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? 由于小型独立测试应用程序使用“ As Integer”,并且可以在W2k8R2和W2K12上运行,因此是否存在引起问题的项目设置?

The value in question is a HANDLE . 有问题值是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. MSDN中所述 ,这是一个指针,因此在x86应用程序中为32位长(整数),在x64应用程序中为64位长(长)。

By default, .NET applications run as x64 on 64-bit operating systems and as x86 on 32-bit operating systems. 默认情况下,.NET应用程序在64位操作系统上以x64运行,在32位操作系统上以x86运行。 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 . 而不是使用Integer或Long(在所有情况下都不正确),请使用IntPtr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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