Why does Imports System not give access to Windows namespace in vbc.exe? - vb.net

I manage an application that allows the users to automate tasks by writing their own VB code. The user code is compiled using the VBCodeProvider and invoked against the running instance of the application. We've been doing this for a few years now starting with .NET 2.
Traditionally, we have imported the System namespace in the compiler settings so users wouldn't have to write System. all the time. When we went to .NET 4, however, we found that statements like Windows.Forms.Form wouldn't compile anymore. The error was "Type 'Windows.Forms.Form' is not defined." This is odd because other namespaces work. IO.Stream and Reflection.Assembly do not have an error without the System at the beginning.
I've created a simple example. I've put the below code into a file. Then I compiled this file with vbc.exe from both the .NET2 and .NET4 directories. The 2 version works fine. The 4 version will not compile unless you comment out the variable f2.
Imports System
Public Class MyClassName
Public Shared Sub Main
'this works in v2 and v4
Dim f As New System.Windows.Forms.Form
f.ShowDialog
'this does not work in v4
Dim f2 As New Windows.Forms.Form
f2.ShowDialog
End Sub
End Class
Does anyone know how to get this to compile in vbc.exe version 4? And before you say "Just tell the users to type System.Windows.Forms" I will agree that it would be great if they would do that, but users do what users do and I have to work it out.
UPDATE:
I've found that the Windows.Foundation.Diagnostics namespace is causing a collision with the abbreviated use of Window.Forms. Is there any way to hide this namespace from my compilation? Visual Studio 2010 does not have the same conflict, so it must be getting around it somehow.

Related

COMInterop - System cannot find file - IDE only

I've an old VB6 program which I have haven't used for several months. The program references numerous C# assemblies (.NetFramework 4.8). Running the programs as binaries, it all works fine.
I have opened the VB6 ide to step some of my code and I am now encountering an automation error "The system cannot find the file specified." (Err 80070002) when trying to instantiate one of the COM Interop classes. This didn't used to happen.
Private Sub InitMessageStore()
Dim l_oBusFactory As IfxBusService.BusFactory <= COM Interop reference
Set l_oBusFactory = New IfxBusService.BusFactory <= ERROR here
l_oBusFactory.InitialiseMessageStore GetConnection(m_oIfxsys.Dbase.Database.Definition)
End Sub
The COM Interop decls
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IBusFactory))]
[Guid("200C6C26-6881-4CB5-A8E7-E0E5532D6D5F")]
public class BusFactory : IBusFactory
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("5531BD20-2C7B-452B-A7D7-2D05E39EB83E")]
public interface IBusFactory
I have rechecked the registry registrations for the above ... the salient one being
and the file is in the folder location as specified in the registry.
I'm using Win10 Version 10.0.19044 Build 19044. I'm running everything as administrator. I have DEP switched to essential Windows programs and services only.
As I said above, everything works fine when run as binaries, but, to me, it looks like the OS is stopping the VB6 ide from loading a COM Interop assembly.
Any suggestions as to how I can get stepping through (but not necessarily into) my COM Interop code working again.
In the end, I missed that there were 2 entries under the InprocServer32, one of which pointed to the correct file (1.0.0.0), but the other one (5.80.0.111 - which is actually the version no of our com binaries) didn't point to a file:///, but rather the pure .net assembly. Once I deleted the incorrect one, it all started working ok.
Still not sure where the wrong one came from!

UseExternalSignInCookie is not a member of Owin.IAppBuilder

I have two VS 2013 MVC 5 SPA web projects. Both are newly created and have not been modified (i.e. still the vanilla template). One of them works and one does not. The only difference that I can see is that one was created in a project that has been upgraded from VS 2010 to VS 2012 and then to 2013 and is one of about 60-70 projects in the solution. The other is a newly created solution with the MVC app as the only project in it.
The fresh MVC app in it's own solution works without any problem, however, the MVC app that was added to the upgraded project gives the following compile time error:
'UseExternalSignInCookie' is not a member of 'Owin.IAppBuilder'. ...\Mvc5UI\App_Start\Startup.Auth.vb
'UseOAuthBearerTokens' is not a member of 'Owin.IAppBuilder'. ...\Mvc5UI\App_Start\Startup.Auth.vb
'GetExternalAuthenticationTypes' is not a member of 'Microsoft.Owin.Security.IAuthenticationManager'. ...\Mvc5UI\Controllers\AccountController.vb
The code where the problem is (Startup.Auth.vb):
Public Sub ConfigureAuth(app As IAppBuilder)
'...
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie)
app.UseOAuthBearerTokens(OAuthOptions)
'...
End Sub
and (AccountController.vb)
Public Function GetExternalLogins(returnUrl As String, Optional generateState As Boolean = False) As IEnumerable(Of ExternalLoginViewModel)
Dim descriptions As IEnumerable(Of AuthenticationDescription) = Authentication.GetExternalAuthenticationTypes()
'...
End Sub
This code is the same in both the upgraded solution and the newly built solution, so there's something environmental going on. I've made sure that the NuGet packages are setup correctly. The thing that I find most interesting (and is probably a misunderstanding on my part) is that when I explore the assembly metadata for IAppBuilder I don't see any of the methods that are referenced, so the error makes sense. However, then the question becomes, why does it work in the newly created MVC app?
As I was writing the question, I was able to determine the answer. The UseExternalSignInCookie and UseOAuthBearerTokens methods are actually extension methods on the IAppBuilder, which is why I was confused. These extension methods are found in Microsoft.Owin.Security.Cookies which was apparently not being referenced correctly.
After figuring that out and as I was going through the project references, I noticed that the Microsoft.AspNet.Identity.Owin assembly reference was broken, so I reinstalled that package with update-Package -reinstall Microsoft.AspNet.Identity.Owin package manager command and then everything started working.
I'm still not sure why this was broken to begin with, but it's working now.

How can I easily call an IronPython function from VB.net?

I am struggling with this program which uses emgucv(an opencv wrapper for .net) for about 2 weeks. The problem is unfortunately not programming, but setting up emgucv in such a way that it works. I didn't manage to do so for vb.net so I tried doing it for ironpython(because I know python too). Emgucv seems to work perfectly when using ironpython, so I created a function that takes an image as an argument and analyses it in the way I want, returning another image with the results in it. The problem is I want to call this function, giving it the image argument(it could be a string containing the path) from within VB.net and become another string containing the result image as return. I later plan to package that project in a setup so I can redistribute it.
So I am asking you guys: Do you know an easy way to call an IronPython function in VB.net in such a way so I can also package the whole project and redistribute it to people?
Thank you so much for reading this and it would be great if you could also help me with my problem! :)
While IronPython is not my expertise I am well versed in EMGU and its applications. If you insist in using IronPython the following website clearly shows how to pass a string to an IronPython Class.
The following code is taken from the link and is not my own:
Option Explicit On
Option Strict On
Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types
Module Module1
Sub Main()
Dim helloWorld As New HelloWorldVB()
Console.WriteLine(helloWorld.HelloWorld("Maurice"))
Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
Console.WriteLine(helloWorld.HelloWorld("Maurice"))
Console.ReadLine()
End Sub
End Module
I would follow the tutorial from the link but the important code is bellow as this imports the require runtime information for IronPython:
**Imports Microsoft.Scripting.Hosting
Imports IronPython.Hosting
Imports IronPython.Runtime.Types**
&
**Dim runtime As ScriptRuntime = PythonEngine.CurrentEngine.Runtime
Dim scope As ScriptScope = runtime.ExecuteFile("HelloWorld.py")
Dim pythonType As PythonType = scope.GetVariable(Of PythonType)("HelloWorldIronPython")
helloWorld = CType(runtime.Operations.Call(pythonType), HelloWorldVB)
Console.WriteLine(helloWorld.HelloWorld("Maurice"))**
Obviously Console.WriteLine(helloWorld.HelloWorld("Maurice")) would be corrected to:
Dim result_location As String = helloWorld.HelloWorld("Maurice")
Where "Maurice" would be the string containing your image location.
Now I have to ask about the problems you were having setting up EMGU in visual studio I know it can be frustrating to do especially to people who are new to it. If you would like I would be happy to help you set it up properly. The reason I ask is since you are providing this to and end user your code could be more efficient without calling IronPython. Especially since each process will require reading and writing from the hard drive.
To start: I will assume that you have included References to Emgu.CV, Emgu.CV.UI, and EMGU.Util in your project. But it is essential that you add "opencv_core220.dll", "opencv_imgproc220.dll" files directly to your project and ensure in the properties window that the 'Copy to Output' option is set to "Copy always". If it isn't you will get errors of not a having the image in the right format etc. You only really need these two .dll to read images in and access the data etc you may need others for .avi movies for example. Note that these two .dll must now be distributed with your project for it to work.
To note this may change depending if your using a 64 bit machine or a 32 bit machine but the 64 bit EMGU version will not run on X86 machines. You must also ensure that your target platform is correct in Visual Studio.
I hope this helps you,
Cheers
Chris

"System.IO.FileNotFoundException: Could not load file or assembly" when the assembly does in fact exist

As part of our migration from .net 1.1 to .net 3.5, we had to change out a few vender DLLs.
One of them is giving us trouble in only 1 spot out of the 4 spots we use it at:
The trouble spot is a windows form project that uses reflection to dynamically load some DLLs that run long running processes. One of these long running processes is an agent that relies on one of our vender DLLs
We're getting the missing assembly exception at the point where we first enter a function that references the library. I already checked the silly things such as if we had forgotten to move a reference from the old version to the new version, but that's not the case. I also checked the bin directory of the project and the assembly is there.
Has anyone encountered a situation in which .net 2.0 runtime refuses to load an assembly like that? And if so, how could we fix the issue.
Additional Information:
The specific vendor in this case is dtSearch and this is the boundry where the error gets thrown:
Private Sub BuildIndex()
SetIndexOptions()
ExecuteIndexJob()
End Sub
Private Sub SetIndexOptions()
'Body removed for brevity
End Sub
The library is referenced in SetIndexOptions. BuildIndex() gets entered, but the exception happens when SetIndexOptions gets called. The function is never actually entered.
If you are still having problems you can use the Assembly Binding Log Viewer (Fuslogvw.exe) to identify which assemblies are loaded by your application. This tool is part of the .NET Framework. This will provide you with details on all dependent assemblies.
I have used this in the past when working with third party assemblies, very usefull
The FileNotFoundException can be raised even when the assembly does exist if one of the dependent assemblies couldn't be loaded.
Try using Dependency Walker to check and see that all dependent assemblies are also present.
Turns out the default compile option changed from VS2003 to VS2008 and it was compiling in the wrong bitness :\ Now I feel a bit silly!

VB.NET and BITS - Background Intelligent Transfer Service

Has any one used BITs in VB.NET? If so, do you have code samples and advice?
I was looking at SharpBits but I have a VB project that I wanted to use BITS for. Is it possible to use it with my VB.NET program? (.NET 2.0) I was tempted to try to convert each class to VB.NET in the SharpBits.Base folder but figured I'd ask in case someone has headed down this route before.
Edit: Ok folks in case you run across this question. What you can do is in the Sharpbits.Base folder (that you download from codeplex) there is a DLL you can reference in the Bin directory. You can add that into your references to access it. Marking Konrad as answer since he was kind enough to post.
Further edit:
I managed to get sharpbits working with some quick code which I pasted below for anyone who might stumble upon this question. Like I mentioned above add the DLL to your project.
Dim b As New SharpBits.Base.BitsManager
Dim mynewjob As SharpBits.Base.BitsJob = _
b.CreateJob("jobname", SharpBits.Base.JobType.Download)
mynewjob.AddFile("\\server\share\bigfile.zip", "c:\bigfile.zip")
mynewjob.Resume()
You'll need to write some logic to check for the status of the job. Once it hits "Transferred" status you can then mark it as complete. This will write the file from a .bin to the file name you listed. Something that helped me was installing the Windows Support Tools (you can get it from a Windows 2003 Cd/DVD in the sup tools folder)and using Bitsadmin.exe to view the status of the job while debugging. Hope this helps the next rookie. =)
Any reason why you can't simply use SharpBits in VB? The advantage of .NET is precisely that libraries written in the different .NET languages can interoperate seamlessly so you can simply use SharpBits in VB, no matter what .NET-compliant language it was written in.
You could take a look here:
Using Windows XP Background Intelligent Transfer Service (BITS) with Visual Studio .NET
I have started from here to write my own library to manage BITS to transfer big video file across private LAN. Example are for NET 1.1 but should not be difficult port it to NET 2.0.
Here's a new alternative. The BITS team at Microsoft now has a page on Calling into BITS from .NET and C# using reference DLLs plus a complete sample call BITS Manager on GitHub.
I've just tried using them with Visual Basic; my code ended up looking like this:
Imports BITS = BITSReference1_5
Module Module1
Sub Main()
Dim mgr = New BITS.BackgroundCopyManager1_5
Dim jobGuid As BITS.GUID
Dim job As BITS.IBackgroundCopyJob
mgr.CreateJob("My simple job", BITS.BG_JOB_TYPE.BG_JOB_TYPE_DOWNLOAD, jobGuid, job)
job.AddFile("http://www.microsoft.com", "c:\temp\2019\BITS-VB\Downloadfile.html")
job.Resume()
End Sub
End Module
(Note that I also added a reference to a DLL that I downloaded from the BITS Manager source from the Reference DLL directory)