Đầu tiên: bạn chọn VC++ --> ATL







Tiếp đó, để viết code của bạn vào, bạn vào Menu View -> CLass view --> Add class





Đánh tên Class như hình bên





Chọn như hình rồi Finish




Vào class diagram chọn Add method
http://forums.congdongcviet.com/showthread.php?t=36075




Nhập thêm tên biến, tên hàm vào




<font size="4">Cuối cùng,tạo 1 Project đơn giản để test DLL server




đây là cách load COM server:

Mã nguồn PHP:
#include <windows.h>#include <atlstr.h>#include "../MyCOM/MyCOM_i.h"#include "../MyCOM/MyCOM_i.c"#define STDAPI EXTERN_C HRESULT STDAPICALLTYPEbool GetFolder(__in TCHAR* tszPathIn,__out TCHAR* tszDirectory){ lstrcpy(tszDirectory,tszPathIn); int ileng = lstrlen(tszDirectory); int i; for ( i = ileng -1;i>=0;i--) { if (tszDirectory[i] == _T('\\')) { tszDirectory[i] = _T('\0'); break; } } if (i == 0) { tszDirectory = NULL; return false; } return true;}bool CurrentPath(__out TCHAR* tszPathOut,__in int iMaxPath){ TCHAR tszPathCurrentFile[MAX_PATH]; if (GetModuleFileName( NULL, tszPathCurrentFile, iMaxPath) == FALSE) { return false; } if (GetFolder(tszPathCurrentFile,tszPathOut) == false) { tszPathOut = NULL; return false; } return true;}bool RegisterCOM(__in TCHAR* szPathDLL){ HINSTANCE hInstallDLL;#ifdef _UNICODE hInstallDLL = LoadLibraryW((WCHAR*)szPathDLL);#else hInstallDLL = LoadLibraryA((char*)szPathDLL);#endif if (hInstallDLL == NULL) { return false; }//http://forums.congdongcviet.com/showthread.php?t=36075 typedef HRESULT (STDAPICALLTYPE* REGISTERPointer)(); REGISTERPointer DllRegisterServer; DllRegisterServer = (REGISTERPointer)GetProcAddress(hInstallDLL, EXPORT_REGISTER_COM); if (DllRegisterServer == NULL) { return false; }//http://forums.congdongcviet.com/showthread.php?t=36075 DllRegisterServer(); return true;}int main(int argc, _TCHAR* argv[]){ TCHAR szCurrentDirectory[MAX_PATH]; CurrentPath(szCurrentDirectory,MAX_PATH); SetCurrentDirectory(szCurrentDirectory);#ifdef _DEBUG RegisterCOM(_T("../MyCOM/Debug/Mycom.dll"));#else RegisterCOM(_T("../MyCOM/Release/Mycom.dll"));#endif BSTR* bstsMessage; HRESULT hr; ISimpleCOM *isimChat; hr = CoInitialize(0); if(SUCCEEDED(hr)) { hr = CoCreateInstance(CLSID_SimpleCOM, NULL, CLSCTX_INPROC_SERVER, IID_ISimpleCOM, (void**) &isimChat); if(SUCCEEDED(hr)) { hr = isimChat->callme(_T("Congdongcviet"), &bstsMessage); MessageBox(NULL,*bstsMessage, _T("Message Return From COM Server"),0); ::SysFreeString(*bstsMessage); hr = isimChat->Release(); } } CoUninitialize(); return 0;}  
</font>