views:

50

answers:

3

It seems that calling a P-Invoke while the App is not completed loaded make it spew the errors of the related functions.

For example at the moment I'm trying to load at startup various DLL through LoadLibrary and test if they have exported a particular function.

If I do that while the app completed its load procedure no error are given (just the result is 0 and I can check them through GetLastError), If I do it at startup time (for example the Load event of the form) it spews the errors (for example a missing module etc..).

There is a way to mute those errors?

Note that these are not exceptions, but system messageboxes and try...catch does not work here.

Thanks.

A: 

If you want to detect/test is various DLL's have particular entrypoints, use the GetProcAddress() API. If GetProcAddress() return's 0 for particular entrypoint, then that entry point does not exist in that DLL.

Ants
Coff Coff, I already know this. The question is another...I already do this. But if the LoadLibrary fails and is called from startup a messagebox appear. :P
feal87
Oh, I see. My mistake. Since your question's titled had "P/Invoke errors", I assumed that the errors were from P/Invokes to the actual DLL. Based on your comment above, what you actually were seeing were LoadLibrary() failures.
Ants
+1  A: 

It doesn't make a lot of sense that you would see message boxes from calling LoadLibrary(). See if P/Invoking SetErrorMode() with SEM_NOOPENFILEERRORBOX solves your problem. Using the Shown instead of a Load event is worth a try too.

Hans Passant
Thank you, fixes the problem. :)
feal87
Just as a note, I used all the values in the SetErrorMode. Only the SEM_NOOPENFILEERRORBOX does not suffice. :P (i reactivate them when i've completed the DLL scan)
feal87
A: 

I dont have the code to try but I have a feeling that you get a message box because an exception is thrown in a separate thread and not being handled by your try and catch block. Can you try wire up these two events at the beginning of your application and try and see if the exception is being caught by your event handler here.

    Application.ThreadException
    AppDomain.CurrentDomain.UnhandledException
Fadrian Sudaman