문제

Python COM 서버를 구현하고 py2exe 도구를 사용하여 실행 파일 및 DLL을 생성합니다. 그런 다음 regsvr32.exe를 사용하여 DLL을 등록했습니다. 등록이 성공했다는 메시지를 받았습니다. 그런 다음 .NET에서 해당 DLL에 대한 참조를 추가하려고했습니다. DLL 위치로 탐색하고 선택했지만 다음과 같은 오류 메시지 상자가 있습니다. DLL에 대한 참조를 추가 할 수 없으면 파일에 액세스 할 수 있고 유효한 어셈블리 또는 COM 구성 요소인지 확인하십시오. 서버 및 설정 스크립트의 코드는 아래에 추가됩니다. 서버를 파이썬 스크립트로 실행하고 늦은 바인딩을 사용하여 .NET에서 소비 할 수 있다고 언급하고 싶습니다. 내가 놓치거나 잘못하고있는 것이 있습니까? 어떤 도움을 주셔서 감사합니다.

고마워, 사라

안녕하세요

import pythoncom

import sys

class HelloWorld:

    #pythoncom.frozen = 1
    if hasattr(sys, 'importers'):
        _reg_class_spec_ = "__main__.HelloWorld" 
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    _reg_clsid_ = pythoncom.CreateGuid()
    _reg_desc_ = "Python Test COM Server"
    _reg_progid_ = "Python.TestServer"
    _public_methods_ = ['Hello']
    _public_attrs_ = ['softspace', 'noCalls']
    _readonly_attrs_ = ['noCalls']

    def __init__(self):
        self.softspace = 1
        self.noCalls = 0

    def Hello(self, who):
        self.noCalls = self.noCalls + 1
        # insert "softspace" number of spaces
        print "Hello" + " " * self.softspace + str(who)
        return "Hello" + " " * self.softspace + str(who)


if __name__=='__main__':
    import sys
    if hasattr(sys, 'importers'):

        # running as packed executable.

        if '--register' in sys.argv[1:] or '--unregister' in sys.argv[1:]:

            # --register and --unregister work as usual
            import win32com.server.register
            win32com.server.register.UseCommandLine(HelloWorld)
        else:

            # start the server.
            from win32com.server import localserver
            localserver.main()
    else:

        import win32com.server.register
        win32com.server.register.UseCommandLine(HelloWorld) 

setup.py

from distutils.core import setup
import py2exe

setup(com_server = ["hello"])
도움이 되었습니까?

해결책 2

나는 어떤 사람이 비슷한 질문을 할 수 있도록 내 질문에 대답 할 것입니다. 도움이되기를 바랍니다. .NET (& Visual-Studio)에 TLB가있는 COM 서버가 필요하기 때문에 COM 탭에서 내 서버를 찾을 수 없습니다. 그러나 Python의 COM 서버에는 TLB가 없습니다. 따라서 (c# 및 늦은 바인딩) .net에서 서버를 사용합니다. 다음 코드는 이것을 만드는 방법을 보여줍니다.

// C# 코드

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Reflection;

namespace ConsoleApplication2

{

    class Program

    {
        static void Main(string[] args)

        {

              Type pythonServer;
              object pythonObject;
              pythonServer = Type.GetTypeFromProgID("PythonDemos.Utilities");
              pythonObject = Activator.CreateInstance(pythonServer);

        }
    }
}   `

다른 팁

라인 :

_reg_clsid_ = pythoncom.CreateGuid()

이 파일이 호출 될 때마다 새 안내서를 만듭니다. 명령 줄에서 GUID를 만들 수 있습니다.

C:\>python -c "import pythoncom; print pythoncom.CreateGuid()"
{C86B66C2-408E-46EA-845E-71626F94D965}

그런 다음 라인을 변경하십시오.

_reg_clsid_ = "{C86B66C2-408E-46EA-845E-71626F94D965}"

이 변경 후, 나는 당신의 코드를 실행하고 다음 vbscript로 테스트 할 수있었습니다.

Set obj = CreateObject("Python.TestServer")   
MsgBox obj.Hello("foo")

MSVC가 "참조 추가"문제를 해결하는지 확인하기 위해 편리하지 않습니다.

등록 된 것을 사용하려는 경우 com 객체, 당신은 com 탭에서 그것을 찾아야합니다. 참조를 추가하십시오 대화 상자. DLL로 이동하지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top