Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

com - Can someone help me with events in vb6 from c# dll

I have found nice application for NFC card reading which in winforms works very nice.

Code found here:

NfcReader: A very simple NFC library for C# that supports insert and discard events

Git: https://github.com/h4kbas/NfcReader

But i have a problem. I need now to make this work in com for vb6.

I must "replicate" event hooking, like in the following code.

I exposed methods and events to vb6 successfully.

NFC = new NFCReader();

NFC.CardInserted += new NFCReader.CardEventHandlerDelgate(Card_Inserted);
NFC.CardEjected += new NFCReader.CardEventHandlerDelgate(Card_Ejected);
NFC.DeviceDisconnected += new NFCReader.CardEventHandlerDelgate(Device_disconected);
NFC.StartCardMonitoring();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With the information @kunif provided, you first have to make the .NET library COM Visible in order to use the NFC Reader in VB6. If you have the source code, you can do this fairly easily. Otherwise, if you just have a DLL, you can write a wrapper DLL and make that COM Visible. Sounds like you might've done this already when you say "i exposed events and methods to vb6 successfully".

The next step is to add a reference to the library in VB6: Project > References...

Then, you can create an instance of the NFCReader:

Public WithEvents objNFC As NFCReader

Private Sub Form_Load()

    ' Create NFCReader object
    Set objNFC = New NFCReader

    objNFC.StartCardMonitoring

End Sub

' Card Inserted event handler
Private Sub objNFC_CardInserted()
    ' Handle Card Inserted event
End Sub

' Card Ejected event handler
Private Sub objNFC_CardEjected()
    ' Handle Card Ejected event
End Sub

' Device Disconnected event handler
Private Sub objNFC_DeviceDisconnected()
    ' Handle Device Disconnected event
End Sub

Once you've added a reference to the DLL, you should see the events and their parameters show up in VB6. Make sure you declare the object as WithEvents and the object will appear in the left dropdown in the code window in Visual Studio. The right dropdown will display the available events.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...