client with COAUTHIDENTITY.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // client.cpp
  2. #define _WIN32_DCOM
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include "Component CoInitializeSecuritycomponent.h"
  6. void main()
  7. {
  8. cout << "Client: Calling CoInitialize()" << endl;
  9. CoInitialize(NULL);
  10. IUnknown* pUnknown;
  11. ISum* pSum;
  12. cout << "Client: Calling CoCreateInstanceEx() " << endl;
  13. // Your user, domain, and password below
  14. COAUTHIDENTITY AuthIdentity;
  15. AuthIdentity.User = L"User";
  16. AuthIdentity.UserLength = wcslen(L"User");
  17. AuthIdentity.Domain = L"Domain";
  18. AuthIdentity.DomainLength = wcslen(L"Domain");
  19. AuthIdentity.Password = L"Password";
  20. AuthIdentity.PasswordLength = wcslen(L"Password");
  21. AuthIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  22. COAUTHINFO AuthInfo;
  23. AuthInfo.dwAuthnSvc = RPC_C_AUTHN_WINNT;
  24. AuthInfo.dwAuthzSvc = RPC_C_AUTHZ_NONE;
  25. AuthInfo.pwszServerPrincName = NULL;
  26. AuthInfo.dwAuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT;
  27. AuthInfo.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
  28. AuthInfo.pAuthIdentityData = &AuthIdentity;
  29. AuthInfo.dwCapabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
  30. // Your server name below
  31. COSERVERINFO ServerInfo;
  32. ServerInfo.dwReserved1 = 0;
  33. ServerInfo.pwszName = L"RemoteServerName";
  34. ServerInfo.pAuthInfo = &AuthInfo;
  35. ServerInfo.dwReserved2 = 0;
  36. MULTI_QI qi;
  37. qi.pIID = &IID_IUnknown;
  38. qi.pItf = NULL;
  39. qi.hr = 0;
  40. HRESULT hr = CoCreateInstanceEx(CLSID_InsideCOM, NULL, CLSCTX_REMOTE_SERVER, &ServerInfo, 1, &qi);
  41. pUnknown = qi.pItf;
  42. if(FAILED(hr))
  43. {
  44. if(hr == E_ACCESSDENIED)
  45. printf("CoCreateInstance FAILED hr = E_ACCESSDENTIEDn", hr);
  46. else
  47. printf("CoCreateInstance FAILED hr = %0xn", hr);
  48. exit(0);
  49. }
  50. cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
  51. hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  52. if(FAILED(hr))
  53. cout << "QueryInterface FAILED" << endl;
  54. cout << "Client: Calling Release() for pUnknown" << endl;
  55. hr = pUnknown->Release();
  56. cout << "Client: pSum = " << pSum << endl;
  57. int sum;
  58. hr = pSum->Sum(4, 9, &sum);
  59. if(SUCCEEDED(hr))
  60. cout << "Client: Calling Sum() return value is " << sum << endl;
  61. cout << "Client: Calling Release() for pSum" << endl;
  62. hr = pSum->Release();
  63. cout << "Client: Calling CoUninitialize()" << endl;
  64. CoUninitialize();
  65. }