描述:
我是用ATL向导产生的服务器,用的是进程外exe选项,里面加了一个接口,是ICRemoteTime,一切正常。
又作了一个客户,代码如下:
// ClientTime.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#import "E:\Program Files\Microsoft Visual Studio\MyProjects\TimeServer\TimeServer.tlb" no_namespace named_guids
int main(int argc, char* argv[])
{
HRESULT hr;
CoInitialize(NULL);
IUnknown* pIUnknown;
ICRemoteTime* pIRetime;
hr = CoCreateInstance(
CLSID_CRemoteTime,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IUnknown,
(void**)&pIUnknown);
if(FAILED(hr)) {
printf("error!\n");
return 0;
}
hr = pIUnknown->QueryInterface(
IID_ICRemoteTime,
(void**)&pIRetime);
if(FAILED(hr)) {
printf("hehe error!!\n");
}
return 0;
}
编译运行后可以返回IUnknown,但就是返不回ICRemoteTime,说是接口不存在。
怎么回事呢?
解决方案1:
http://www.vchelp.net/vchelp/archive.asp?type_id=29&class_id=1&cata_id=3&article_id=1083&search_term=
VC下的DCOM服务器与客户端,你看一下这个,应该有帮助的,里面有源代码
#define _WIN32_WINNT 0x0400
解决方案3: 错了,你是DCOM啊,DCOM不能用CoCreateInstance,应该用CoCreateInstanceEx,你这样当然就找不到接口拉
HRESULT hr;
hr = CoInitialize(NULL);
ASSERT(SUCCEEDED(hr));
MULTI_QI qi;
qi.pIID = &IID_ICRemoteTime;
qi.hr = NULL;
qi.pItf = NULL;
COAUTHIDENTITY authidentity;
authidentity.User = L"administrator"; ////你机子的帐号
authidentity.UserLength = wcslen(authidentity.User);
authidentity.Domain = NULL;
authidentity.DomainLength = 0;
authidentity.Password = L""; ////密码
authidentity.PasswordLength = wcslen(authidentity.User);
authidentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
COAUTHINFO authinfo = {-1, 0, 0, RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE, &authidentity, 0};
COSERVERINFO servInf;
servInf.dwReserved1 = NULL;
servInf.dwReserved2 = NULL;
// servInf.pAuthInfo = NULL;
servInf.pAuthInfo = &authinfo;
USES_CONVERSION;
servInf.pwszName = L"127.0.0.1";
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_ACCESS_CONTROL, NULL);
ASSERT(SUCCEEDED(hr));
hr = CoCreateInstanceEx(CLSID_CRemoteTime, NULL, CLSCTX_REMOTE_SERVER, &servInf, 1, &qi);
if (FAILED(hr))
{
TRACE(_T("CoCreateInstanceEx failed"));
return false;;
}
if (FAILED(qi.hr))
{
TRACE(_T("Failed to connect to server"));
return false;;
}
//通过IUnkonwn指针去查询接口指针,返回IAccount指针
hr = pUnknown->QueryInterface(IID_ICRemoteTime,(void**)&pIRetime)
注册一下com,应该就行拉,
project->setting 中Post-build step
nmake -fXXXps.mk xxx是你的项目名字
然后xxx -regserver
这样应该就行拉,如果不行的话看看你的服务器程序有没问题