-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from supersega/pycomserver
Implement python COM Server
- Loading branch information
Showing
20 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ | |
Build/ | ||
packages/ | ||
*/Generated Files/ | ||
*/env/ | ||
|
||
*.tlb | ||
*.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import "oaidl.idl"; | ||
import "ocidl.idl"; | ||
|
||
[ | ||
oleautomation, | ||
object, | ||
uuid(0A315D51-1316-4822-9DB8-0980E4AFC7E0), | ||
pointer_default(unique) | ||
] | ||
interface IRoyalPython : IUnknown | ||
{ | ||
HRESULT Eat(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from ctypes import * | ||
from comtypes import CLSCTX_LOCAL_SERVER, CLSCTX_INPROC_SERVER | ||
from comtypes.server.localserver import REGCLS_MULTIPLEUSE | ||
import sys | ||
|
||
# Get wrapped code for RoyalPython.tlb, | ||
# It was generated in RegPyComServer.bat file from RoyalPython.idl | ||
from comtypes.client import GetModule | ||
GetModule("RoyalPython.tlb") | ||
|
||
# comtypes.gen.PyComServer was generated by GetModule above | ||
# RoyalPython corresponds to the RoyalPython from RoyalPython.idl | ||
from comtypes.gen.PyComServer import RoyalPython | ||
|
||
# Implement COM server using auto generated type RoyalPython | ||
class PyRoyalPython(RoyalPython): | ||
_reg_threading_ = "Both" | ||
_reg_progid_ = "PyComServer.RoyalPython.1" | ||
_reg_novers_progid_ = "PyComServer.RoyalPython" | ||
_reg_clsctx_ = CLSCTX_LOCAL_SERVER | CLSCTX_INPROC_SERVER | ||
_reg_desc_ = "Royal python eats mammals" | ||
_regcls_ = REGCLS_MULTIPLEUSE | ||
|
||
def Eat(self): | ||
print("eat") | ||
|
||
if __name__ == '__main__': | ||
# If some arguments passed to the script we call UseCommandLine what register/unregister COM server. | ||
if len(sys.argv) > 1: | ||
from comtypes.server.register import UseCommandLine | ||
UseCommandLine(PyRoyalPython) | ||
else: | ||
# Run the server. | ||
from win32com.server import localserver | ||
localserver.serve(['{F405A270-9088-4800-A5CD-A5E1DED02AB4}']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>b48dc892-e120-49f5-8e20-d497214811d5</ProjectGuid> | ||
<ProjectHome>.</ProjectHome> | ||
<StartupFile>PyComServer.py</StartupFile> | ||
<SearchPath> | ||
</SearchPath> | ||
<WorkingDirectory>.</WorkingDirectory> | ||
<OutputPath>.</OutputPath> | ||
<Name>PyComServer</Name> | ||
<RootNamespace>PyComServer</RootNamespace> | ||
<LaunchProvider>Standard Python launcher</LaunchProvider> | ||
<CommandLineArguments>/regserver</CommandLineArguments> | ||
<EnableNativeCodeDebugging>False</EnableNativeCodeDebugging> | ||
<InterpreterId>MSBuild|env|$(MSBuildProjectFullPath)</InterpreterId> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="PyComServer.py" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="readme.md" /> | ||
<Content Include="RoyalPython.idl" /> | ||
<Content Include="requirements.txt" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Interpreter Include="env\"> | ||
<Id>env</Id> | ||
<Version>3.9</Version> | ||
<Description>env (Python 3.9 (64-bit))</Description> | ||
<InterpreterPath>Scripts\python.exe</InterpreterPath> | ||
<WindowsInterpreterPath>Scripts\pythonw.exe</WindowsInterpreterPath> | ||
<PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable> | ||
<Architecture>X64</Architecture> | ||
</Interpreter> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" /> | ||
<!-- Uncomment the CoreCompile target to enable the Build command in | ||
Visual Studio and specify your pre- and post-build commands in | ||
the BeforeBuild and AfterBuild targets below. --> | ||
<!--<Target Name="CoreCompile" />--> | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
setlocal | ||
pushd %~dp0 | ||
|
||
@echo on | ||
|
||
py -3 -m venv ..\Build\buildenv || exit /b 1 | ||
call ..\Build\buildenv\Scripts\Activate.bat || exit /b 1 | ||
|
||
python -m pip install -r requirements.txt || exit /b 1 | ||
|
||
:: In C++ projects we do it automatically, but for python server handcrafted generation needed | ||
midl RoyalPython.idl || exit /b 1 | ||
python PyComServer.py -regserver || exit /b 1 | ||
|
||
popd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import "oaidl.idl"; | ||
import "ocidl.idl"; | ||
import "..\Interfaces\IRoyalPython.idl"; | ||
|
||
[ | ||
uuid(F0D8338A-BDC1-45D7-A14F-27D64E7BCA18) | ||
] | ||
library PyComServer | ||
{ | ||
importlib("stdole2.tlb"); | ||
|
||
[uuid(F405A270-9088-4800-A5CD-A5E1DED02AB4)] | ||
coclass RoyalPython { | ||
[default] interface IRoyalPython; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# PyComServer | ||
|
||
This project contains a COM Server implemented with python, pywin32 and comtypes used to create COM Server. | ||
To be able to register PyComServer we have to generate .tlb manually from .idl using midl compiler. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pywin32 | ||
comtypes==1.1.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
set OutDir=%1 | ||
regsvr32 /s %OutDir%AtlServer.dll || exit /b 1 | ||
regsvr32 /s %OutDir%AtlFreeServer.dll || exit /b 1 | ||
regsvr32 /s %OutDir%ManagedServer\ManagedServer.comhost.dll || exit /b 1 | ||
regsvr32 /s %OutDir%ManagedServer\ManagedServer.comhost.dll || exit /b 1 | ||
|
||
call ..\PyComServer\RegPyComServer.bat || exit /b 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "../pch.h" | ||
#include <gtest/gtest.h> | ||
#include <Interfaces/IRoyalPython.h> | ||
#include <ComUtility/Utility.h> | ||
#include <wrl.h> | ||
|
||
using namespace testing; | ||
using Microsoft::WRL::ComPtr; | ||
|
||
TEST(PyComServerTests, | ||
RequireThat_CoCreateInstance_CreatesPythonServer_WhenCalledWithGuidToPythonServer) | ||
{ | ||
CLSID classid{}; | ||
HR(CLSIDFromProgID(L"PyComServer.RoyalPython.1", &classid)); | ||
|
||
ComPtr<IRoyalPython> py; | ||
HR(CoCreateInstance(classid, nullptr, CLSCTX_LOCAL_SERVER, __uuidof(IRoyalPython), &py)); | ||
EXPECT_NE(py, nullptr); | ||
|
||
py->Eat(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters