2

Native C++ code:

int main()
{
   auto hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); 

   COSERVERINFO server = {0, L"Win7x64", nullptr, 0};

   MULTI_QI qi = {&IID_XXXService, nullptr, 0};

   hr = CoCreateInstanceEx(CLSID_XXXService, nullptr, CLSCTX_REMOTE_SERVER, &server, 1, &qi);
}

Succeeds, with any threading model. With or without a call to CoInitializeSecurity. But when trying to instantiate same object from a .NET/C#, it always fails with 0x80070005:

static void Main(string[] args)
{
    var machineName = "Win7x64";
    try
    {
        Type dcomType = Type.GetTypeFromCLSID(typeof(XXXService).GUID, machineName, false);
        object dcomObj = Activator.CreateInstance(dcomType);               
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

I have tried the latter with CoInitializeSecurity (DllImported), with MTA and STA thread attribute, with UAC elevated process, but it always fails. If machineName is for current machine, it works (yes, current development machine has same COM interface, and class registered).

EDIT: In order to successfully call CoInitializeSecurity, the setting 'Enable the Visual Studio hosting process' must be disabled. Still then, CreateInstance fails.

With native code, it always works. With managed code, it never works. What could be done?

EDIT2: COM security on remote machine:

  • Access Permissions (Limits): Everyone, as well as ANONYMOUS LOGON (among other common) - Full access.
  • Access Permissions (Default): SELF, SYSTEM, ANONYMOUS LOGON - Full access to all.
  • Launch & Activation (Limits): Everyone, DCOM Users, ANONYMOUS LOGON (among other common) - Full access.
  • Launch & Activation Permissions (Default): SYSTEM, NETWORK, ANONYMOUS LOGON, INTERACTIVE - Full access to all.
Ajay
  • 18,086
  • 12
  • 59
  • 105
  • Try passing `true` as the last arg to `Type.GetTypeFromCLSID(typeof(XXXService).GUID, machineName, false)`. Does it throw? – noseratio Aug 18 '14 at 10:20
  • Tried both, doesn't make any difference. Throw or not throw, it should work. It is anyway failing. – Ajay Aug 18 '14 at 10:22
  • Do *not* expect anybody to write you the missing manual on DCOM security. Post your code and carefully describe the account you use and its rights on the remote machine. – Hans Passant Aug 18 '14 at 12:05
  • @HansPassant, Updated the security. Question is still the same - why does it work with native code and not from managed world. It has nothing to do with security (at server side). The code posted is complete. – Ajay Aug 18 '14 at 12:28
  • The complete lack of error checking and the **required** interface pointer marshaling, always necessary when you use COINIT_MULTITHREADED, does not inspire me to think that your native code works either. The kind of oversight that the CLR never makes. – Hans Passant Aug 18 '14 at 12:34
  • Function call is secondary. Typecasting and marshalling are secondary. Posted minimal understandable code, not a Code-Complete theory. I have posted only after failed attempts, and failed online results. – Ajay Aug 18 '14 at 12:39
  • @Ajay did you ever get past this? I'm having the same problem. – Mark Richman May 02 '16 at 17:34

0 Answers0