0

Last week I stumbled upon a problem and I'm not sure how to solve it. I already spent a whole day to try to solve it alone.

I have an unmanaged DLL that worked fine in my C# code on a Windows XP (32 bit) System. When I try to run this C# code on a newer System (for example Windows 7 (64 bit) it doesn't work anymore.

This is the code

[DllImport("MyTest.dll")]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern String DoSomething();

As I said this works fine on my Windows XP System but doesn't work on my Windows 7 System. When I try to run this code on my Windows 7 System I get the following Exception (translated into English)

The DLL "MyTest.dll": Recurrence too deep, Stackoverflow. (Exception from HRESULT: 0x800703E9) can not be loaded.

I'm guessing the problem is because of this:

Load 32bit DLL library in 64bit application

How to call win32 dll in windows 7

I'm just not sure about it because when I search for my exception in combination with DLLImport I can't find anything.

If this is really the problem what would be the best solution?

Community
  • 1
  • 1
Shamshiel
  • 2,051
  • 3
  • 31
  • 50

2 Answers2

0

You could check:

  1. unmanaged dll dependencies with dependency walker. There might be missing c++ redistributable. Look for MSVCR{xxx}.dll where {xxx} specifies version of the redistributable.
  2. compilation settings of managed application that in the end loads unmanaged dll (Any CPU, x86 or x64). Might be that managed application is set to Any CPU but unmanaged application is x86 and therefore load of unmanaged dll fails.
  3. installed .NET frameworks on windows xp and windows 7. The problem might be different patches applied on .NET frameworks but this is the least likely to be your problem.
pepo
  • 8,644
  • 2
  • 27
  • 42
  • 1. I used the dependency walker and there is no dependency to a MSVCR{xxx}.dll at all. 2. I can only compile it as AnyCPU but I tried to run it on a Windows Server 2008 (32 bit) System and it didn't work there, too. – Shamshiel Mar 11 '14 at 13:29
  • Could you paste C definition of the method you are trying to marshal? – pepo Mar 11 '14 at 14:17
  • Sadly I can't do that because I don't have access to the sources of the DLL. The "marshaling" worked fine on Windows XP and on a Windows Server 2003, so I think the problem isn't the C# code or the C code. I think it has something do to with the change to a newer OS. I tried running the application on a Windows 7 32-bit OS and it still didn't work. Maybe it has something do to with the change of the kernel from 5.x to 6.x? – Shamshiel Mar 17 '14 at 06:35
0

I solved the problem like this:

[DllImport("MyTest.dll", CharSet = CharSet.Ansi)]
private static extern IntPtr DoSomething();
public static string Do_Something()
{
   IntPtr tempPointer = DoSomething();
   string tempSomething = Marshal.PtrToStringAnsi(tempPointer);
   return tempSomething ;
}

The problem had to do with a corrupted heap. The handling of corrupted heaps is different in newer version of Windows and because of that my application crashed. It can be solved through changing the C#-Code or the C++-Code.

Detailed information about the problem can be found here: http://blogs.msdn.com/b/asiatech/archive/2009/12/24/net-application-may-crash-on-windows-2008-when-calling-function-from-native-c-dll.aspx

Shamshiel
  • 2,051
  • 3
  • 31
  • 50