Skip to content

Commit 2d6d267

Browse files
Add NetServer/ComClient example
1 parent 39d2c6d commit 2d6d267

File tree

11 files changed

+434
-5
lines changed

11 files changed

+434
-5
lines changed

ComClient/ComClient.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "stdafx.h"
2+
#include <Windows.h>
3+
#include <atlbase.h>
4+
5+
using namespace ATL;
6+
7+
// IServer definition
8+
DECLARE_INTERFACE_IID_(IServer, IUnknown, "F38720E5-2D64-445E-88FB-1D696F614C78")
9+
{
10+
// Compute and return the value of PI
11+
STDMETHOD(ComputePi)(_Out_ double *pi) PURE;
12+
};
13+
14+
const IID IID_IServer = __uuidof(IServer);
15+
16+
// {114383E9-1969-47D2-9AA9-91388C961A19}
17+
const CLSID CLSID_Server = { 0x114383E9, 0x1969, 0x47D2, { 0x9A, 0xA9, 0x91, 0x38, 0x8C, 0x96, 0x1A, 0x19 } };
18+
19+
HRESULT QueryServer()
20+
{
21+
CComPtr<IServer> server;
22+
HRESULT hr = ::CoCreateInstance(CLSID_Server, nullptr, CLSCTX_INPROC, IID_IServer, (void**)&server);
23+
if (FAILED(hr))
24+
return hr;
25+
26+
double pi;
27+
hr = server->ComputePi(&pi);
28+
if (FAILED(hr))
29+
return hr;
30+
31+
::printf("\u03C0 = %f\n", pi);
32+
33+
return S_OK;
34+
}
35+
36+
int main()
37+
{
38+
// Set console codepage to utf-8. Also note the '/utf-8' compiler flag.
39+
::SetConsoleOutputCP(65001);
40+
41+
::CoInitializeEx(0, COINITBASE_MULTITHREADED);
42+
43+
HRESULT hr = QueryServer();
44+
45+
::CoUninitialize();
46+
47+
if (FAILED(hr))
48+
return EXIT_FAILURE;
49+
50+
return EXIT_SUCCESS;
51+
}
52+

ComClient/ComClient.vcxproj

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>15.0</VCProjectVersion>
23+
<ProjectGuid>{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}</ProjectGuid>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>ComClient</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v141</PlatformToolset>
33+
</PropertyGroup>
34+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
35+
<ConfigurationType>Application</ConfigurationType>
36+
<UseDebugLibraries>false</UseDebugLibraries>
37+
<PlatformToolset>v141</PlatformToolset>
38+
<WholeProgramOptimization>true</WholeProgramOptimization>
39+
</PropertyGroup>
40+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
41+
<ConfigurationType>Application</ConfigurationType>
42+
<UseDebugLibraries>true</UseDebugLibraries>
43+
<PlatformToolset>v141</PlatformToolset>
44+
</PropertyGroup>
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
46+
<ConfigurationType>Application</ConfigurationType>
47+
<UseDebugLibraries>false</UseDebugLibraries>
48+
<PlatformToolset>v141</PlatformToolset>
49+
<WholeProgramOptimization>true</WholeProgramOptimization>
50+
</PropertyGroup>
51+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
52+
<ImportGroup Label="ExtensionSettings">
53+
</ImportGroup>
54+
<ImportGroup Label="Shared">
55+
</ImportGroup>
56+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
57+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
58+
</ImportGroup>
59+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
60+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
61+
</ImportGroup>
62+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
63+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
64+
</ImportGroup>
65+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
66+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
67+
</ImportGroup>
68+
<PropertyGroup Label="UserMacros" />
69+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
70+
<LinkIncremental>true</LinkIncremental>
71+
</PropertyGroup>
72+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
73+
<LinkIncremental>true</LinkIncremental>
74+
</PropertyGroup>
75+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
76+
<LinkIncremental>false</LinkIncremental>
77+
</PropertyGroup>
78+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
79+
<LinkIncremental>false</LinkIncremental>
80+
</PropertyGroup>
81+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
82+
<ClCompile>
83+
<PrecompiledHeader>Use</PrecompiledHeader>
84+
<WarningLevel>Level3</WarningLevel>
85+
<Optimization>Disabled</Optimization>
86+
<SDLCheck>true</SDLCheck>
87+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
88+
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
89+
</ClCompile>
90+
<Link>
91+
<SubSystem>Console</SubSystem>
92+
<GenerateDebugInformation>true</GenerateDebugInformation>
93+
</Link>
94+
</ItemDefinitionGroup>
95+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
96+
<ClCompile>
97+
<PrecompiledHeader>Use</PrecompiledHeader>
98+
<WarningLevel>Level3</WarningLevel>
99+
<Optimization>Disabled</Optimization>
100+
<SDLCheck>true</SDLCheck>
101+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
102+
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
103+
</ClCompile>
104+
<Link>
105+
<SubSystem>Console</SubSystem>
106+
<GenerateDebugInformation>true</GenerateDebugInformation>
107+
</Link>
108+
</ItemDefinitionGroup>
109+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
110+
<ClCompile>
111+
<PrecompiledHeader>Use</PrecompiledHeader>
112+
<WarningLevel>Level3</WarningLevel>
113+
<Optimization>MaxSpeed</Optimization>
114+
<FunctionLevelLinking>true</FunctionLevelLinking>
115+
<IntrinsicFunctions>true</IntrinsicFunctions>
116+
<SDLCheck>true</SDLCheck>
117+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
118+
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
119+
</ClCompile>
120+
<Link>
121+
<SubSystem>Console</SubSystem>
122+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
123+
<OptimizeReferences>true</OptimizeReferences>
124+
<GenerateDebugInformation>true</GenerateDebugInformation>
125+
</Link>
126+
</ItemDefinitionGroup>
127+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
128+
<ClCompile>
129+
<PrecompiledHeader>Use</PrecompiledHeader>
130+
<WarningLevel>Level3</WarningLevel>
131+
<Optimization>MaxSpeed</Optimization>
132+
<FunctionLevelLinking>true</FunctionLevelLinking>
133+
<IntrinsicFunctions>true</IntrinsicFunctions>
134+
<SDLCheck>true</SDLCheck>
135+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
136+
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
137+
</ClCompile>
138+
<Link>
139+
<SubSystem>Console</SubSystem>
140+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
141+
<OptimizeReferences>true</OptimizeReferences>
142+
<GenerateDebugInformation>true</GenerateDebugInformation>
143+
</Link>
144+
</ItemDefinitionGroup>
145+
<ItemGroup>
146+
<ClInclude Include="stdafx.h" />
147+
<ClInclude Include="targetver.h" />
148+
</ItemGroup>
149+
<ItemGroup>
150+
<ClCompile Include="ComClient.cpp" />
151+
<ClCompile Include="stdafx.cpp">
152+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
153+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
154+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
155+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
156+
</ClCompile>
157+
</ItemGroup>
158+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
159+
<ImportGroup Label="ExtensionTargets">
160+
</ImportGroup>
161+
</Project>

ComClient/ComClient.vcxproj.filters

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClInclude Include="stdafx.h">
19+
<Filter>Header Files</Filter>
20+
</ClInclude>
21+
<ClInclude Include="targetver.h">
22+
<Filter>Header Files</Filter>
23+
</ClInclude>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<ClCompile Include="stdafx.cpp">
27+
<Filter>Source Files</Filter>
28+
</ClCompile>
29+
<ClCompile Include="ComClient.cpp">
30+
<Filter>Source Files</Filter>
31+
</ClCompile>
32+
</ItemGroup>
33+
</Project>

ComClient/stdafx.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// stdafx.cpp : source file that includes just the standard includes
2+
// ComClient.pch will be the pre-compiled header
3+
// stdafx.obj will contain the pre-compiled type information
4+
5+
#include "stdafx.h"
6+
7+
// TODO: reference any additional headers you need in STDAFX.H
8+
// and not in this file

ComClient/stdafx.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// stdafx.h : include file for standard system include files,
2+
// or project specific include files that are used frequently, but
3+
// are changed infrequently
4+
//
5+
6+
#pragma once
7+
8+
#include "targetver.h"
9+
10+
#include <stdio.h>
11+
#include <tchar.h>
12+
13+
14+
15+
// TODO: reference additional headers your program requires here

ComClient/targetver.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
// Including SDKDDKVer.h defines the highest available Windows platform.
4+
5+
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
6+
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
7+
8+
#include <SDKDDKVer.h>

ComInterop.sln

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetClient_RegFree", "NetCli
1212
{693824CA-5F5D-4714-9212-53799CAF3A9A} = {693824CA-5F5D-4714-9212-53799CAF3A9A}
1313
EndProjectSection
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetServer", "NetServer\NetServer.csproj", "{8F7988CB-2824-4479-B8A1-DD07BABDF957}"
16+
EndProject
17+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ComClient", "ComClient\ComClient.vcxproj", "{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}"
18+
EndProject
1519
Global
1620
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1721
Debug|Any CPU = Debug|Any CPU
@@ -58,6 +62,28 @@ Global
5862
{AE87BBA2-0A6B-4EDD-B10F-4D50DCFD25C4}.Release|x64.Build.0 = Release|Any CPU
5963
{AE87BBA2-0A6B-4EDD-B10F-4D50DCFD25C4}.Release|x86.ActiveCfg = Release|Any CPU
6064
{AE87BBA2-0A6B-4EDD-B10F-4D50DCFD25C4}.Release|x86.Build.0 = Release|Any CPU
65+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
66+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Debug|Any CPU.Build.0 = Debug|Any CPU
67+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Debug|x64.ActiveCfg = Debug|Any CPU
68+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Debug|x64.Build.0 = Debug|Any CPU
69+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Debug|x86.ActiveCfg = Debug|Any CPU
70+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Debug|x86.Build.0 = Debug|Any CPU
71+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Release|Any CPU.ActiveCfg = Release|Any CPU
72+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Release|Any CPU.Build.0 = Release|Any CPU
73+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Release|x64.ActiveCfg = Release|Any CPU
74+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Release|x64.Build.0 = Release|Any CPU
75+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Release|x86.ActiveCfg = Release|Any CPU
76+
{8F7988CB-2824-4479-B8A1-DD07BABDF957}.Release|x86.Build.0 = Release|Any CPU
77+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Debug|Any CPU.ActiveCfg = Debug|Win32
78+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Debug|x64.ActiveCfg = Debug|x64
79+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Debug|x64.Build.0 = Debug|x64
80+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Debug|x86.ActiveCfg = Debug|Win32
81+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Debug|x86.Build.0 = Debug|Win32
82+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Release|Any CPU.ActiveCfg = Release|Win32
83+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Release|x64.ActiveCfg = Release|x64
84+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Release|x64.Build.0 = Release|x64
85+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Release|x86.ActiveCfg = Release|Win32
86+
{E34CE8A4-4A28-4AE1-B825-8BC1EC0093EC}.Release|x86.Build.0 = Release|Win32
6187
EndGlobalSection
6288
GlobalSection(SolutionProperties) = preSolution
6389
HideSolutionNode = FALSE

NetServer/NetServer.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace NetServer
2+
{
3+
using System;
4+
using System.Runtime.InteropServices;
5+
6+
[ComVisible(true)]
7+
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
8+
[Guid("F38720E5-2D64-445E-88FB-1D696F614C78")]
9+
public interface IServer
10+
{
11+
double ComputePi();
12+
}
13+
14+
[ComVisible(true)]
15+
[Guid("114383E9-1969-47D2-9AA9-91388C961A19")]
16+
public class Server : IServer
17+
{
18+
public double ComputePi()
19+
{
20+
double sum = 0.0;
21+
int sign = 1;
22+
for (int i = 0; i < 64; ++i)
23+
{
24+
sum += sign / (2.0 * i + 1.0);
25+
sign *= -1;
26+
}
27+
28+
return 4.0 * sum;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy