I'm trying to connect to a hyperSQL database (HSQLDB) on my local machine from a c# application in visual studio.
I've ran through the steps to create the .net JDBC driver to build the dll, and also downloading the demo console application in the "download" section from the following url: http://nikolaiklimov.de/query-java-HyperSQL-database-with-csharp/
The demo application works!! i can connect to the database and query its contents . Next I have converted the console app into a class library then call the class library to query the DB but this is where it falls over and I get initialization errors of System.TypeInitializationException. any idea why the project falls over after converting to a class library. (if i convert the class library straight back a console app it works again).
The code and connection string are:
namespace HyperSQL
{
public static class sqlconnector
{
readonly static string CONNECTION_STRING =
ConfigurationManager.ConnectionStrings["HyperSQL"].ConnectionString;
const string SQL = "SELECT * FROM meeting";
public static void getdata()
{
try
{
java.sql.DriverManager.registerDriver(new org.hsqldb.jdbcDriver());
using (java.sql.Connection conn = java.sql.DriverManager.getConnection(CONNECTION_STRING))
{
java.sql.PreparedStatement ps = conn.prepareStatement(SQL);
using (java.sql.ResultSet rs = ps.executeQuery())
{
while (rs.next())
{
Console.WriteLine($"MEETING_NO={rs.getInt("MEETING_NO")}");
Console.WriteLine("------------------");
}
}
}
}
catch (Exception ex)
{
}
Console.ReadLine();
}
}
}
jdbc:hsqldb:hsql://localhost:3458/elitedb;crypt_key=DADADADADAADDADAD;crypt_type=AES;shutdown=true;write_delay=false;user=****;password=****
Ive added a console app to my solution after the conversion to a class libary. The console app code is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
HyperSQL.sqlconnector.getdata();
}
catch (Exception ex)
{
}
}
}
}
Here is a link to download my solution created in visual studio. It contains a console application that calls a function in a class library. The class library (HyperSQL) itself is simply converted from a console application and can be converted back. You will need a running instance of HyperSQL on your machine to be able to connect successfully.
http://eliteservicedev.azurewebsites.net/DemoHyperSQL.zip
Have you seen the error listed in the original site you posted?
If your forget to add runtime dependencies to the build folder of your
project then you can get these errors: Could not load file or
assembly 'IKVM.OpenJDK.Util, Version=8.1.5717.0, Culture=neutral,
PublicKeyToken=13235d27fcbfff58' or one of its dependencies. The
system cannot find the file specified. TypeInitializationException:
The type initializer for 'org.hsqldb.jdbc.JDBCDriver' threw an
exception: FileNotFoundException: Could not load file or assembly
'IKVM.Runtime, Version=8.1.5717.0, Culture=neutral,
PublicKeyToken=13235d27fcbfff58' or one of its dependencies. 'The
type initializer for 'sun.util.locale.provider.LocaleProviderAdapter'
threw an exception.' FileNotFoundException: Could not load file or
assembly 'IKVM.OpenJDK.Text, Version=8.1.5717.0, Culture=neutral,
PublicKeyToken=13235d27fcbfff58' or one of its dependencies. 'The
type initializer for 'org.hsqldb.HsqlDateTime' threw an exception.'
InvalidCastException: Unable to cast object of type
'java.util.PropertyResourceBundle' to type
'sun.util.resources.OpenListResourceBundle'.
***** UPDATE
With the source project i see an error, looking in the bin\debug folder i see that non all the referenced file are copied to ConsoleApp1.exe bin folder: this is the compare result:
Confronto in corso dei file consoleapp1.txt e DEMOCSHARPTHYPERSQL.TXT
***** consoleapp1.txt
ConsoleApp1.exe
ConsoleApp1.exe.config
ConsoleApp1.pdb
hsqldb.dll
HyperSQL.dll
***** DEMOCSHARPTHYPERSQL.TXT
hsqldb.dll
HyperSQL.dll
*****
***** consoleapp1.txt
IKVM.OpenJDK.Jdbc.dll
IKVM.OpenJDK.Text.dll
***** DEMOCSHARPTHYPERSQL.TXT
IKVM.OpenJDK.Jdbc.dll
IKVM.OpenJDK.Localedata.dll --> Missing in ConsoleApp1.exe folder
IKVM.OpenJDK.Text.dll
*****
If i manually copy the missing dll on ConsoleApp1.exe folder the program run correctly. I think that is a visual studio bug
Seems VS is not copying all IKVM DLLs to output folder due to not resolving they are needed. Please see this SourceForge issue and this GitHub issue, the later one with my comments.
Related
I want to use CppSharp to create a C# wrapper for some classes from a C++ library which I intend to use from C# (on a 64 Bit Windows 10 system).
Unfortunately I am absolutely uncertain how to proceed. I cloned CppSharp into my filesystem and built it as described in the 'Getting started' section
https://github.com/mono/CppSharp/blob/main/docs/GettingStarted.md
I also ran all the test successfully.
From what I read in the documentation I was under the impression that to use CppSharp one has to create a C# application (for simplicity a console application), implement an abstract class from CppSharp and execute some commands from CppSharp. From
https://github.com/mono/CppSharp/issues/82
EDIT: Now I use the following code
using CppSharp;
using CppSharp.AST;
using CppSharp.Generators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CppSharpTransformer
{
class DllDemoGenerator : ILibrary
{
public static void Main(string[] args)
{
ConsoleDriver.Run(new DllDemoGenerator());
}
void Setup(Driver driver)
{
var options = driver.Options;
options.GeneratorKind = GeneratorKind.CSharp;
var module = options.AddModule("Sample");
module.IncludeDirs.Add(#"C:\Users\Boehm.J\Documents\CppSharpSamples\Sample\include");
module.Headers.Add("Sample.h");
module.LibraryDirs.Add(#"C:\Users\Boehm.J\Documents\CppSharpSamples\Sample\lib");
module.Libraries.Add("Sample.lib");
}
public void SetupPasses(Driver driver) { }
public void Preprocess(Driver driver, ASTContext ctx)
{
}
public void Postprocess(Driver driver, ASTContext ctx)
{
}
void ILibrary.Setup(Driver driver)
{
}
}
}
which is in Program.cs of my Console application I called CppSharpTransformer.
I could make it compile ok (Visual Studio 2022, Console Project with NET 6.0) by adding the following DLLs to my project:
CppSharp.AST.dll
CppSharp.CLI.dll
CppSharp.CppParser.dll
CppSharp.dll
CppSharp.Generator.dll
CppSharp.Parser.Bootstrap.dll
CppSharp.Parser.CLI.dll
CppSharp.Parser.CSharp.dll
CppSharp.Parser.dll
CppSharp.Parser.Gen.dll
CppSharp.Runtime.dll
I added them under "Dependencies (Abhängigkeiten) > Assemblys" and also as files as members of my project.
But I can not execute the application currently, as I get the runtime error:
Could not load file or assembly 'CppSharp.Parser.CLI, Version=0.0.0.0,
Culture=neutral, PublicKeyToken=null'. Das System kann die angegebene
Datei nicht finden.
(The german text says "The system can not find the specified file").
But the dll of this name is, it seems, in place. It occurs in
C:\Users\Boehm.J\Documents\work\CppSharpTransformer\CppSharpTransformer
and in
C:\Users\Boehm.J\Documents\work\CppSharpTransformer\CppSharpTransformer\bin\x64\Release\net6.0
I've just run into the same issue.
Make sure you copy the Ijwhost.dll into your application's output path.
For example like this:
<Target Name="CopyNative" AfterTargets="Build">
<Copy SourceFiles="..\CppSharp\bin\Release_x64\Ijwhost.dll" DestinationFolder="$(OutputPath)" />
</Target>
Should it be possible to use ef-core-5.0 in a native c++ application using an c++/cli interop assembly with .net core 5 as the target framework? To experiment with this I've modified the netcore branch of the CppCliMigrationSample (https://github.com/mjrousos/CppCliMigrationSample), changing the from "netcoreapp3.1" to "net5.0" for CppCliInterop and the from "net47;netcoreapp3.1" to "net5.0-windows" for the ManagedLibrary. The sample builds and works as expected with these changes. However, adding the efcore 5 assemblies to ManagedLibrary and trying to a simple query on a scaffolded model fails with the following error:- System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=5.0.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.'. I figure I need an entry in the app's .config but I'm not sure what it should be. Can anyone help with resolving this?
Following is what I have in my modified ManagedLibrary.csproj:-
ManagedLibrary.csproj
Sample query:
using ManagedLibrary.Context;
using ManagedLibrary.Model;
using System.Linq;
namespace ManagedLibrary
{
public static class Mi2cdbData
{
public static Prjdata GetFirstProject()
{
using (var context = new BaseContext())
{
return context.Prjdata.FirstOrDefault();
}
}
}
}
The following shows how it's being called in CppClinterop.cpp:-
CppClinterop.cpp
I am trying to create a custom printer driver to generate images. For this, I have installed Printer++ which converts print files to postscripts. To convert postscript file to image, I am using ghostscript. Independently both the processes are running fine and I am able to achieve what is required.
But, I need a custom process to generate images in one go. I followed through the Printer++ tutorial but it didn't work.
This is what I have done:
I installed Printer++ and gave the name of the printer driver as- Septane.
In Visual Studio, I created a project- Test.
And the following code in Processor.cs class:
using System;
using System.Net.Mail;
using PrinterPlusPlusSDK;
namespace Test
{
public class Processor : PrinterPlusPlusSDK.IProcessor
{
public PrinterPlusPlusSDK.ProcessResult Process(string key, string psFilename)
{
//Convert PS to Png
psFilename = "b.ps";
MessageBox.Show("Rahul");
ConvertPsToTxt(psFilename);
}
public static string ConvertPsToTxt(string psFilename, string txtFilename)
{
var retVal = string.Empty;
var errorMessage = string.Empty;
var command = "C:\\PrinterPlusPlus\\gs\\gswin64.exe";
var args = string.Format("-dNOPAUSE -dBATCH -dFirstPage=1 -q -r300 -sDEVICE=png256 -sOutputFile=", psFilename, txtFilename);
retVal = Shell.ExecuteShellCommand(command, args, ref errorMessage);
return retVal;
}
}
}
This class inherits from PrinterPlusPlusSDK.IProcessor and implements the PrinterPlusPlusSDK.ProcessResult Process function. I have tested the standalone console project (without using PrinterPlusPlusSDK processor) and that converts ps to png successfully.
Now, as per the tutorial, the dll needs to be deployed to printer++ and registered as a processor. I copied Test.dll to Printer++ installation folder and added an entry to PrinterPlusPlus.exe.config file.
The config entry looks like:
<processors>
<add key="Test"
value="Test.Processor, Septane, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</processors>
That's it. Now, when I print a file, it gives an error:
Proccessor not found for: Septane
What am I doing wrong?
If anyone has better idea for achieving the same, please let me know. Actually, mine is a commercial product so can't use CutePDF/VerPDf kind options.
Edit: I now know why I was getting error- Processor not found. I renamed my printer to Test and the error disappeared. I have edited my code as well.
For testing, I have added a message box. I expected to get a popup once I give print command. But that is not the case. The ps file is getting generated without an error and that's it. I can't view pop-up message and there is no png converted file. Can someone please help me resolve this issue at least? It doesn't seem to be picking up the dll at all.
Thanks.
Remember The Printer name must be same as the you dll name
If your printer driver name is Septane. Then you must have to create a project name with "Septane". In that case project name "Test" will not work.
I am new to Prolog and C#. When I try to integrate Prolog with C# I found some errors,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SbsSW.SwiPlCs;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// Environment.SetEnvironmentVariable("SWI_HOME_DIR", #"the_PATH_to_boot32.prc"); // or boot64.prc
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" }; // suppressing informational and banner messages
PlEngine.Initialize(param);
PlQuery.PlCall("assert(father(martin, inka))");
PlQuery.PlCall("assert(father(uwe, gloria))");
PlQuery.PlCall("assert(father(uwe, melanie))");
PlQuery.PlCall("assert(father(uwe, ayala))");
using (var q = new PlQuery("father(P, C), atomic_list_concat([P,' is_father_of ',C], L)"))
{
foreach (PlQueryVariables v in q.SolutionVariables)
Console.WriteLine(v["L"].ToString());
Console.WriteLine("all children from uwe:");
q.Variables["P"].Unify("uwe");
foreach (PlQueryVariables v in q.SolutionVariables)
Console.WriteLine(v["C"].ToString());
}
PlEngine.PlCleanup();
Console.WriteLine("finished!");
}
}
}
}
It is not running, Before run this I was add reference SwiPlCs.dll. But it shows error "FileNotFoundException was unhandled, The specified module could not be found. (Exception from HRESULT: 0x8007007E)".
So can anyone help me to fix this error?
I got this coding here
The link you've provided has full explanation of this error:
If libswipl.dll or one of its dependencies could not found you will recive an error like
System.IO.FileNotFoundException: Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)
An other common error is:
SWI-Prolog: [FATAL ERROR:
Could not find system resources]`
Failed to release stacks
To fix this add the SWI_HOME_DIR environment variable as described in SWI-Prolog FAQ FindResources with a statment like this befor calling PlEngine.Initialize.
Environment.SetEnvironmentVariable("SWI_HOME_DIR", #"the_PATH_to_boot32.prc");
This code is commented by default, Uncomment it and provide the correct path to the the_PATH_to_boot32.prc. As described in FAQ:
Solution
On Windows, it suffices to leave libswipl.dll in the installation tree (i.e., do not copy it elsewhere) and add the bin directory of the installation tree to %PATH%.
A cross-platform and robust solution is to use putenv() to put an appropriate path into the environment before calling PL_initialise().
...;
putenv("SWI_HOME_DIR=C:\\Program Files\\swipl");
if ( PL_initialise(argc, argv) )
PL_halt(1);
...
In the final version of your application you link the saved-state to the executable (using swipl-ld or cat (Unix)) and comment the putenv() call.
I'm using ASP.NET MVC3, StructureMap and SharpSvn.
Here's the code that I'm using:
public class SvnFileReader : ISvnFileReader
{
private readonly string _svnAddress;
public SvnFileReader(string svnAddress)
{
_svnAddress = svnAddress;
}
public void DownloadFiles(DirectoryInfo destination)
{
using (var svnClient = new SvnClient())
{
// checkout the code to the specified directory
svnClient.CheckOut(new Uri(_svnAddress), destination.FullName);
}
}
}
When I execute this code:
_svnFileReader.DownloadFiles(new System.IO.DirectoryInfo(#"d:\test"));
I get the following error message:
Could not load file or assembly
'file:///D:\Projects\SvnFileReaderDemo\bin\SharpSvn-DB44-20-Win32.dll'
or one of its dependencies. The module
was expected to contain an assembly
manifest.
Any help would be greatly appreciated!
You should exclude the SharpSvn DLLs from StructureMap automatic assembly scanning for dependencies. This is an unmanaged library but because you have configured StructureMap to look for types in all dlls when it tries to load this one it breaks.
UPDATE:
If you are running this code on a x64 bit OS you may try downloading the specific x64 SharpSvn which uses SharpSvn-DB44-20-x64.dll.