Skip to content

Commit 445a0a8

Browse files
author
Fredrik Orderud
committed
Add array-passing example.
1 parent 623fcc0 commit 445a0a8

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

MyClientCpp/Main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ int main() {
7575
double pi = cruncher->ComputePi();
7676
std::wcout << L"pi = " << pi << std::endl;
7777

78+
CComSafeArray<BYTE> arr;
79+
arr.Attach(cruncher->ComputeValues(4));
80+
std::wcout << L"array: [";
81+
for (unsigned int i = 0; i < arr.GetCount(); ++i)
82+
std::wcout << (int)arr[(LONG)i] << L", ";
83+
std::wcout << L"]\n";
84+
7885
auto callback = CreateLocalInstance<MyClient>();
7986
server->Subscribe(callback);
8087

MyClientCs/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ static void CommunicateWithServer()
1616
double pi = cruncher.ComputePi();
1717
Console.WriteLine($"pi = {pi}");
1818

19+
byte[] values = cruncher.ComputeValues(4);
20+
Console.WriteLine("ComputeValues: ["+ string.Join(", ", values) + "]");
21+
1922
// release reference to help GC clean up (not strctly needed)
2023
cruncher = null;
2124
}

MyInterfaces/MyInterfaces.idl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ interface IMyClient : IUnknown {
8888
interface INumberCruncher : IUnknown {
8989
[helpstring("Compute the value of pi")]
9090
HRESULT ComputePi([out, retval] double *ret);
91+
92+
[helpstring("Get an array with magic values.")]
93+
HRESULT ComputeValues([in] unsigned int count, [out, retval] SAFEARRAY(BYTE)* vals);
9194
};
9295

9396
[object,

MyServerCpp/MyServerImpl.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ class ATL_NO_VTABLE NumberCruncher :
3737
return S_OK;
3838
}
3939

40+
HRESULT STDMETHODCALLTYPE raw_ComputeValues(unsigned int count, /*out*/SAFEARRAY** vals) override {
41+
if (!vals)
42+
return E_INVALIDARG;
43+
44+
// populate array with values
45+
CComSafeArray<BYTE> tmp(count);
46+
for (unsigned int i = 0; i < count; ++i)
47+
tmp[(LONG)i] = (BYTE)i;
48+
49+
*vals = tmp.Detach();
50+
return S_OK;
51+
}
52+
53+
4054
BEGIN_COM_MAP(NumberCruncher)
4155
COM_INTERFACE_ENTRY(INumberCruncher)
4256
END_COM_MAP()

MyServerCs/MyServerImpl.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,14 @@ public double ComputePi ()
142142
{
143143
return Math.PI;
144144
}
145+
146+
public byte[] ComputeValues(uint count)
147+
{
148+
byte[] values = new byte[count];
149+
for (uint i = 0; i < count; i++)
150+
values[i] = (byte)i;
151+
152+
return values;
153+
}
145154
}
146155
}

0 commit comments

Comments
 (0)