VB6 event passing bool argument that's always 'true' in C#

北城余情 提交于 2019-12-04 15:20:58

Sorry that the problem wasn't as simple as bit-wise Boolean stuff in VB6 world.

If the problem is marshalling, is it possible that the interop DLL that .NET makes is somehow out of sync with its respective VB6 DLL? (As an aside, are you familiar with the Binary Compatibility settings in VB6?)

For the giggles, I did the following - please let me know if this simple set of projects is a representative model for the coding or marshalling issue you are tracking. If this issue is marshalling, it might be helpful to work it through simplified versions of the c# and vb6 projects in tracking it down.

With VB6, I created a simple ActiveX DLL (project named LibBoolBuster), with a class called BoolBuster:

Public Event WriteComplete(ByVal aCommsOk As Boolean, ByVal aBadPIN As Boolean)

Public Sub DoIt(ByVal Mode As Integer)
    RaiseEvent WriteComplete(CBool(Mode And 1), CBool(Mode And 2))   
End Sub

With VS2003 (old .NET, but should still apply), I created a simple C# Windows app, having a form with four buttons, and making reference to the above VB6 ActiveX DLL. I made a member in the form that accesses the VB6 DLL:

public LibBoolBuster.BoolBuster vb6Obj = new LibBoolBuster.BoolBuster();

DLL via Interop is LibBoolBuster; class BoolBuster has event and DoIt member. In the form initialize, the event hookup is done like so:

vb6Obj.WriteComplete += new LibBoolBuster.__BoolBuster_WriteCompleteEventHandler(vb6Obj_WriteComplete);

The WriteComplete event handler is a member of the form:

private void vb6Obj_WriteComplete ( System.Boolean aCommsOk  , System.Boolean aBadPIN )
{
    MessageBox.Show(aCommsOk.ToString() + ", " + aBadPIN.ToString());           
}

Each button click event handler makes a simple call to DoIt:

vb6Obj.DoIt(0);

(and vb6Obj.DoIt(1), vb6Obj.DoIt(2), etc.)

Very simple project: Click a button, DoIt is called, event WriteComplete is fired. I gave it a try, and things worked as expected.


Take a close look on the VB6 side. You indicated the event is raised with:

RaiseEvent WriteComplete(aCommsReceived, Not (aIsAcknowledged))

Make sure that aIsAcknowledged is only either 0 or -1; otherwise you will not get the result you're expecting.

VB6 Boolean operators are all bitwise; i.e. (Not 1) results in -2 not zero.

Also, in VB6 any non-zero integer will be mapped to True when converting from Integer to Boolean - only zero becomes false.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!