服务器端没有什么不同的
客户的例子
// client.cpp : Defines the entry point for the console application.
//
#include "atlbase.h"
#include "..\dbsvr\dbsvr.h"
#import "H:\program files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
const IID IID_IDbAccess = {0xB5A79775,0x7878,0x4984,{0xA0,0x97,0xEC,0x57,0xDA,0xFC,0xB0,0x1E}};
const CLSID CLSID_DbAccess = {0x1C0B24CC,0x47EF,0x4B7B,{0xA0,0xB7,0xBA,0x33,0x6F,0x25,0x83,0x86}};
int main(int argc, char* argv[])
{
USES_CONVERSION;
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if(FAILED(hr))
return -1;
COAUTHIDENTITY AuthIdentity;
AuthIdentity.User=L"wyzegg";
    AuthIdentity.UserLength=wcslen(L"wyzegg");
    AuthIdentity.Password=L"password";
    AuthIdentity.PasswordLength=wcslen(L"password");
    AuthIdentity.Domain=L"";
AuthIdentity.DomainLength=wcslen(L"");
    AuthIdentity.Flags=SEC_WINNT_AUTH_IDENTITY_UNICODE;
COAUTHINFO AuthInfo;
AuthInfo.dwAuthnSvc=/*RPC_C_AUTHN_GSS_KERBEROS;//*/RPC_C_AUTHN_WINNT;
    AuthInfo.dwAuthzSvc=RPC_C_AUTHZ_NONE/*RPC_C_AUTHZ_NONE*/;
AuthInfo.pwszServerPrincName=NULL;
AuthInfo.dwAuthnLevel=/*RPC_C_AUTHN_LEVEL_CONNECT;//*/RPC_C_AUTHN_LEVEL_DEFAULT;
AuthInfo.dwImpersonationLevel=RPC_C_IMP_LEVEL_IMPERSONATE;
    AuthInfo.pAuthIdentityData=&AuthIdentity;
AuthInfo.dwCapabilities=EOAC_NONE;
COSERVERINFO ServerInfo={0,L"139.1.8.5",&AuthInfo,0};
MULTI_QI qi[2]={{&IID_IUnknown,NULL,0},{&IID_IDbAccess,NULL,0}};
IDbAccess *pDbAccess;
hr=CoCreateInstanceEx(CLSID_DbAccess ,NULL,CLSCTX_REMOTE_SERVER,&ServerInfo,2,(MULTI_QI*)&qi);
switch(hr)
{
case S_OK:
printf("Indicates success.\n");
break;
case REGDB_E_CLASSNOTREG :
printf("A specified class is not registered in the registration database. Also can indicate that the type of server you requested in the CLSCTX enumeration is not registered or the values for the server types in the registry are corrupt.\n");
CoUninitialize();
return -1;
case CLASS_E_NOAGGREGATION :
printf("This class cannot be created as part of an aggregate.\n");
CoUninitialize();
return -1;
    case CO_S_NOTALLINTERFACES :
      printf("At least one, but not all of the interfaces requested in the pResults array were successfully retrieved. The hr field of each of the MULTI_QI structures in pResults indicates with S_OK or E_NOINTERFACE whether the specific interface was returned. \n");
CoUninitialize();
return -1;
case E_NOINTERFACE:
printf("None of the interfaces requested in the pResults array were successfully retrieved.\n");
CoUninitialize();
return -1;
default:
    printf("cannot create object\n");
CoUninitialize();
return -1;
}
pDbAccess=(IDbAccess *)qi[1].pItf;
LPDISPATCH lpRs; 
hr=pDbAccess->SetDbFilePath(SysAllocString(L"E:\\com+\\record\\user.mdb"));
if(hr!=0)
{
pDbAccess->Release();
    CoUninitialize();
return 0;
    }
    hr=pDbAccess->Connect();
if(hr!=0)
{
pDbAccess->Release();
    CoUninitialize();
return 0;
}
hr=pDbAccess->ExcuteQuery(SysAllocString(L"select * from user"),&lpRs);  
if(hr!=0)
{
pDbAccess->Release();
    CoUninitialize();
return 0;
}
    _RecordsetPtr pUserSet(lpRs);
FieldsPtr      pFieldsPtr;
FieldPtr       pFieldPtr;
char name[255];
long nRow=0;
while (!pUserSet->EndOfFile)
{
nRow++;
pFieldsPtr = pUserSet->Fields;
pFieldPtr   = pFieldsPtr->Item["user_name"];
   if( pFieldPtr->Value.vt==VT_NULL ) 
strcpy(name,"");
else 
strcpy(name,_bstr_t(pFieldPtr->Value));
printf("row%d's user_name is %s\n",nRow,name);
       pUserSet->MoveNext();
}
pUserSet->Close();
pDbAccess->DisConnect();
pDbAccess->Release();
    CoUninitialize();
return 0;
}

解决方案 »

  1.   

    蛋:你好!谢谢你给我的回答。IDbAccess是不是COM里的接口,在客户端能直接用吗(IDbAccess *pDbAccess)?我拿我的com里的接口在客户里直接用的时候,就会编译错误。另外,如何生成代理DLL和存根DLL,如何分发,谢谢,
    成功之后,一定给分。
      

  2.   

    当然需要包含由MIDL编译的头文件
    如何生成代理DLL和存根DLL,如何分发
    在本栏已经多次问到,你可以搜索一下
      

  3.   

    wyzegg(蛋) :谢谢,想必你一定是高手,真想你能拉我一把,能告诉我你的E_mail吗?我的是[email protected]。给分了。
      

  4.   

    过奖啦,我也是刚学习COM的
    DAOBADAN @163,NET