EnsureLibraryPresent error in huysentruitw's SapNwRfc connector with .net5 - c#

I'd like to use huysentruitw's SapNwRfc connector to connect to SAP. I have copied all the necessary files to project root (sapnwrfc.dll, icuuc50.dll, icuin50.dll, icudt50.dll) but I still got error when I try to call this:
SapLibrary.EnsureLibraryPresent();
the error I got:
System.BadImageFormatException
HResult=0x8007000B
Message=An attempt was made to load a program with an incorrect format. (0x8007000B)
Source=SapNwRfcDotNet
StackTrace:
at SapNwRfc.Internal.Interop.RfcInterop.RfcGetVersion(UInt32& majorVersion, UInt32& minorVersion, UInt32& patchLevel)
at SapNwRfc.Internal.Interop.RfcInterop.GetVersion(UInt32& majorVersion, UInt32& minorVersion, UInt32& patchLevel)
at SapNwRfc.SapLibrary.GetVersion()
at SapNwRfc.SapLibrary.EnsureLibraryPresent()
I have tried to changed platform from any CPU to x86 (then I got error from IIS Express), and x64 (the same behavior as any CPU)
Do you have any tips how to handle it?
Thank you.
EDIT
Tested with .NET Framework 4.7.2 and works fine. With Core 2.1, 3.1 and .NET5 does not work.
I have also tried it without copying the files into project folder and result is the same (files are present in System32 and SysWOW64 folders)

Related

Some clients are unable to load SQLite.Interop.dll

There is an application that uses SQLite. Link to System.Data.SQLite and SQLite.Interop.dll (for x64 and x86 platforms) added statically, version 1.0.112.0. (all 3 files) (SQLite was added to the test project via NuGet, and from there copied to the current project). The System.Data.SQLite and SQLite.Interop.dll files (in the x86 and x64 folders) are present on the client machine. .Net version is 4.7.2.
Most clients work fine, but a few throw an exception with the text "Unable to load DLL 'SQLite.Interop.dll'". I build test versions for x86 and x64 on my machine, in each case SQLite.Interop.dll was loaded from the corresponding folder (If SQLite.Interop.dll does not exist on the corresponding path, then there was an exception).
Where to look to avoid this exception on all client machines?
There is a hunch that the client is missing Microsoft Visual C ++ Redistributable. But later I find out that Redistributable was installed.
The next assumption is that the system can find SQLite.Interop.dll, but an error occurs during loading or initialization, which is interpreted by the wrapper(System.Data.SQLite) as DllNotFoundException.
Maybe the path pointing to the dll is too long?
We had a similar problem when the path that pointed to the dll was too long (More than 255 chars).
After a long search, the cause of the DllNotFoundException was found. This is because SQLite.Interop.dll has a dependency on msvcr120. (Dependency Walker was used to figure this out). But there is one small thing, if the application is for the x64 platform, then this file is expected to be found in the C:\Windows\System32 folder. But if the platform is x86, then the file should be on the path C:\Windows\SysWOW64. I saw that msvcr120 is in the System32 folder and decided that problem is not related to msvcr120.
The simplest solution is to build the application for the x86 platform, and place the msvcr120 next to the exe-file.
I found a better solution to the problem. The corresponding version of msvcr120 is located next to SQLite.Interop.dll in the x86 or x64 folder. Before using sqlite (for example, in the Application constructor or in its overloaded OnStartup() method) we need to add our folder to the PATH environment variable.
var pathVar = Environment.GetEnvironmentVariable("PATH");
string customDllFolder;
if (Environment.Is64BitProcess)
{
customDllFolder = Path.Combine(Environment.CurrentDirectory, "x64");
}
else
{
customDllFolder = Path.Combine(Environment.CurrentDirectory, "x86");
}
pathVar = string.Concat(pathVar, ";", customDllFolder);
Environment.SetEnvironmentVariable("PATH", pathVar, EnvironmentVariableTarget.Process);
This solution is better because we will only use a local copy of msvcr120 if it is not on the user's machine. We can also build an application for both platforms (x86 and x64). This approach can be used for other unmanaged libraries as well.

ServiceStack license error after packaging

In my ServiceStack client application, everything was fine until I have tried packaging it with SmartAssembly or ILRepack. There are three DLLs:
(ServiceStack.Client.dll, ServiceStack.Interfaces.dll, ServiceStack.Text.dll)
Which when packed cause my application to crash, no matter whether I use encryption\obfuscation options or just pack them into one EXE. My app is targeting .NET 4.0 (I want to support win XP). Stack trace of the exception is:
ServiceStack.LicenseException: Unauthorized access request of a licensed feature. Type: 'AccessToken', Assembly: 'ClientTester.exe', 'My EXE path'
ServiceStack.Net40PclExport.VerifyInAssembly(Type accessType, ICollection`1 assemblyNames)
ServiceStack.LicenseUtils.RequestAccess(Object accessToken, LicenseFeature srcFeature, LicenseFeature requestedAccess)
ServiceStack.JsonServiceClient.ToJson[T](T o)
ServiceStack.EncryptedServiceClient.CreateEncryptedMessage(Object request, String operationName, Byte[] cryptKey, Byte[] authKey, Byte[] iv, String verb)
ServiceStack.EncryptedServiceClient.Send[TResponse](String httpMethod, Object request)
I can't install the patch as the installer says that it is blocked or the conditions are invalid. In addition, I am not sure that this patch would help me. Any ideas are welcome.
Sidenote: another ServiceStack application, which uses other ServiceStack DLLs except aforementioned ones works well even when packed.
It looks like the absence of an explicitly installed .NET Framework 4.0 is causing that issue. If I compile the solution for 4.5\4.6 version and pack it - everything is fine. Also compiling and packing it for .NET 4.0 and using it on PC that EXPLICITLY has this version installed works as well

Access Denied VS 2010

i m trying to build COM dll file using C# and VS2010 - framework 3.5-:
if i try building the project without performing Clean, it will build, however this works only before any cleaning operation, after cleaning the project i will get stuck with this error
Error 16 Cannot register assembly "C:\Users\User\Desktop\Demo[Demo]
SCPP T1\bin\Debug\PrinterPlugin.dll" - access denied. Please make sure
you're running the application as administrator. Access to the
registry key
'HKEY_CLASSES_ROOT\CLSID{B9162A23-45F9-47CC-80F5-FE0FE9B9E1A2}' is
denied. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets 3912 5 PrinterPlugin
i have based the original files on FrameWork 4.0 then converted to 3.5 due to COM registration incompatibility. building with Framework 4.0 will build successfully but will produce "TYPELIB" file is missing while registering the library.
further elaboration, the old PrinterPlugin works like a charm based on framework 3.5, it would register correctly, and would function with no warnings or erros of what-so-ever, these errors started popping after adding some components from another SDK that have been distributed using framework 4.0 and uses other microsoft libraries, like bidispl.dll, ReachFramework... etc.
EDIT0
i already have tried starting the VS2010 as admin, from every location.
EDIT1
i have tried building a new project using Framework 3.5, and still facing the same error, even when executing the devenv.exe as admin from the Common7 > IDE folder.
Error 18 Cannot register assembly
"C:\Users\Ameer\Desktop\projects\PrinterPlugin without resize
17-05-2015\printerplugin without resize
10-05-2015\bin\Debug\PrinterPlugin.dll" - access denied.
Please make sure you're running the application as administrator.
Access to the registry key
'HKEY_CLASSES_ROOT\CLSID{B9162A23-45F9-47CC-80F5-FE0FE9B9E1A2}' is
denied. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets 3912 5 PrinterPlugin
i m guessing this is something has to do with the compatibility between the two SDKs.

Looks like MSVS 11.0 Beta spoiled a MSVS 10.0 installation

I ran into compilation problems with my MSVS 10 after installing MSVS 11Beta. Now, when I compile my C# Projects in MSVS 10 (Projects created in MSVS 10; Target framework: 3.5), I get errors MSB4216, MSB4028 with following text in output window:
1>Task "GenerateResource" skipped, due to false condition; ('%(EmbeddedResource.Type)' == 'Resx' and '%(EmbeddedResource.GenerateResource)' != 'false' and '$(GenerateResourceMSBuildRuntime)' != 'CLR2') was evaluated as ('Resx' == 'Resx' and '' != 'false' and 'CLR2' != 'CLR2').
1>Task "GenerateResource"
1> Launching task "GenerateResource" from assembly "Microsoft.Build.Tasks.v3.5, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" in an external task host with a runtime of "CLR2" and a process architecture of "x86".
1>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(2199,5): error MSB4216: Could not run the "GenerateResource" task because we could not create or connect to a task host with runtime "CLR2" and architecture "x86". Please ensure that (1) the requested runtime and/or architecture are available on the machine, and (2) that the required executable "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NetFX 4.0 Tools\MSBuildTaskHost.exe" exists.
1>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(2217,7): error MSB4028: The "GenerateResource" task's outputs could not be retrieved from the "FilesWritten" parameter. Object does not match target type.
1>Done executing task "GenerateResource" -- FAILED.
How can I fix these errors?
EDIT:
Mentioned file "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NetFX 4.0 Tools\MSBuildTaskHost.exe" exists.
C++ projects are compling without problems.
Reinstalling VS10 doesn't solve the problem.
This is GenerateResource task, that must compile resx-file, but generates an exception:
<!-- But we can't use those parameters if we're targeting 3.5, since we're using the 3.5 task -->
<GenerateResource
Sources="#(EmbeddedResource)"
UseSourcePath="$(UseSourcePath)"
References="#(ReferencePath)"
AdditionalInputs="$(MSBuildAllProjects)"
NeverLockTypeAssemblies="$(GenerateResourceNeverLockTypeAssemblies)"
StateFile="$(IntermediateOutputPath)$(MSBuildProjectFile).GenerateResource.Cache"
StronglyTypedClassName="%(EmbeddedResource.StronglyTypedClassName)"
StronglyTypedFileName="%(EmbeddedResource.StronglyTypedFileName)"
StronglyTypedLanguage="%(EmbeddedResource.StronglyTypedLanguage)"
StronglyTypedNamespace="%(EmbeddedResource.StronglyTypedNamespace)"
StronglyTypedManifestPrefix="%(EmbeddedResource.StronglyTypedManifestPrefix)"
PublicClass="%(EmbeddedResource.PublicClass)"
OutputResources="#(EmbeddedResource->'$(IntermediateOutputPath)%(ManifestResourceName).resources')"
MSBuildRuntime="$(GenerateResourceMSBuildRuntime)"
MSBuildArchitecture="$(GenerateResourceMSBuildArchitecture)"
Condition="'%(EmbeddedResource.Type)' == 'Resx' and '%(EmbeddedResource.GenerateResource)' != 'false' and '$(GenerateResourceMSBuildRuntime)' == 'CLR2'">
I tried to debug MSBuild script (.csproj). Just before the fatal GenerateResource task I checked all the properties and items. There was nothing about "8.0A" but only about "7.0A"
There is an ugly way to fix the problem: renaming folder "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A". I hate to accept this answer.
How long is your username?
It seems that there is a bug when the username is 20 characters long. If your username is 19 characters or less it works fine.
I have opened an issue on connect.
Edit:
Have you tried setting the environment variable DisableOutOfProcTaskHost to true as suggested in the connect issue, that worked for me.
A similar questin was asked on the MSDN forums. Did you reboot after installing VS11?
http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/7d955d96-ff73-47d3-8830-85ea321eb4ab
This issue occurred for me on my TFS 2010 build server after installing VS2010 and then installing .NET Framework 4.5. This allowed me to build .NET 4.5 projects but any VS2008 project targeting CLR2 (.NET 2.0 -3.5) I attempted to build would return the error. None of the suggestions on the handful of sites talking about this issue worked.
Rebooting - Did not fix the issue
Setting DisableOutOfProcTaskHost = true in an environment variable and also within the project file - Did not fix the issue
Username of my build account was already less than 20 characters
Renaming windows SDK folder - N/A since I did not have VS2012 and the 8.0A SDK installed... just .NET framework 4.5.
In any case, to fix this I uninstalled .NET 4.5, repaired VS2010, and then rebooted the build server. Now I am able to build VS2008 and VS2010 projects with no issues.
Luckily I no longer need to build .NET 4.5 projects as that team decided to go back to .NET 4.0.
Another possible fix is to change the target framework of your projects to .Net 4.0. It's not always a solution, but it certainly is a possibility if all else fails.
Worked for me: Removing the bin and obj folder for problematic folders and restarting the solution
In my case, I received that error message when trying to build a solution on a 32-bit Windows 7 machine. The way to resolve the error for me was to right-click on the project, choose properties, then go to the Build tab. In here I changed the "Platform target" from "Any CPU" to "x86". HTH
Setting Setting DisableOutOfProcTaskHost = true in an environment variable worked for me.

Cannot encrypt / decrypt SQLite database in .NET4

I am running Visual Studio 2010 on a Windows 7 x64. The app I'm writing is supposed to run on all platforms (AnyCPU). I'm able to encrypt/decrypt the database file if I'm using the System.Data.SQLite.dll either x86 version (sqlite-netFx40-static-binary-bundle-Win32-2010-1.0.77.0.zip) or x64 one (sqlite-netFx40-static-binary-bundle-x64-2010-1.0.77.0.zip).
I need my app to run on both platforms x86 and x64 (Any CPU project build setting). I've tried installing the ADO.NET 4.0 Provider (SQLite-1.0.67.1-vs2010-net4-setup.exe). I added the reference (right-click on project, Add Reference, .NET tab -> System.Data.SQLite) and ran the program. If I decrypt the file and try to encrypt by calling ChangePassword("myPass"), I get the following exception:
System.EntryPointNotFoundException was caught
Message=Unable to find an entry point named 'sqlite3_rekey' in DLL 'System.Data.SQLite.DLL'.
Source=System.Data.SQLite TypeName=""
StackTrace:
at System.Data.SQLite.UnsafeNativeMethods.sqlite3_rekey(IntPtr db, Byte[ key, Int32 keylen)
at System.Data.SQLite.SQLite3.ChangePassword(Byte[ newPasswordBytes)
at System.Data.SQLite.SQLiteConnection.ChangePassword(Byte[ newPassword)
at System.Data.SQLite.SQLiteConnection.ChangePassword(String newPassword)
at SQLiteTest.Database.Encrypt() in C:\SQLiteTest\Database.cs:line 166
Also, I've tried to open a connection using the SQLiteConnection object and I get two different exceptions in two different cases.
First, if the file is encrypted and I don't specify a password in the connection string I get this:
System.Data.SQLite.SQLiteException was caught
Message=File opened
that is not a database file file is encrypted or is not a database
Source=System.Data.SQLite ErrorCode=-2147467259
StackTrace:
at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous,
UInt32 timeoutMS, String& strRemain)
at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior
behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
at System.Data.SQLite.SQLiteConnection.Open()
at SQLiteTest.Database.GetAllLanguages() in C:\SQLiteTest\Database.cs:line 216
Second, if I add the password parameter to the connection string, I get the System.EntryPointNotFoundException like the one above.
So, does anyone know a sure-fire way to work with encrypted SQLite database in a C# app on AnyCPU platform?
Thanks in advance!
Following Solution is a little bit dirty, but it may work, we didn't use encrypted database so i'm not 100% if that will work for you.
We downloaded both versions of sqlite dlls for x64 and x86 and placed them in different folders. We load them manually dependent on what platform application is currently running ( we check IntPtr.Size , from .net 4 there is a property in Environment class called Is64BitOperatingSystem) when we loaded assembly manually we get connection instance with
var sqliteConnectionType = assembly.GetType("System.Data.SQLite.SQLiteConnection");
(DbConnection)Activator.CreateInstance(sqliteConnectionType);
we don't use reference to any version of sqlite in our project.

Categories