Click here to Skip to main content
16,017,907 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a piece of code that worked seamlessly until now. It uses the Windows Management Instrumentation API. I have not changed the code nor anything in my machine (as far as I know), but now when the code is run under the Visual Studio debugger (either 2008 or 2003), it raises an exception, apparently when calling ConnectServer in the code below:
C#
HRESULT hRes;

// Initialize
CoInitialize(0);

// Create the locator
IWbemLocator* pIWbemLocator= NULL;
IWbemServices* pWbemServices= NULL;
hRes= CoCreateInstance(
    CLSID_WbemLocator,
    0,
    CLSCTX_INPROC_SERVER,
    IID_IWbemLocator, (LPVOID *) &pIWbemLocator);
if (hRes != S_OK)
{
    return false; // Could not create the locator
}

// Connect to the server
hRes= pIWbemLocator->ConnectServer(
    L"ROOT\\CIMV2",          // WMI namespace
    NULL,                    // User name
    NULL,                    // User password
    0,                       // Locale
    NULL,                    // Security flags
    0,                       // Authority
    0,                       // Context object
    &pWbemServices           // IWbemServices proxy
    );
if (hRes != S_OK)
{
    return false; // Could not connect to the server
}

If I Ignore the exception, I then get a "0xC0020043: An internal error occurred in RPC." exception.

I am confident that the code is correct as it has been in use for years. And I stress that when executing without the debugger is still works (both Release and Debug builds), so I cannot blame my Windows configuration. But I am stuck when I want to debug.

Can anybody help ?
Posted
Updated 21-Jul-19 21:13pm
v2

here is a sample.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa390423(v=vs.85).aspx[^]

Try to get it running.

The interesting difference ist:

CoInitializeEx(0, COINIT_MULTITHREADED);
 
Share this answer
 
Comments
YvesDaoust 31-May-13 4:13am    
Hi KarstenK,

thank you for taking the time to look at my question.

The good news: this sample program indeed works (provided you remove an extraneous pclsObj->Release() at the end, which causes an access violation exception (!)).

The bad news: the problem is still there.
It does not lie in the initialization function (I already tried three different options, to no avail: CoInitialize(0), CoInitializeEx(0, COINIT_APARTMENTTHREADED) and CoInitializeEx(0, COINIT_MULTITHREADED) - these three work with the sample program.)
I also tried the additional call to CoInitializeSecurity, which is missing in my code. This did change the behavior in some versions of my program but not in others.
So I conclude that the problem is memory-mapping dépendent or due to a race and will be harder to trace as it is somewhat random.
Anyway, your suggestion opened new ways to investigate, thanks.
KarstenK 31-May-13 4:40am    
Sometimes threading and rights are involved. Be sure that the code is running in the main thread and try adminstrative rights.

Good luck ;-)
YvesDaoust 31-May-13 5:38am    
The code is working. I am confident that security rights and threading issues are correctly dealt with (even if these matters are totally and forever opaque to me) as all following operations succeed. The problem is silently ignored in a normal execution, it is just the debugger that tries to challenge me.

Thanks again.
KarstenK 31-May-13 6:00am    
You should contact Microsoft and file a bug report ...
As a workaroud, I have disabled the break into the debugger for this specific exception.

Debug > Exceptions... > Add > Win32 Exceptions > enter "The interface can not be found" and "0x800706B5". Then uncheck the exception.

Seems to work so far, hoping it will not resurface when the least desired...
 
Share this answer
 
v2

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