Consuming FORTRAN DLL in C# via COM (P/invoke) - c#

I'm currently trying to consume a FORTRAN DLL that I have been given by a third party. Now, it has been consumed before by other vendors (unsure whether they used C# or not to consume it), but I'm getting some reference errors when I'm trying to consume it.
I'm basically trying to get it to work within a small test app.
Here's the C# code I'm using to import (basic COM really):
[DllImport("foo.dll")]
public static extern void foo(ref int IS, ref double[] BETA, ref int K, out double TH, out double SETH, out int IER);
static void Main(string[] args)
{
double[] betas = new double[3];
betas[0] = 25.6;
betas[1] = 30.8;
betas[2] = 35.8;
int score = 5;
int numberOfItems = 3;
double latentVariable;
double standardErrorOfEstimate;
int errorCode;
foo(ref score, ref betas, ref numberOfItems, out latentVariable, out standardErrorOfEstimate, out errorCode);
Console.ReadLine();
}
Note: The DLL method signature matches that what I have.
When trying to run the application, I get the following Exception:
Unable to load DLL 'foo.dll': The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail. (Exception from HRESULT: 0x800736B1)
Looking in the event log, the following details is shown on the error:
Activation context generation failed for "C:\dllpath\foo.dll". Dependent Assembly Microsoft.VC90.DebugCRT,processorArchitecture="x86",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="9.0.21022.8" could not be found. Please use sxstrace.exe for detailed diagnosis.
Also, running sxstrace, the same information and error is shown:
ERROR: Cannot resolve reference Microsoft.VC90.DebugCRT,processorArchitecture="x86",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="9.0.21022.8".
ERROR: Activation Context generation failed.
End Activation Context Generation.
Now, after doing some googling and looking round on here, some people recommend installing the Redistributable packages for C++. I've done that for both 2008 and 2010 on both x86 and x64 platforms and still no joy.
Anyone got any ideas? I'm using Visual Studio 2010 and Windows 7 (if that helps?).

The problem is DebugCRT. You've got the debug build of that DLL and it has a dependency on the debug build of the CRT. Which requires an appropriate version of Visual Studio, the only way to get the debug build of the CRT on your machine. Which is Visual Studio 2008, indicated by the 9.0 version number. You have no use for the debug build if you don't also have the source code for this DLL. Nor can you get this DLL deployed and working on your customer's machine.
Get ahead by contacting the owner and asking for the release build so that the redistributable packages can work.

Related

Xamarin iOS - "Native linking failed for .." with Static Library Bind

I'm trying to build an, only iOS, app but I get two errors:
linker command failed with exit code 1 (use -v to see invocation)
Native linking failed for '/Users/{name}/Desktop/{application name}/{application name}/obj/iPhone/Debug/device-builds/iphone8.1-11.1.2/mtouch-cache/arm64/libSomething.dll.dylib'. Please file a bug report at http://bugzilla.xamarin.com
I get these errors with a dll generated from a binding of a (native obj-c) static library with XCode 9.2
I'm using Visual Studio 7.2 with Xamarin.iOS 11.6.1.2
I fixed it.
The error was in the Static Library, was missing two classes.
I've added these two in XCode and then works.

Error when reading audio file?

I have the following function that I am attempting to use to determine the length of an MP3 file:
public static string GetMP3DurationBackup(string Filename)
{
string Duration = null;
WMPLib.WindowsMediaPlayer w = new WMPLib.WindowsMediaPlayer();
WMPLib.IWMPMedia m = w.newMedia(Filename);
if (m != null)
{
Duration = m.durationString;
}
w.close();
return Duration;
}
I have run into an issue where I get the following error:
Retrieving the COM class factory for component with CLSID
{6BF52A52-394A-11D3-B153-00C04F79FAA6} failed due to the following
error: 80040154..
when I call the above function from my web application (call below):
string test = MediaUtil.GetMP3DurationBackup(#"C:\Temp\Audio\bad.mp3");
But when I call it from a console application test harness I created (exact same call as above) it works fine. I have set the project that contains the function to target x86 in the Build properties, but that did not fix the issue.
Does anyone know why this would happen? Suggestions on where to start to debug this?
UPDATED FOR BOUNTY:
Ok, I've tried a number of things but I am still getting this error. Among other things I have tried the steps below which I felt were the most promising, but no dice:
Went into my registry and confirmed that the value at:
HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{6BF52A52-394A-11d3-B153-00C04F79FAA6}\InprocServer32
is refering to C:\WINDOWS\SysWOW64\wmp.dll
Opened command prompt,
navigated to C:\WINDOWS\SysWow64, ran: regsvr32.exe wmp.dll
I have created a console app test harness and I am able to reproduce the error if I run the test project in x64. If I switch it to x86 it works fine.
Does anyone have any idea of why the above would not resolve the issue? Suggestions on where to look next?
You say it doesn't work in x64, but you try to register the 32-bit version of wmp.dll (C:\Windows\SysWow64 contains 32-bit assemblies).
Try to register the x64 version of wmp.dll, which is located in C:\Windows\System32 on a 64-bit platform.
If you don't have this file then there probably is no 64bit Windows Media Player available for your platform. But there is a workaround:
Create a 32-bit console application that takes the mp3 filename as command line argument and outputs the duration to stdout using Console.WriteLine, then in the webapp, you call the console application and capture the output like in this example on MSDN
Give this lib a whirl. Its fast and has no special requirements for software to be installed on the machine.
http://naudio.codeplex.com/

InvalidCastException: Unable to cast COM object of type x to interface type y

I have some C# code written on machine #1 using Visual C# Express 2010. The code is dependent on some COM objects that are registered DLLs. It compiles and runs fine on machine #1. The COM objects are installed from a program called BostonWorkstation, which is installed on machines #1 and #2.
Machine 1:
Code written on.
Visual C# 2010 Express installed.
The "BWS" DLLs are registered.
The program runs just fine.
Has .NET versions: v4.0, v4, v3.5, v3.0, v2.0
32bit
Machine 2:
The "BWS" DLLs are registered.
The program errors with System.InvalidCastException (below).
Visual C# 2010 Express not installed.
Has .NET versions: v4, v3.5, v3.0, v2.0, v1.1
32bit
Machine 3:
The "BWS" DLLs are not registered.
Visual C# 2010 Express installed.
The program errors (as expected) with: System.Runtime.InteropServices.COMException (...): Retrieving the COM class factory for component with CLSID {...} failed due to the following error: 80040154 Class not registered (...)
32bit
When I try to run that program (compiled on machine 1) on machine #2 which also has the same exact registered DLLs, but the program fails to run. I also tried running it on machine #3, which doesn't even have the DLLs and that gets a different error as expected.
The runtime error on machine 2:
1 Unable to load BostonWorkstation, error: System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'BostonWorkStation70.BostonWorkStation'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at TestBWS.LoginForm() in C:\Documents and Settings\user\My Documents\Visual Studio 2010\Projects\project1\TestBWS\CodeFile1.cs:line 67
(The Visual Studio folder reference is just the compile time path)
Checking the registry on machine 1 I found this:
HKEY_CLASSES_ROOT\Interface{8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E}
with a default REG_SZ value of BostonWorkStation
The code:
using BostonWorkStation70; //line 1
...
try
{
bwsLogin = new BostonWorkStation(); //line 67
}
catch (Exception e)
{
Console.WriteLine("1 Unable to load BostonWorkstation, error: " + e);
System.Console.ReadLine(); //pause the debug window
return false;
}
I have tried running ProcMon to compare the machines Process logs, but there are thousands of entries for just this program and I'm not seeing any missing references offhand. I ran a compare, but a successful Process log has 5x as many lines.
From what I understand, running the program on machine 2 has it creating the BWS object as a generic System.__ComObject (instead of BostonWorkStation70.BostonWorkStation) and I don't know why. The DLLs etc are definitely correct on machine 2, otherwise I'd be getting that Class not registered error on runtime.
Why will the compiled program not run on machine 2? All the dependent COMs/DLLs are on it.
if that error message is coming from machine #2, the one you said "Visual C# 2010 Express not installed."
why is it running from the default visual studio projects folder?
C:\Documents and Settings\user\My Documents\Visual Studio 2010\Projects\project1\TestBWS\CodeFile1.cs:line 67
You may have other problems....
Have you registered your COM object class from the library using regsvr32.exe on second machine?
To check that I'd search the registry on first and second machines looking for all possible {8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E} string entries. If search results are different then it's probably the case.
If the BostonWorkstation components are unmanaged - open them up in dependency walker to make sure that all their dependent dlls are present.
Odds are they are unmanaged components and make use of the MSVCRT 10. In which case you will need to install the VS 2010 CRT on machine 2.

Missing External Function in IEFrame.DLL and SHLWAPI.DLL when referencing SharpSVN

Note: This section contains incorrect information - skip to the update below for details. This section left in for historical purposes.
I have a WPF project in Visual Studio 2008 targeting .NET 3.5 that references SharpSVN. When I make a call into SvnClient.Export, I get an exception that Marshal.GetExceptionCode tells me is 0xc06d007e. This seems to be caused by a bad DLL. I have tracked (with the Dependency Walker) that DLL to two culprits: IEFRAME.DLL and SHLWAPI.DLL. They are missing the following export functions:
SHLWAPI.DLL: #270
IEFRAME.DLL: #141, #159, #160
I also initially had a missing DLL that was fixed by adding %ProgramFiles%\Internet Explorer to the path. I do not recall the DLL name.
This is a fresh install of Visual Studio 2008, SharpSVN 1.7002.1998.12257.
I can run the "svn" command that is provided with SharpSVN to export from our repository, so I am leaning toward a solution/project configuration/reference problem.
How do I fix this error?
*Update: I have refined my understanding of this problem. The SHLWAPI and IEFRAME issues appear to be a normal condition - User32.dll has the same behavior. I modified my environment in the following ways:
Environment variable [SVN_SSH] set to TortoisePlink [C:\Program Files\TortoiseSVN\bin\TortoisePlink.exe]
Updated uri target to use current username:
SvnUriTarget uriTarget =
new SvnUriTarget("svn+ssh://" + Environment.UserName + "#myserver/mypath");
With this configuration, I can successfully export when running this code as part of the WPF application. However, I still get errors when running as part of a MSTest project.
System.Runtime.InteropServices.SEHException: External component has thrown an exception.\r\n
at svn_client_export5(Int32* , SByte* , SByte* , svn_opt_revision_t* , svn_opt_revision_t* , Int32 , Int32 , Int32 , svn_depth_t , SByte* , svn_client_ctx_t* , apr_pool_t* )\r\n
at SharpSvn.SvnClient.Export(SvnTarget from, String toPath, SvnExportArgs args, SvnUpdateResult& result) in g:\\dist\\src\\sharpsvn\\commands\\export.cpp:line 100
at SharpSvn.SvnClient.Export(SvnTarget from, String toPath, SvnExportArgs args) in g:\\dist\\src\\sharpsvn\\commands\\export.cpp:line 66
at SimulationManager.Model.SubversionInterface.Export() in C:\\Users\\phines\\Documents\\Projects\\SimulationManager\\SimulationManager\\SimulationManager\\Model\\SubversionInterface.cs:line 43
I suspect this is due to a reliance on the presentation framework, but would still like to know if there is a way to get this working in a GUI-less way.
The errors with IEFrame and SHLWAPI are normal problems that depends reports as errors, but do not acutally cause the error. In my case, the error was caused by using SharpSVN in a non-user interface project. When moved to a WPF project, it worked as expected.
I have changed strategies to call a command shell "svn" application to achieve UI-less automation.

Migrating .net 2.0 console application on win 2008 r2

I have some old 32bit console .NET 2.0 applications that should be migrated to win2008 r2 server.
Note 1: I have one old 32bit application that doesn't load any assembly and is working correctly.
Note 2: When I start one of the applications I get
System.BadImageFormatException Could not load file or assembly error
Note 3:
Each of them are using NLog version 0.9.0.0. When I start these applications on our new server, I get following exception in nLOG.txt:
Error Could not load platform specific extensions: System.NullReferenceException: Object reference not set to an instance of an object.
at NLog.FilterFactory.AddFiltersFromAssembly(Assembly theAssembly, String prefix)
at NLog.Config.XmlLoggingConfiguration.LoadExtensionsFromAssembly(Assembly asm, String prefix)
at NLog.Config.XmlLoggingConfiguration.RegisterPlatformSpecificExtensions(String name)
Can you please help us how to solve this problem? Thank you in advance.
Deveti Putnik
Go to your project settings, and make sure you build your target to 32-bit (x86), and "All Platforms".
The problem you're seeing is a 64-bit executable trying to access a 32-bit DLL. .NET executables by default run as 64-bit on 64-bit OSes.
For point 3:Why not Upgrade NLog to 2.0, For point 2: "System.BadImageFormatException Could not load file or assembly for which assembly" can you find out assemby name causing this problem,it should be there in error message

Categories

Resources