描述:
我想把函数暴露出来,做成了api. 创建的是win32 DLL工程,编译环境vc6.0:
#define DllExport __declspec( dllexport )
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
// Initial COM
case DLL_PROCESS_ATTACH:
break;
// Detach COM
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
bool DllExport __stdcall CreateVirtualDirection(LPCTSTR lpszVirtualDirName, LPCTSTR lpszDiskPath)
{
IADsContainer* iContainer = NULL;
IADs* iAds = NULL;
HRESULT hr = S_OK;
// Get Web Server
hr = ADsGetObject(L"IIS://localhost/w3svc", IID_IADs, (void**)&iContainer);
if(FAILED(hr))
{
iContainer->Release();
return false;
}
// Get default site
iContainer->GetObject(_bstr_t("IIsWebVirtualDir"),_bstr_t("1"),(IDispatch**)&iAds);
// Get Access to the virtual directory
if(iAds->QueryInterface(IID_IADsContainer,(void**)&iContainer) == S_OK)
{
if(iContainer->Create(_bstr_t("IIsWebVirtualDir"),_bstr_t(lpszVirtualDirName),(IDispatch**)&iAds)==S_OK)
{
// Set virtual directory's property
iAds->Put(_bstr_t("AccessRead"),_variant_t("True"));
iAds->Put(_bstr_t("AccessWrite"),_variant_t("True"));
iAds->Put(_bstr_t("Path"),_variant_t(lpszDiskPath));
iAds->SetInfo();
iAds->Release();
iAds->Release();
iContainer->Release();
iContainer->Release();
return true;
}
else
{
iAds->Release();
iAds->Release();
iContainer->Release();
iContainer->Release();
return false;
}
}
else
{
iAds->Release();
iContainer->Release();
}
return true;
}
现在的问题是:
1)要在DLL_PROCESS_ATTACH中初始化com以及DLL_PROCESS_DETACH释放com怎么写?本人没有学过com,所以...很急
2)编译的时候有这样的问题IID_IADsContainer,IID_IADs没有定义,这个应该是接口的name把,怎么处理?
谢谢了
解决方案1:
msdn上的一句话
Because there is no way to control the order in which in-process servers are loaded or unloaded, do not call CoInitialize, CoInitializeEx, or CoUninitialize from the DllMain function.
不能在dllmain中调用com的初始化和卸载。
2这个得看代码才行。