-
Mar 2nd, 2020, 06:59 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Help with converting few lines of C# to VB.NET
Hello,
in my project I'm using the Report Viewer control. And to be able to use it I need to include the Loader.cs class. But I'm using VB.NET.
Could you help me to translate the class to VB.NET?
I did try with some auto converters online, but it doesn't work.
Here is the Loader.cs code:
Code:
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace SqlServerTypes
{
/// <summary>
/// Utility methods related to CLR Types for SQL Server
/// </summary>
public class Utilities
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr LoadLibrary(string libname);
/// <summary>
/// Loads the required native assemblies for the current architecture (x86 or x64)
/// </summary>
/// <param name="rootApplicationPath">
/// Root path of the current application. Use Server.MapPath(".") for ASP.NET applications
/// and AppDomain.CurrentDomain.BaseDirectory for desktop applications.
/// </param>
public static void LoadNativeAssemblies(string rootApplicationPath)
{
var nativeBinaryPath = IntPtr.Size > 4
? Path.Combine(rootApplicationPath, @"SqlServerTypes\x64\")
: Path.Combine(rootApplicationPath, @"SqlServerTypes\x86\");
LoadNativeAssembly(nativeBinaryPath, "msvcr120.dll");
LoadNativeAssembly(nativeBinaryPath, "SqlServerSpatial140.dll");
}
private static void LoadNativeAssembly(string nativeBinaryPath, string assemblyName)
{
var path = Path.Combine(nativeBinaryPath, assemblyName);
var ptr = LoadLibrary(path);
if (ptr == IntPtr.Zero)
{
throw new Exception(string.Format(
"Error loading {0} (ErrorCode: {1})",
assemblyName,
Marshal.GetLastWin32Error()));
}
}
}
}
And here is what I got with online code converter:
Code:
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Namespace SqlServerTypes
''' <summary>
''' Utility methods related to CLR Types for SQL Server
''' </summary>
Public Class Utilities
Private Declare Function LoadLibrary Lib "kernel32.dll" (ByVal libname As String) As IntPtr
''' <summary>
''' Loads the required native assemblies for the current architecture (x86 or x64)
''' </summary>
''' <param name="rootApplicationPath">
''' Root path of the current application. Use Server.MapPath(".") for ASP.NET applications
''' and AppDomain.CurrentDomain.BaseDirectory for desktop applications.
''' </param>
Public Shared Sub LoadNativeAssemblies(ByVal rootApplicationPath As String)
Dim nativeBinaryPath = Path.Combine(rootApplicationPath, "SqlServerTypes\x64\")
'TODO: Warning!!!, inline IF is not supported ?
(IntPtr.Size > 4)
Path.Combine(rootApplicationPath, "SqlServerTypes\x86\")
Utilities.LoadNativeAssembly(nativeBinaryPath, "msvcr120.dll")
Utilities.LoadNativeAssembly(nativeBinaryPath, "SqlServerSpatial140.dll")
End Sub
Private Shared Sub LoadNativeAssembly(ByVal nativeBinaryPath As String, ByVal assemblyName As String)
Dim path = path.Combine(nativeBinaryPath, assemblyName)
Dim ptr = Utilities.LoadLibrary(path)
If (ptr = IntPtr.Zero) Then
Throw New Exception(String.Format("Error loading {0} (ErrorCode: {1})", assemblyName, Marshal.GetLastWin32Error))
End If
End Sub
End Class
End Namespace
My projects:
Virtual Forms
VBA Telemetry
-
Mar 2nd, 2020, 07:28 AM
#2
Re: Help with converting few lines of C# to VB.NET
Why bother translating the code at all? Why not simply compile the the C# code into a DLL and reference it? The whole .NET Framework is written in C# and you use that well enough.
-
Mar 2nd, 2020, 08:58 AM
#3
Re: Help with converting few lines of C# to VB.NET
There are 2 main problems with the conversion:
1. The 'If' operator is a pretty clear conversion of the C# ternary conditional operator - the converter should have handled it:
Code:
Dim nativeBinaryPath = If(IntPtr.Size > 4, Path.Combine(rootApplicationPath, "SqlServerTypes\x64\"), Path.Combine(rootApplicationPath, "SqlServerTypes\x86\"))
2. VB is case-insensitive, so if you have *anything* with the same name as the variable in the variable initializer even if the casing is different, VB thinks you're referring to the variable, so you have to fully qualify the term in the initializer:
Code:
Dim path = System.IO.Path.Combine(nativeBinaryPath, assemblyName)
Otherwise, VB thinks "Path" is the local variable you are declaring in the same statement.
Last edited by David Anton; Mar 2nd, 2020 at 09:13 AM.
-
Mar 2nd, 2020, 05:26 PM
#4
Re: Help with converting few lines of C# to VB.NET
I agree with JMC but if you truly want it in VB.NET
Code:
''' <summary>
''' Utility methods related to CLR Types for SQL Server
''' </summary>
Public Class Utilities
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function LoadLibrary(ByVal libname As String) As IntPtr
End Function
''' <summary>
''' Loads the required native assemblies for the current architecture (x86 or x64)
''' </summary>
''' <param name="rootApplicationPath">
''' Root path of the current application. Use Server.MapPath(".") for ASP.NET applications
''' and AppDomain.CurrentDomain.BaseDirectory for desktop applications.
''' </param>
Public Shared Sub LoadNativeAssemblies(ByVal rootApplicationPath As String)
Dim nativeBinaryPath = If(IntPtr.Size > 4,
Path.Combine(rootApplicationPath, "SqlServerTypes\x64\"),
Path.Combine(rootApplicationPath, "SqlServerTypes\x86\"))
LoadNativeAssembly(nativeBinaryPath, "msvcr120.dll")
LoadNativeAssembly(nativeBinaryPath, "SqlServerSpatial140.dll")
End Sub
Private Shared Sub LoadNativeAssembly(ByVal nativeBinaryPath As String, ByVal assemblyName As String)
Dim filePath = Path.Combine(nativeBinaryPath, assemblyName)
Dim ptr = LoadLibrary(filePath)
If ptr = IntPtr.Zero Then
Throw New Exception($"Error loading {assemblyName } (ErrorCode: {Marshal.GetLastWin32Error() })")
End If
End Sub
End Class
-
Mar 2nd, 2020, 05:53 PM
#5
Thread Starter
Addicted Member
Re: Help with converting few lines of C# to VB.NET
Thank you all!!
After a little of thinking, it is good starting point to also give C# a go in my life, and at the end, I did what JMC recommended. I created a dll with the C# and it works.
Once again, thank you all.
Davor
My projects:
Virtual Forms
VBA Telemetry
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|