Link to home
Create AccountLog in
Avatar of codefinger
codefingerFlag for United States of America

asked on

VB and VC# Express 2010 projects and references and platforms oh my!

In order to compete, my new VB.NET winforms software application has to include support
for the USBUIRT, an infrared transceiver that allows a PC to learn and transmit
codes for a remote control.   I am programming in the Windows XP environment.


One of his users provided the manufacturer (a term I use loosely, since this guy builds
the USBUIRT more as a hobby) with an inadequate .NET wrapper written in C#.  It works
with standard remotes like TV remotes, but for less common remotes that send their codes
in short bursts (like the remote control for a fan) it did not work.  After some
searching on his web site forum, I found that yet another user had sent him two new files
to replace his source code files in his .net library that would have corrected the
deficiency, had he been able to use them.  (I wish that user had sent him the revised
recompiled .net wrapper library instead, but no such luck)  The manufacturer sent me the
revised files.

He also supplied the source code to the managed wrapper. So I downloaded
MicroSoft Visual C# 2010 Express, loaded the wrapper project, replaced the two files with
the ones the user had supplied, and recompiled.  I got some warnings but when I changed
the warning level, they went away.

Feeling very proud of myself for my ingenuity and persistence, I added the new recompiled
DLL to the reference for my VB.NET project and sure enough, the new functions I needed to
handle short bursts were detected by the intellisense, which is pretty good for a guy who can only program in VB.NET.

BUT  --  when I tried to initialize the reference, I got the following error message:
---------------------------------------------------------
The type initializer for the USBUIRT threw an exception.
Unable to read USBUIRT driver version
---------------------------------------------------------

So its back to his forum, where I found this very useful information:
--------------------------------------------------------------------
The issue is with a 64-bit application trying to load a 32-bit DLL (uuirtdrv.dll)
To correct the problem, change each project in your solution from AnyCPU to x86. To do
this, go to Build --> Configuration Manager. In the platform column, choose (New). Choose
x86 from the New Platform drop down. Uncheck Create New Solution Platforms. Click OK.
Back on the configuration manager window, select the new x86 option for all of your
projects.
Note: You will probably need to load the managed wrapper source code as a project,
reference the project, and force it to compile as x86, instead of just referencing the
already-compiled UsbUirtManagedWrapper.DLL
--------------------------------------------------------------------------------


So I followed the instructions in Microsoft Visual C# 2010 express and recompiled. But
I can only take this so far.  I tried just referencing the recompiled wrapper dll, but the error remains.

I do not think MVB 2010 Express will let me add a project written with VC# 2010 Express.

Unless I am missing something?

So this is where I am stuck.  Neither the user who wrote the original wrapper or the one who sent the revised files are still available, and the manufacturer has been some help, but has not offered to provide an adequate .net wrapper I can actually use in my VB.NET project.

Thanks in advance to anyone willing to tackle this.... (This is one of those times I wish the board would let me award more than 500 points.)  
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Did you compile the Debug or Release version? And did you add the reference to the same version?

Also, you should be able to add a C# project to a Solution that contains a VB project (at least you can with the non-Express versions, so i would assume Express will too), so you could try that.
Hi!

Try this:
http://www.sharpdevelop.net/OpenSource/SD/Download/
You can convert the #C code to VB.NET and add it to your own project, the dubug posibilities are better

Regards!
Matti
>> the debug possibilites are better

I'd be very interested to know how, given that they both use the same debugger!
>> the debug possibilites are better
If it's in the same code not as compiled dll you can set breakpoints etc.
Of cause debug version will display the code line, do a debug compilation of this dll

Regards!
Matti
Yes, but there's no need to go through the hassle of converting to a different language to do that.
Avatar of codefinger

ASKER

Really, I do hope I can accomplish my goal without having to learn an entirely new language.  I have quite a bit of time and code invested in my VB.NET Winforms project.

Also, you should be able to add a C# project to a Solution that contains a VB project (at least you can with the non-Express versions, so i would assume Express will too), so you could try that.

But that is precisely where I am stuck.   MVB 2010 Express says it cannot add project files of type csproj. and suggests I use a different application.

Did you compile the Debug or Release version? And did you add the reference to the same version?

I appreciate that this is a little confusing, so let me try to be more clear.   The USBUIRTManagedWrapper is the C# project.   I add a reference to it from my VB.NET project.
But I did compile a debug wrapper version and that was the reference I added to my VB.NET project, which currently is also still a debug version.   Not sure if that is relevant.



Ok, this is weird.  I cannot seem to build a Release version with VC# 2010 express, only debug.
I changed the Configuration to Release, but the only files in the BIN directory are all under debug.

What is going on?

Never mind that last.  I was able to compile an x86 wrapper release version, but I still get the same error from the VB.NET project.
Hi codefinger

If I understand correctly, you modified the C# project to be x86.  Did you do the same in your VB project?
Hairbrush:

I did not.   Will doing so limit the platforms on which it can run, or the drivers it can use?
USBUIRT is not the only transceiver I want the application to support.

Changing the architecture to x86 will mean that if the application is installed on a 64-bit version of Windows, it will run under WOW64 (Windows on Windows) - see http://en.wikipedia.org/wiki/WOW64 for a description.  I am assuming your application doesn't need to take advantage of 64-bit addressing, in which case this will have no adverse affect.

As for driver support, that should only be an issue in the unlikely event that a transceiver you wanted to use only had 64-bit drivers.
Took a little work to locate the option in the VB Express version, but when I did I was disappointed to find its been set to build x86 all along.

Still stuck....



Can you open the C# project and use the "find" feature to find the piece of code that produces the error message "Unable to read USBUIRT driver version"?  Can you paste that code into EE so we can see what it is doing?
The wrapper project includes a test app which throws the same exception at the line
if (Controller.DriverVersion != 0x0100).   I cannot debug any deeper than that.

See attached...


private static void RunTestApp() 
		{
			Console.WriteLine("UUIRTDRV Example Program...");
			Console.WriteLine("===========================");

			if (Controller.DriverVersion != 0x0100)
			{
				Console.WriteLine("ERROR: Invalid uuirtdrv version!\n");
				return;
			}

			using (Controller mc = new Controller()) 
			{
				mc.Received += new UsbUirt.Controller.ReceivedEventHandler(mc_Received);
				Console.WriteLine("USB-UIRT Info: Protocol: Version={0} Firmware: Version={1} Date={2}",
					mc.ProtocolVersion, mc.FirmwareVersion, mc.FirmwareDate.ToShortDateString());

				while(DoLoop(mc) == true);
				mc.Received -= new UsbUirt.Controller.ReceivedEventHandler(mc_Received);
			}
		}

Open in new window

There was some more information written to the output console:

UUIRTDRV Example Program...
===========================
UUIRTDRV Example Program...
===========================
An exception was thrown: System.TypeInitializationException: The type initialize
r for 'UsbUirt.Controller' threw an exception. ---> System.ApplicationException:
 Unable to read UsbUirt driver version ---> System.MethodAccessException: Attemp
t by security transparent method 'UsbUirt.Controller..cctor()' to call native co
de through method 'UsbUirt.Controller.UUIRTGetDrvInfo(UInt32 ByRef)' failed.  Me
thods must be security critical or security safe-critical to call native code.
   at UsbUirt.Controller..cctor() in C:\Documents and Settings\Al Knowles\Deskto
p\VERA\usbuirt\api_example_code\UsbUirt_rev1\UsbUirt\UsbUirt Managed Wrapper\Con
troller.cs:line 47
   --- End of inner exception stack trace ---
   at UsbUirt.Controller..cctor() in C:\Documents and Settings\Al Knowles\Deskto
p\VERA\usbuirt\api_example_code\UsbUirt_rev1\UsbUirt\UsbUirt Managed Wrapper\Con
troller.cs:line 54
   --- End of inner exception stack trace ---
   at UsbUirt.Controller.get_DriverVersion()
   at TestApp.Class1.RunTestApp() in C:\Documents and Settings\Al Knowles\Deskto
p\VERA\usbuirt\api_example_code\UsbUirt_rev1\UsbUirt\TestApp\TestApp.cs:line 39
   at TestApp.Class1.Main(String[] args) in C:\Documents and Settings\Al Knowles
\Desktop\VERA\usbuirt\api_example_code\UsbUirt_rev1\UsbUirt\TestApp\TestApp.cs:l
ine 24
Press return to exit.
ASKER CERTIFIED SOLUTION
Avatar of Hairbrush
Hairbrush
Flag of Jersey image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Windows says it is unable to load the assembly:

C:\Documents and Settings\Al Knowles\Desktop\VERA\usbuirt\api_example_code\UsbUirt_rev1\UsbUirt\UsbUirt Managed Wrapper\bin\x86\Release\UsbUirtManagedWrapper.dll


It does not tell me why.
Not sure if TRUST was actually the issue, it may have been that I was compiling to an incompatible .NET version.
I recompiled the C# code to a lower .NET version  and everything is working now.