EDN Admin
Well-known member
I am trying to port my Windows Mobile C++ application to Windows 7 (not Phone), but I cant get Component Object Model working - I made a sample application to test COM behavior on Windows 7, but although the DllRegisterServer succeeds I cannot see the entries
in HKEY_CLASSES_ROOTCLSID. I think it might be caused by Windows 7 UAC and Registry Virtualization (but running it as Administrator made no difference). RegSetValueEx returns 0 (ERROR_SUCCESS) as expected, so there is no error until I call CoCreateInstance,
which results in E_NOINTERFACE.
This is my DllRegisterServer function:
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // Purpose: Adds needed values to the system registry
<span style="color:Green; // Parameters: -
<span style="color:Green; // Return value: In case of failure returns E_FAIL, otherwise S_OK
STDAPI DllRegisterServer(<span style="color:Blue; void)
{
HRESULT bRes = S_OK;
<span style="color:Blue; wchar_t path[MAX_PATH];
<span style="color:Blue; wchar_t clsid[39];
<span style="color:Blue; wchar_t regclsid[MAX_PATH];
OLECHAR* olechar = NULL;
HKEY hKey = NULL;
LRESULT lRes = NULL;
DWORD dwDisp = 0;
*path = Lx0;
*clsid = Lx0;
*regclsid = Lx0;
<span style="color:Green; // convert CLSID to string
StringFromCLSID(CLSID_COMTest, &olechar);
wcscpy_s(clsid, 39, olechar);
swprintf(regclsid, MAX_PATH, L<span style="color:#A31515; "CLSID\%s", clsid);
CoTaskMemFree(olechar);
olechar = NULL;
<span style="color:Green; // HKCR,CLSID{dde5296f-ddfc-4068-b8c3-35c8354f6565},Default,0x00000000,"Test"
lRes = RegCreateKeyExW(HKEY_CLASSES_ROOT, regclsid, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwDisp);
<span style="color:Blue; if (lRes == ERROR_SUCCESS)
{
<span style="color:Green; // save value to "default" key
lRes = RegSetValueExW(hKey, NULL, 0, REG_SZ, (CONST BYTE*)L<span style="color:#A31515; "Test", (wcslen(L<span style="color:#A31515; "Test") + 1) * <span style="color:Blue; sizeof(<span style="color:Blue; wchar_t));
<span style="color:Blue; if (lRes != ERROR_SUCCESS)
bRes = E_FAIL;
::RegCloseKey(hKey);
hKey = NULL;
}
<span style="color:Blue; else
{
bRes = E_FAIL;
<span style="color:Blue; return bRes;
}
swprintf(regclsid, MAX_PATH, L<span style="color:#A31515; "CLSID\%s\InprocServer32", clsid);
<span style="color:Green; // HKCR,CLSID{dde5296f-ddfc-4068-b8c3-35c8354f6565}InprocServer32,Default,0x00000000,"path"
lRes = RegCreateKeyExW(HKEY_CLASSES_ROOT, regclsid, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwDisp);
<span style="color:Blue; if (lRes == ERROR_SUCCESS)
{
<span style="color:Green; // get current path
UINT length = MAX_PATH;
GetDllPath(g_hInst, path, &length);
lRes = RegSetValueExW(hKey, NULL, 0, REG_SZ, (CONST BYTE*)path, wcslen(path) * <span style="color:Blue; sizeof(<span style="color:Blue; wchar_t));
<span style="color:Blue; if (lRes != ERROR_SUCCESS)
bRes = E_FAIL;
::RegCloseKey(hKey);
hKey = NULL;
}
<span style="color:Blue; else
{
bRes = E_FAIL;
}
<span style="color:Blue; return bRes;
}
[/code]
On Windows Mobile, an equivalent of this code worked fine, but I dont have much experience in programming for "normal" Windows. Here is a link to the complete source code of this example:
http://hotfile.com/dl/125008064/088449c/COMTest.7z.html http://hotfile.com/dl/125008064/088449c/COMTest.7z.html
Thanks in advance,
Lukas V.
View the full article
in HKEY_CLASSES_ROOTCLSID. I think it might be caused by Windows 7 UAC and Registry Virtualization (but running it as Administrator made no difference). RegSetValueEx returns 0 (ERROR_SUCCESS) as expected, so there is no error until I call CoCreateInstance,
which results in E_NOINTERFACE.
This is my DllRegisterServer function:
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // Purpose: Adds needed values to the system registry
<span style="color:Green; // Parameters: -
<span style="color:Green; // Return value: In case of failure returns E_FAIL, otherwise S_OK
STDAPI DllRegisterServer(<span style="color:Blue; void)
{
HRESULT bRes = S_OK;
<span style="color:Blue; wchar_t path[MAX_PATH];
<span style="color:Blue; wchar_t clsid[39];
<span style="color:Blue; wchar_t regclsid[MAX_PATH];
OLECHAR* olechar = NULL;
HKEY hKey = NULL;
LRESULT lRes = NULL;
DWORD dwDisp = 0;
*path = Lx0;
*clsid = Lx0;
*regclsid = Lx0;
<span style="color:Green; // convert CLSID to string
StringFromCLSID(CLSID_COMTest, &olechar);
wcscpy_s(clsid, 39, olechar);
swprintf(regclsid, MAX_PATH, L<span style="color:#A31515; "CLSID\%s", clsid);
CoTaskMemFree(olechar);
olechar = NULL;
<span style="color:Green; // HKCR,CLSID{dde5296f-ddfc-4068-b8c3-35c8354f6565},Default,0x00000000,"Test"
lRes = RegCreateKeyExW(HKEY_CLASSES_ROOT, regclsid, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwDisp);
<span style="color:Blue; if (lRes == ERROR_SUCCESS)
{
<span style="color:Green; // save value to "default" key
lRes = RegSetValueExW(hKey, NULL, 0, REG_SZ, (CONST BYTE*)L<span style="color:#A31515; "Test", (wcslen(L<span style="color:#A31515; "Test") + 1) * <span style="color:Blue; sizeof(<span style="color:Blue; wchar_t));
<span style="color:Blue; if (lRes != ERROR_SUCCESS)
bRes = E_FAIL;
::RegCloseKey(hKey);
hKey = NULL;
}
<span style="color:Blue; else
{
bRes = E_FAIL;
<span style="color:Blue; return bRes;
}
swprintf(regclsid, MAX_PATH, L<span style="color:#A31515; "CLSID\%s\InprocServer32", clsid);
<span style="color:Green; // HKCR,CLSID{dde5296f-ddfc-4068-b8c3-35c8354f6565}InprocServer32,Default,0x00000000,"path"
lRes = RegCreateKeyExW(HKEY_CLASSES_ROOT, regclsid, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwDisp);
<span style="color:Blue; if (lRes == ERROR_SUCCESS)
{
<span style="color:Green; // get current path
UINT length = MAX_PATH;
GetDllPath(g_hInst, path, &length);
lRes = RegSetValueExW(hKey, NULL, 0, REG_SZ, (CONST BYTE*)path, wcslen(path) * <span style="color:Blue; sizeof(<span style="color:Blue; wchar_t));
<span style="color:Blue; if (lRes != ERROR_SUCCESS)
bRes = E_FAIL;
::RegCloseKey(hKey);
hKey = NULL;
}
<span style="color:Blue; else
{
bRes = E_FAIL;
}
<span style="color:Blue; return bRes;
}
[/code]
On Windows Mobile, an equivalent of this code worked fine, but I dont have much experience in programming for "normal" Windows. Here is a link to the complete source code of this example:
http://hotfile.com/dl/125008064/088449c/COMTest.7z.html http://hotfile.com/dl/125008064/088449c/COMTest.7z.html
Thanks in advance,
Lukas V.
View the full article