VB6 OCX In C# Windows App

P

PvdG42

Trying to use a MagTek mag card swipe reader in a C# Win app. The only API
MagTek offers for Windows is via VB6 OCX (COM DLL). I can use the DL and its
methods in a VB .NET app with no problems, both in VS 2008 and in direct
execution of the VB app in Windows outside the IDE.

I developed a simple C# app (identical to an early VB test app), to use the
MagTek DLL, but the C# version fails to initialize the swipe reader with the
failing code found as generated in <appname>.designer.cs.

The failing statement in C#:

((System.ComponentModel.ISupportInitialize)(this.axUSBHID1)).EndInit();

The successful VB equivalent statement:

CType(Me.USBHID1, System.ComponentModel.ISupportInitialize).EndInit()

You may well ask, why not just proceed in VB? I have an associate who is
quite experienced in C#, but not in VB, and thus prefers to use C#.

I realize that I may well have failed to supply all needed info, and will on
request.

Any advice/suggestions?

TIA.
 
P

Peter Duniho

PvdG42 said:
Trying to use a MagTek mag card swipe reader in a C# Win app. The only
API MagTek offers for Windows is via VB6 OCX (COM DLL). I can use the DL
and its methods in a VB .NET app with no problems, both in VS 2008 and
in direct execution of the VB app in Windows outside the IDE.

I developed a simple C# app (identical to an early VB test app), to use
the MagTek DLL, but the C# version fails to initialize the swipe reader
with the failing code found as generated in <appname>.designer.cs.

The failing statement in C#:

((System.ComponentModel.ISupportInitialize)(this.axUSBHID1)).EndInit();

The successful VB equivalent statement:

CType(Me.USBHID1, System.ComponentModel.ISupportInitialize).EndInit()

You may well ask, why not just proceed in VB? I have an associate who is
quite experienced in C#, but not in VB, and thus prefers to use C#.

I realize that I may well have failed to supply all needed info, and
will on request.

Any advice/suggestions?

Without a specific description of the failure, never mind a
concise-but-complete code example, it would be practically impossible to
know for sure what the issue is. However, as a shot in the dark, have
you tried making sure that you access the COM object in an STA thread?
Have you taken steps to make sure you only access the COM object in the
STA thread in which it was created?

You may (or may not) find a recent discussion on the question of COM
interop in C# that we had here:
http://groups.google.com/group/micr...read/thread/29149effba98d9d5/d596d851ee927ad1

You might also consider using Reflector to convert the VB.NET code that
is known to work to C# code. You may find that VB.NET is doing things
on your behalf that C# requires you do to explicitly.

Underneath, it's all IL code so if it works in VB.NET, you should be
able to get it to work in C# too. :) (Actually, that's not quite
true…VB.NET has some features that are simply not supported in C#; but I
think that COM interop wouldn't need any of those features).

Pete
 
P

PvdG42

PvdG42 said:
Trying to use a MagTek mag card swipe reader in a C# Win app. The only API
MagTek offers for Windows is via VB6 OCX (COM DLL). I can use the DL and
its methods in a VB .NET app with no problems, both in VS 2008 and in
direct execution of the VB app in Windows outside the IDE.

I developed a simple C# app (identical to an early VB test app), to use
the MagTek DLL, but the C# version fails to initialize the swipe reader
with the failing code found as generated in <appname>.designer.cs.

The failing statement in C#:

((System.ComponentModel.ISupportInitialize)(this.axUSBHID1)).EndInit();

The successful VB equivalent statement:

CType(Me.USBHID1, System.ComponentModel.ISupportInitialize).EndInit()

You may well ask, why not just proceed in VB? I have an associate who is
quite experienced in C#, but not in VB, and thus prefers to use C#.

I realize that I may well have failed to supply all needed info, and will
on request.

Any advice/suggestions?

TIA.

This is to myself as well as to Peter, who was kind enough to respond.

I should have included the exception and error message raised by the failing
C# statement:

"Class not registered (Exception from HRESULT: 0x80040154
(REGDB_E_CLASSNOTREG))"

The example that exhibits this behavior is simple and (relatively) concise
example, but whomever wanted to investigate would have to install the Magtek
OCX as well as having the example app. I'll be happy to zip the two up and
upload/send the archive to anyone who wants to see it.

Meanwhile, I'll take Peter's suggestion to use Reflector to translate the VB
code and see what I may have missed when I did the manual conversion.
 
P

Peter Duniho

PvdG42 said:
I should have included the exception and error message raised by the
failing C# statement:

"Class not registered (Exception from HRESULT: 0x80040154
(REGDB_E_CLASSNOTREG))"

Based on that error, I think there's almost no chance my previous guess
will be helpful.

The error sounds like there's some problem mapping to a COM classbeing
used. But you still could provide more detail than you have, even
without a full example with the DLL. For example, you're calling an
EndInit() method; was there a prior call to BeginInit()? Are you using
the same object reference for both? What other initialization may or
may not have occurred? Showing at least both the VB.NET and C# code
involved in initializing this COM object would be useful.

If you're successfully calling BeginInit() on the same object, that
suggests that the COM object you're using directly is fine, and that it
itself is having some problem not finding a required COM class.

Also, the ISupportInitialize interface _should_ be an optimization,
rather than required. So another test might be to simply remove the
calls to BeginInit() and EndInit() and see if that works.

Of course, if there's not actually a call to BeginInit() before the call
to EndInit(), that might be part of the problem. :)

Perhaps looking at the VB.NET-compiled code will lead to a solution. If
not, maybe the above provides other things to think about.

Pete
 
P

PvdG42

Peter Duniho said:
Based on that error, I think there's almost no chance my previous guess
will be helpful.

The error sounds like there's some problem mapping to a COM classbeing
used. But you still could provide more detail than you have, even without
a full example with the DLL. For example, you're calling an EndInit()
method; was there a prior call to BeginInit()? Are you using the same
object reference for both? What other initialization may or may not have
occurred? Showing at least both the VB.NET and C# code involved in
initializing this COM object would be useful.

If you're successfully calling BeginInit() on the same object, that
suggests that the COM object you're using directly is fine, and that it
itself is having some problem not finding a required COM class.

Also, the ISupportInitialize interface _should_ be an optimization, rather
than required. So another test might be to simply remove the calls to
BeginInit() and EndInit() and see if that works.

Of course, if there's not actually a call to BeginInit() before the call
to EndInit(), that might be part of the problem. :)

Perhaps looking at the VB.NET-compiled code will lead to a solution. If
not, maybe the above provides other things to think about.

Pete

Actually, (and, boy is my face red!!) I stumbled on the cure early this
morning while comparing project properties with the VB version. Turns out I
had failed to specify an x86 target platform in the C# project. The vb OCX
requires this to work. I'm developing on a x64 Win 7 box, where the target
defaults to "any...".

Like the scarecrow in Alice, I need a brain. Sorry to take up your time, and
thanks!
 
P

Peter Duniho

PvdG42 said:
[...] I had failed to specify an x86 target platform in the C# project.
The vb OCX requires this to work. I'm developing on a x64 Win 7 box,
where the target defaults to "any...".

Yup. That would definitely result in "some problem mapping to the COM
class being used", there not being a registered COM DLL for the given
class and CPU architecture. :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top