본문 바로가기
코드/c/c++

OleRegGetUserType 사용 후 문자열 해제

by WeZZ 2015. 12. 15.

 

 

BSTR을 이용하여 OleRegGetUserType(x,x,bstr) 을 호출 후 꼭 CoTaskMemFree를 사용하자.

 

실제 msdn에는 언급이 없지만 rectos의 OleRegGetUserType의 원형을 보면

 

HRESULT WINAPI OleRegGetUserType(
00651     REFCLSID clsid,
00652     DWORD dwFormOfType,
00653     LPOLESTR* pszUserType)
00654 {
00655   WCHAR   keyName[60];
00656   DWORD   dwKeyType;
00657   DWORD   cbData;
00658   HKEY    clsidKey;
00659   LONG    hres;
00660 
00661   /*
00662    * Initialize the out parameter.
00663    */
00664   *pszUserType = NULL;
00665 
00666   /*
00667    * Build the key name we're looking for
00668    */
00669   sprintfW( keyName, clsidfmtW,
00670             clsid->Data1, clsid->Data2, clsid->Data3,
00671             clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
00672             clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
00673 
00674   TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwFormOfType, pszUserType);
00675 
00676   /*
00677    * Open the class id Key
00678    */
00679   hres = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
00680   if (hres != ERROR_SUCCESS)
00681     return REGDB_E_CLASSNOTREG;
00682 
00683   /*
00684    * Retrieve the size of the name string.
00685    */
00686   cbData = 0;
00687 
00688   hres = RegQueryValueExW(clsidKey,
00689               emptyW,
00690               NULL,
00691               &dwKeyType,
00692               NULL,
00693               &cbData);
00694 
00695   if (hres!=ERROR_SUCCESS)
00696   {
00697     RegCloseKey(clsidKey);
00698     return REGDB_E_READREGDB;
00699   }
00700 
00701   /*
00702    * Allocate a buffer for the registry value.
00703    */
00704   *pszUserType = CoTaskMemAlloc(cbData);
00705 
00706   if (*pszUserType==NULL)
00707   {
00708     RegCloseKey(clsidKey);
00709     return E_OUTOFMEMORY;
00710   }
00711 
00712   hres = RegQueryValueExW(clsidKey,
00713               emptyW,
00714               NULL,
00715               &dwKeyType,
00716               (LPBYTE) *pszUserType,
00717               &cbData);
00718 
00719   RegCloseKey(clsidKey);
00720 
00721   if (hres != ERROR_SUCCESS)
00722   {
00723     CoTaskMemFree(*pszUserType);
00724     *pszUserType = NULL;
00725 
00726     return REGDB_E_READREGDB;
00727   }
00728 
00729   return S_OK;
00730 }
 

CoTaskMemAlloc을 이용하여 문자열 포인터를 할당하고 있다.

주의해서 사용하시길...