Using <cfgmgr32.h> in MFC - Error CR_ACCESS_DENIED

82 views Asked by At

I am a really newbie programmer, and currently using <cfgmgr32.h>. I noticed several function I called keep on return CR_ACCESS_DENIED error back to me. I am unable to find the solution else where so I decided to came here.

Here is my code (sorry if it is messy):

DEVINST pDeviceInstance;
CONFIGRET configRet = 0;
configRet = CM_Locate_DevNodeW(&pDeviceInstance, DeviceInstanceIDW, 
    CM_LOCATE_DEVNODE_PHANTOM); // CR_SUCCESS

configRet = CM_Reenumerate_DevNode(pDeviceInstance, CM_REENUMERATE_RETRY_INSTALLATION | 
    CM_REENUMERATE_NORMAL); // CR_ACCESS_DENIED

configRet = CM_Setup_DevNode(pDeviceInstance, CM_SETUP_DEVNODE_READY); // CR_ACCESS_DENIED

PNP_VETO_TYPE vetoed;
WCHAR strVetoed;
configRet = CM_Query_And_Remove_SubTreeW(pDeviceInstance, &vetoed, &strVetoed, 
    MAX_PATH, CM_REMOVE_UI_OK); // CR_ACCESS_DENIED
if (configRet != CR_REMOVE_VETOED)
    configRet = CM_Uninstall_DevNode(pDeviceInstance, 0); // CR_ACCESS_DENIED

I got my DeviceInstanceIDW from calling SetupDiGetDeviceInstanceId, which I am confident it is not having any issue.

I tried CM_Locate_DevNodeW first to get the DEVINST and proceed to my stuff. But right after that, I am stuck.
I noticed in the .h file it comment //NT ONLY, but I didn't managed to figure out what does that means.

My questions are:
a) What does the CR_ACCESS_DENIED actually about (as I cannot find any detailed documentation)
b) How should I solve this and make it work?
c) Is it a bad idea to use this <cfgmgr32.h>? I am actually trying to programmatically unplug and plug an USB COM device. The actual scenario is more complicated, but in simple word, this is what I am attempting to do.

I seeking to know how to make the code works, why it doesn't at first and how is the solution provided solve it. Appreciate any constructive comment. Thanks in advance.

1

There are 1 answers

0
CharonEXE On

So since I was dealing with the device related stuff, and error return is access denied, I thinking it could be the user right that causing me the trouble.

So I go on and find some way to grant my program administrator right. Came across this question and its answer, and it work for me too.

So I guess the access denied thing is really related to the user access right, granting access is able to solve that. (While I still dunno why the error code in the header file comment with //NT ONLY)

Still looking forward to any constructive feedback on using <cfgmgr32.h> though as I am still unaware if any practice required to use this.