I created a dll with visual C++ 2010 express (win32). Then moved the file inside the bin directory (debug or release) of a visual C# 2010 express (X86) application because I wanted to call a function from this assembly:
public MyForm() { InitializeComponent(); functionCall(); } [DllImport("extern.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void functionCall(); Everything worked properly.
Then I decided to move some of the functionality of this form inside a user control, among them this extern function call.
public MyControl() { InitializeComponent(); functionCall(); } [DllImport("extern.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void functionCall(); Now a problem arises: Visual Studio designer cannot show the Form, despite if the program is executed it runs ok. The error on the form designer is the following:
Could not load file or assembly extern.dll or one of its dependencies. Operation is not supported. (Exception from HRESULT…)
If I call the function from a different thread (for some reason I need it), Visual Studio crashes without any clue. On the web I’ve read about problems displaying winform by the VS designer, but no one seems to answer my exact question, in particular the dll file is not blocked and both the unmanaged .dll and the managed .exe seem to be 32bit.
UPDATE:
It’s incredible how, despite all searches made I missed the only keyword that would have helped me: DesignMode.
I fixed it like this:
public MyControl() { InitializeComponent(); if (!isDesignMode()) functionCall(); } private bool isDesignMode() { return (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLower() == "vcsexpress" || System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLower() == "devenv"); }