Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Recentrly I came across a very strange problem.
I wrote a WPF program which interacts with a DLL from Infineon.
Everything worked as expected, but at some point, I got a stack overflow exception from nowhere.

I started nailing down the issue (you see the exception comes out of WindowsBase.dll), and saw the issue only happens if I load a specific DLL file (via kernel32 LoadLibrary function).

The following piece of code, for example, will cause the exception to happen (although the LoadLibrary function returns with no error, and I can then use the handle):
C#
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        IntPtr handle = LoadLibrary("dwdiow.dll");
    }
}


In this example, the button1_Click() is exited and after some time the excption comes.

If I do the very same thing with a windows form or console projects (instead of WPF), the error does not show up.

Please help!
Posted
Updated 1-Sep-11 1:20am
v2

1 solution

Try using LoadLibraryEx instead:

C#
[DllImport("kernel32.dll", CharSet=CharSet::Auto, EntryPoint="LoadLibraryEx")]
private static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);

System.IntPtr moduleHandle = LoadLibraryEx(libPath, IntPtr.Zero, 0);


You need to give us more info on the exception, specifically what opject/method it's happening on. In fact, you might also want to ask this question on the MSDN WPF forum.
 
Share this answer
 
v2
Comments
Abhinav S 1-Sep-11 7:27am    
5. Proposed as answer if it works for the OP.
tiran_kaskas 1-Sep-11 8:41am    
Thanks John.
I tried this, no change, still same thing.

There is nothing in the exception except saying:
"An unhandled exception of type 'System.StackOverflowException' occurred in WindowsBase.dll"

Something happens behind the scene, I mean the fucntion has returned, and context went back to the form, but the process starts growing in terms of memory (task manager) until it gives the exception.
kevinchan9527 14-May-12 23:15pm    
i met this problem too. any guy know how to solve.
hannes.ambichler 27-Nov-12 23:40pm    
I am having the ecaxt same issue with a little used product from Identix/L1/Sagem, BioEngine6. After calling some of the unmanaged functions in a DLL of that product, the call returns successfully, and a little later a StackOverflowException occurs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900