Server code -----------------------------------------------------------------------------------------------------------------------------------------------------------
Server code certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------
Altstopwatch. idl [interface definition file]
// altstopwatch.idl : IDL source for altstopwatch//// This file will be processed by the MIDL tool to// produce the type library (altstopwatch.tlb) and marshalling code.import "oaidl.idl";import "ocidl.idl";[object,uuid(FD2BE6F3-9074-47F3-9884-FDD3DE13D158),pointer_default(unique)]
// Define the interface Istopwatch: IUnknown {
// Identical attributes (all pointing to the same member variable) with the same ID value[Propget, id (1), helpstring ("value")] HRESULT name ([out, retval] BSTR * val); [propput, id (1 ), helpstring ("set value")] HRESULT name ([in] BSTR val );}; [Uuid (A4E51DE3-D343-4CB2-A901-5CE1D2067309), version (1.0),] library altstopwatchLib {importlib ("stdole2.tlb"); [uuid (FD3216FD-B513-40F1-BE2C-72B2954D1368), helpstring ("help string stopwatch class")] coclass stopwatch {[default] interface Istopwatch ;};};
Stopwatch. h [interface definition header file]
// stopwatch.h : Declaration of the Cstopwatch#pragma once#include "resource.h" // main symbols#include "altstopwatch_i.h"#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."#endifusing namespace ATL;// Cstopwatchclass ATL_NO_VTABLE Cstopwatch :public CComObjectRootEx
,public CComCoClass
,public Istopwatch{public:Cstopwatch(){}DECLARE_REGISTRY_RESOURCEID(IDR_STOPWATCH)BEGIN_COM_MAP(Cstopwatch)COM_INTERFACE_ENTRY(Istopwatch)END_COM_MAP()DECLARE_PROTECT_FINAL_CONSTRUCT()HRESULT FinalConstruct(){return S_OK;}void FinalRelease(){}public:
// Implement the interfacePublic: virtual/* [helpstring] [id] [propget] */hresult stdmethodcalltype get_name (/* [retval] [out] */BSTR * val) {m_name.CopyTo (val ); return S_ OK;} virtual/* [helpstring] [id] [propput] */HRESULT STDMETHODCALLTYPE put_name (/* [in] */BSTR val) {m_name.AssignBSTR (val ); return S_ OK ;}Private: CComBSTR m_name;}; OBJECT_ENTRY_AUTO (_ uuidof (stopwatch), Cstopwatch)
######################################## ######################################## ######################################## #######
Client code
# Include
# Include
# Import "../Debug/altstopwatch. dll" named_guids no_namespace void test () {CoInitialize (NULL); {CComPtr
Ptr; HRESULT hr = ptr. coCreateInstance (CLSID_stopwatch, NULL, CLSCTX_ALL); assert (SUCCEEDED (hr); // since I have previously written the following code: // BSTR s = L "test :: sysAllocStringByteLen is always 0 sweat !!!!!!!!!!!!!! BSTR s =: SysAllocString (L "test"); // The string to evaluate the value, which must be allocated by: SysAllocByte int len =: SysStringByteLen (s ); int len2 =: SysStringByteLen (L "ffffff"); assert (len = 8); assert (len2 = 0); BSTR m_str =: SysAllocStringByteLen (char *) s, len); ptr-> put_name (s); BSTR sr = NULL; ptr-> get_name (& sr); int r = wcscmp (s, sr ); assert (r = 0);: SysFreeString (s); printf ("access resource success! ");} CoUninitialize ();} int main (int argc, char * argv []) {test (); getchar (); return 0 ;}