From fad6894678268160fe9afccb282b0eada2b1b40b Mon Sep 17 00:00:00 2001 From: Fredrik Orderud Date: Mon, 13 Nov 2023 17:10:28 +0100 Subject: [PATCH] Add array-passing example. --- MyClientCpp/Main.cpp | 7 +++++++ MyClientCs/Program.cs | 3 +++ MyExeServerCpp/MyServerImpl.hpp | 14 ++++++++++++++ MyExeServerCs/MyServerImpl.cs | 9 +++++++++ MyInterfaces/MyInterfaces.idl | 3 +++ 5 files changed, 36 insertions(+) diff --git a/MyClientCpp/Main.cpp b/MyClientCpp/Main.cpp index 817bef2..08c361c 100644 --- a/MyClientCpp/Main.cpp +++ b/MyClientCpp/Main.cpp @@ -85,6 +85,13 @@ int main() { double pi = cruncher->ComputePi(); std::wcout << L"pi = " << pi << std::endl; + CComSafeArray arr; + arr.Attach(cruncher->ComputeValues(4)); + std::wcout << L"array: ["; + for (unsigned int i = 0; i < arr.GetCount(); ++i) + std::wcout << (int)arr[(LONG)i] << L", "; + std::wcout << L"]\n"; + auto callback = CreateLocalInstance(); server->Subscribe(callback); diff --git a/MyClientCs/Program.cs b/MyClientCs/Program.cs index 9ddfc3e..e5ffc16 100644 --- a/MyClientCs/Program.cs +++ b/MyClientCs/Program.cs @@ -16,6 +16,9 @@ static void CommunicateWithServer() double pi = cruncher.ComputePi(); Console.WriteLine($"pi = {pi}"); + byte[] values = cruncher.ComputeValues(4); + Console.WriteLine("ComputeValues: ["+ string.Join(", ", values) + "]"); + // release reference to help GC clean up (not strctly needed) cruncher = null; } diff --git a/MyExeServerCpp/MyServerImpl.hpp b/MyExeServerCpp/MyServerImpl.hpp index 012f19e..ebe8ad1 100644 --- a/MyExeServerCpp/MyServerImpl.hpp +++ b/MyExeServerCpp/MyServerImpl.hpp @@ -37,6 +37,20 @@ class ATL_NO_VTABLE NumberCruncher : return S_OK; } + HRESULT STDMETHODCALLTYPE raw_ComputeValues(unsigned int count, /*out*/SAFEARRAY** vals) override { + if (!vals) + return E_INVALIDARG; + + // populate array with values + CComSafeArray tmp(count); + for (unsigned int i = 0; i < count; ++i) + tmp[(LONG)i] = (BYTE)i; + + *vals = tmp.Detach(); + return S_OK; + } + + BEGIN_COM_MAP(NumberCruncher) COM_INTERFACE_ENTRY(INumberCruncher) END_COM_MAP() diff --git a/MyExeServerCs/MyServerImpl.cs b/MyExeServerCs/MyServerImpl.cs index 5688bf4..5b2172c 100644 --- a/MyExeServerCs/MyServerImpl.cs +++ b/MyExeServerCs/MyServerImpl.cs @@ -142,5 +142,14 @@ public double ComputePi () { return Math.PI; } + + public byte[] ComputeValues(uint count) + { + byte[] values = new byte[count]; + for (uint i = 0; i < count; i++) + values[i] = (byte)i; + + return values; + } } } diff --git a/MyInterfaces/MyInterfaces.idl b/MyInterfaces/MyInterfaces.idl index 029cae2..7acef7f 100644 --- a/MyInterfaces/MyInterfaces.idl +++ b/MyInterfaces/MyInterfaces.idl @@ -88,6 +88,9 @@ interface IMyClient : IUnknown { interface INumberCruncher : IUnknown { [helpstring("Compute the value of pi")] HRESULT ComputePi([out, retval] double *ret); + + [helpstring("Get an array with magic values.")] + HRESULT ComputeValues([in] unsigned int count, [out, retval] SAFEARRAY(BYTE)* vals); }; [object,