1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
//---------------------------------------------------------------------------
#include <vcl.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <Winsock2.h>
#include <tchar.h>
#pragma hdrstop
#include "ServeurCOMtest_TLB.h"
USEUNIT("ServeurCOMtest_TLB.cpp");
//---------------------------------------------------------------------------
int main()
{
HRESULT reponse;
TCOMIServeurCOMT<ServeurCOM> serveur;
/***********************************************************/
/***********************************************************/
/** Initialisation **/
/***********************************************************/
/***********************************************************/
std::cout<< "Initialize: " << std::endl;
if(!SUCCEEDED(reponse=CoInitialize(NULL)))
switch(reponse){
case S_OK:
std::cout << "The COM library was initialized successfully on this thread." << std::endl;
break;
case S_FALSE:
std::cout << "The COM library is already initialized on this thread.." << std::endl;
break;
case RPC_E_CHANGED_MODE:
std::cout << "A previous call to CoInitializeEx specified the concurrency model for this thread as multithread apartment (MTA). If running Windows 2000, this could also mean that a change from neutral-threaded apartment to single-threaded apartment occurred." << std::endl;
break;
default:
std::cout << "Reponse inconnue???: 0x" << std::hex << reponse << std::endl;
break;
}
else
std::cout << "The COM library was initialized successfully on this thread." << std::endl;
/***********************************************************/
/***********************************************************/
/** Avec CoGetClassObject **/
/***********************************************************/
/***********************************************************/
std::cout<< std::endl << "Get Object: " << std::endl;
// Structure "login" pr se connecter sur le PC serveur
COAUTHIDENTITY userLoginOnServeur;
userLoginOnServeur.User = L"coulois";
userLoginOnServeur.UserLength = 7;
userLoginOnServeur.Domain = L"fr";
userLoginOnServeur.DomainLength = 2;
userLoginOnServeur.Password = L"tagada65";
userLoginOnServeur.PasswordLength = 8;
userLoginOnServeur.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
// Structure Info auteur
COAUTHINFO authentification;
authentification.dwAuthnSvc = RPC_C_AUTHN_DEFAULT;
authentification.dwAuthzSvc = RPC_C_AUTHZ_DEFAULT;
authentification.pwszServerPrincName = NULL;
authentification.dwAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
authentification.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
authentification.pAuthIdentityData = &userLoginOnServeur;
authentification.dwCapabilities = EOAC_NONE;
//Creation du COSERVERINFO
COSERVERINFO serveurInfo;
wchar_t serveurName[] = L"UC06P1556.fr.pierre-fabre.lcl";
//std::wstring serveurName = L"UC06P1556.fr.pierre-fabre.lcl";
serveurInfo.pwszName = serveurName;
serveurInfo.pAuthInfo = &authentification;
serveurInfo.dwReserved1 = 0;
serveurInfo.dwReserved2 = 0;
//Création de la ClassFactory
IClassFactory * pFactory;
//CLSCTX_REMOTE_SERVER|CLSCTX_LOCAL_SERVER CLSCTX_ALL
reponse=CoGetClassObject(CLSID_ServeurCOM, CLSCTX_REMOTE_SERVER, /*NULL*/&serveurInfo, IID_IClassFactory, reinterpret_cast<void**>(&pFactory)); // IID_IClassFactory
if(!SUCCEEDED(reponse))
switch(reponse){
case REGDB_E_CLASSNOTREG:
std::cout << "CLSID is not properly registered. Can also indicate that the value you specified in dwClsContext is not in the registry." << std::endl;
break;
case E_NOINTERFACE:
std::cout << "Either the object pointed to by ppv does not support the interface identified by riid, or the QueryInterface operation on the class object returned E_NOINTERFACE." << std::endl;
break;
case REGDB_E_READREGDB:
std::cout << "Error reading the registration database." << std::endl;
break;
case CO_E_DLLNOTFOUND:
std::cout << "In-process DLL or handler DLL not found (depends on context)." << std::endl;
break;
case CO_E_APPNOTFOUND:
std::cout << "EXE not found (CLSCTX_LOCAL_SERVER only)." << std::endl;
break;
case E_ACCESSDENIED:
std::cout << "General access failure (returned from LoadLib/CreateProcess)." << std::endl;
break;
case CO_E_ERRORINDLL:
std::cout << "EXE has error in image." << std::endl;
break;
case CO_E_APPDIDNTREG:
std::cout << "EXE was launched, but it didn't register class object (may or may not have shut down)." << std::endl;
break;
case CO_E_NOTINITIALIZED:
std::cout << "CO_E_NOTINITIALIZED." << std::endl;
break;
default:
std::cout << "Reponse inconnue???: 0x" << std::hex << reponse << std::endl;
DWORD dw = reponse;
LPTSTR lpszFunction = "CoGetClassObject";
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
wsprintf((LPTSTR)lpDisplayBuf,
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
break;
}
else{
std::cout << "Location and connection to the specified class object was successful." << std::endl;
std::cout<< std::endl << "CreateInstance: " << std::endl;
//Création de l'objet
void * pvObj = NULL;
reponse = pFactory->CreateInstance(NULL, IID_IServeurCOM, &pvObj);
if(!SUCCEEDED(reponse))
switch(reponse){
case CLASS_E_NOAGGREGATION:
std::cout << "The pUnkOuter parameter was non-NULL and the object does not support aggregation." << std::endl;
break;
case E_NOINTERFACE:
std::cout << "The object that ppvObject points to does not support the interface identified by riid." << std::endl;
break;
default:
std::cout << "Reponse inconnue???: 0x" << std::hex << reponse << std::endl;
DWORD dw = reponse;
LPTSTR lpszFunction = "pFactory->CreateInstance";
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
wsprintf((LPTSTR)lpDisplayBuf,
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
break;
}
else{
std::cout << "The specified object was created." << std::endl;
std::cout<< std::endl << "Release. " << std::endl;
pFactory->Release(), pFactory=NULL;
//Je ne sais pas comment on assigne un pointeur de référence à un pointeur intelligent Borland.
//Mais ce doit être un truc de ce genre
serveur = static_cast< IServeurCOM * >(pvObj);
}
}
/***********************************************************/
/***********************************************************/
/** Appel de la methode à distance **/
/***********************************************************/
/***********************************************************/
if( SUCCEEDED(reponse) ){
std::cout << std::endl << "Appel de l'objet distant:" << std::endl;
std::wcout << "Nom du PC: " << serveur.NomDuPC << " et voila." << std::endl;
}
system("PAUSE");
return 0;
}
//--------------------------------------------------------------------------- |
Partager