3

Problem in short: How to call a function from DLL A.dll using p/invoke when A.dll depends on another SXS-lib (in my case MSVCR90.DLL)?

I'd like to call a function within a DLL using pinvoke. pinvoke itself works fine for other libs. Calling the function in the DLL from unmanaged C++ works fine, too.

The problem is that the DLL has a reference to MSVCR90.DLL which resides in some SXS folders.

Using LoadLibrary in C++ the library can be used as mentioned. Using C# I don't know how to get the library loaded. I always get an error that MSVCR90.DLL was missing on the computer.

This is what loading the library looks like:

[DllImport("C:\\work\\dllhell\\sample\\sample.dll", 
    EntryPoint = "sample", CallingConvention = CallingConvention.Cdecl)]
public static extern int sample();

When calling the function sample, I only get the following error: HRESULT: 0x8007007E saying that the library would not have been found. Actually, the library exists in various versions in SXS directories.

I tried using Dependency Walker (depends) but it also has not been able to locate the right version of the library, so far.

There is also a manifest shipped with the library containing the following entry:

The following statement is included within the manifest:

<assemblyIdentity type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8'
    processorArchi    tecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />

The version requested exists on my machine, I'm using VS 2010 which ships newer versions of that library but that shouldn't be a problem, I guess.

I found a lot of threads discussing similar problems. I know, I have a dependency issue but I could nowhere find a solution. I know my missing dependency, I have a manifest but should that do it for C#, too? Not only for unmanaged C++?

user1034081
  • 618
  • 5
  • 21
  • Look in the Windows Application event log for the exact details. – Hans Passant Nov 07 '11 at 16:48
  • Thanks for your comment, unfortunately I could not find any new details there. It just shows the same error whether I try to load a non-existing DLL or that DLL that has dependencies on MSVCR90.DLL.The error says there has been a DllNotFoundException at the position I try to call a function from the DLL. – user1034081 Nov 07 '11 at 17:03
  • I can't see it from here. Pay attention to the version number. Get that DLL deployed. – Hans Passant Nov 07 '11 at 17:04
  • There has been a typo in my posting (MSVCR80 instead of MSVCR90) - sorry for that. The exact version should be ok. The version needed is deployed and is loaded when I'm using the library from unmanaged C++. So the library and its dependencies are deployed properly, I think. Should that do it for C#, too? Or is there anything special to consider in opposite to unmanaged C++? – user1034081 Nov 07 '11 at 17:16
  • 2
    You need to include the necessary msvcrt manfest in you app's manifest, or use the activation context API to get the necessary manifest in place when you call LoadLibrary. – David Heffernan Nov 07 '11 at 17:34
  • Using C++ the DLL works just fine, I'm searching for a solution for C# using pinvoke. – user1034081 Nov 07 '11 at 18:18
  • The information here might help you out [Dynamic Link Library Search Order](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx) – user957902 Nov 08 '11 at 23:36

1 Answers1

2

In case anyone searches for that issue: of course, David Heffernan has solved the issue, the dependency has to be added to the application's manifest, i.e. the file app.manifest, create it as new manifest in your projekt if there is no such file, yet.

The following entry is sufficient:

  <dependency>
    <dependentAssembly>
      <assemblyIdentity type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />
    </dependentAssembly>
  </dependency>

This entry goes directly to the root node assembly in the XML file.

Thanks to David Heffernan, forget about my last comment, it has been the solution to my question.

Yahia
  • 69,653
  • 9
  • 115
  • 144
user1034081
  • 618
  • 5
  • 21