Search code examples
c#c++formscredential-providersunmanagedexports

Form not displaying in credential provider


I have a function in C# that displays a form. I have exposed the function using Unmanaged Exports and calling it from C++ in credential provider sample on a command link. The form does not display (nothing happens). However, when I call the same C# form using C++ console application, the form displays without any issue. What could be the difference that C++ console application is loading it but C++ credential provider code is not loading it?

C++ Code:

using CSharpForm = void(__stdcall *)(wchar_t* message);
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE mod = LoadLibraryA("CSharp.dll");
CSharpForm form = reinterpret_cast<CSharpForm>(GetProcAddress(mod, "form1"));
form(L"This is a c# form");
getchar();
return 0;
}

C# Code:

[DllExport(ExportName = "form1", CallingConvention = CallingConvention.StdCall)]
public static void showForm([MarshalAs(UnmanagedType.LPWStr)]string message)
{
    Form_Test form = new Form_Test();
    form.Text = message;
    form.ShowDialog();
}

Solution

  • Try this out:

    Call ICredentialProviderCredentialEvents::OnCreatingWindow method

    HRESULT OnCreatingWindow(
        HWND *phwndOwner
    );
    

    to get window handle, pass additional parameter into your library and use overloaded method ShowDialog.

    public DialogResult ShowDialog(
        IWin32Window owner
    );
    

    You may pass HWND out paramemter valie as IntPtr and convert it using public static System.Windows.Forms.NativeWindow FromHandle(IntPtr handle).