How can I easily call an IronPython function from VB.net? - 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

Related

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

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.

Running Data From External Classes

Over the past few months I have been struggling to find a way to execute external un-compiled classes from my program.
EXAMPLE:
I have a simple run button on a windows forum. When that run button is pressed I wan't to run the Main sub in run.vb. (Run.vb is not part of the program and is located in a directory)
Is their a way to do this without using .dll's?
You want to run VB.Net code as I see. There are a bunch of classes in System.CodeDom.Compiler namespace to deal with .Net source code.
See http://www.codeproject.com/Articles/5472/Compiling-NET-code-on-the-fly
There is a good example with explanation here.
If you want to run VB6 code, the only way is compiling it to dll and then dynamically load and run. Or you can use VBscript if it will suit you.
In fact, CodeDom compiles your code to dll assembly too, but keeps it in memory, so you don't need to clean up any mess after you app is dead

Using long paths in Visual Basic

I use the Directory.getLastWriteTimeUtc() method. If the path is too long for this method it throws an PathTooLongException. So I started to use Google finding a solution for this problem. A couple of search results adviced me to use \\?\ as a prefix. Using the Windows Explorer this solution works perfectly. But it does not work with Visual Basic. Adding this prefix to the path the result is an ArgumentException because the is an illegal character in the path.
So I want to ask you if you have a solution to handle long paths with Visual Basic.
The snippet of code that causes the problem is the following one. It is used to get the most recent file of a folder.
Oh, I'm sorry. Of course I will show you my snippet of code that causes the problem.
Try
If Directory.GetLastWriteTimeUtc(s) > Directory.GetLastWriteTimeUtc(latest) Then
latest = s
End If
Catch e As PathTooLongException
Console.WriteLine("error...")
End Try
I solved the problem by subsitute the Delimon.Win32.IO for the System.IO library. Now I can handle such long paths. Hope I can help anyone later.

Enforcing using the class name whenever a shared member is accessed

We have a coding standard that says all shared (static) fields and methods must be called with the class name. E.g.
NameOfClass.whatever
Rather then
whatever
Is there a tool that we can use to check this is in fact the case?
(Likewise for modules)
Sorry I should have make it clearer we are using VB.NET.
This is a bigger example of what I mean.
Public Class Class1
Public Shared Sub SharedMethod()
End Sub
Public Shared sharedField As Integer
Public Sub NotSharedMethod()
'this next line shold be written as Class1.SharedMethod
SharedMethod()
'this next line shold be written as Class1.sharedField
sharedField = 5
End Sub
End Class
see also What StyleCop like tools are there for VB.NET
Yes,
Use StyleCop and write your custom rule to do your check.
Here's a reference to check how to write custom-rule for StyleCop.
Sure, just create a custom rule in StyleCop. Then incorporate the use of StyleCop into your automated build process.
Sorry, I did not even realize that StyleCop did not have a VB.NET version. What you need is a static analysis tool for VB.NET. Based on this thread, it looks like Project Analyzer is an option. Unfortunately it's not free.
From the web site:
Maintain. To help maintenance, Project Analyzer lets you enforce coding standards[.]
Whatever tool you use, be sure it incorporate it into your automated build process.
In the Project Properties > Compile, you can set the Warning configuration for "Instance Variable access shared member" to Error and it should generate a compiler error instead of a warning.
I'm not sure how you might do it for all projects. You could change the project template to include that option for all new projects.
Sorry I never did find a good tools for this.
You might be able to use FxCop for this purpose, and write a custom rule. Here is a good site that explains how to write custom FxCop rules.

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)