pywin32 icon indicating copy to clipboard operation
pywin32 copied to clipboard

Visual Studio AddPropertySheet Type Error

Open ghost opened this issue 13 years ago • 6 comments

When trying to add a style sheet to a visual studio C++ project using pythoncom, the following error occurs

Traceback (most recent call last): File "test.py", line 87, in <module> proj.addPropertySheet("d:\\temp\\vs\\boost.props") File "test.py", line 34, in addPropertySheet sheet = conf.AddPropertySheet(file) File "C:\Python27\lib\site-packages\win32com\gen_py\0CD36BB6-D828-4DB9-91BF-AD493EE76B79x0x10x0\VCConfiguration.py", line 35, in AddPropertySheet ret = self._oleobj_.InvokeTypes(782, LCID, 1, (9, 0), ((31, 1),),FileName TypeError: The VARIANT type is unknown (0x0000001f)

Source is attached.

Reported by: *anonymous

Original Ticket: pywin32/bugs/621

ghost avatar Nov 19 '12 13:11 ghost

Source highlighting issue

Original comment by: *anonymous

ghost avatar Nov 19 '12 13:11 ghost

31 is VT_LPWSTR, which is not valid in a VARIANT. This looks like a bug in the typelib. If you edit the generated file and change it to VT_BSTR (8) does it succeed ?

Original comment by: rupole

ghost avatar Nov 19 '12 18:11 ghost

Changing to VT_BSTR(8) results in a different error

Traceback (most recent call last): File "test.py", line 87, in <module> proj.addPropertySheet("d:\\temp\\vs\\boost.props") File "test.py", line 34, in addPropertySheet sheet = conf.AddPropertySheet(file) File "C:\Python27\lib\site-packages\win32com\gen_py\0CD36BB6-D828-4DB9-91BF-AD493EE76B79x0x10x0\VCConfiguration.py", line 35, in AddPropertySheet ret = self._oleobj_.InvokeTypes(782, LCID, 1, (9, 0), ((8, 1),),FileName pywintypes.com_error: (-2147352568, 'Bad variable type.', None, None)

Original comment by: *anonymous

ghost avatar Nov 21 '12 10:11 ghost

Could you try passing win32com.client.VARIANT(pythoncom.VT_LPWSTR, "the string") and see if that helps?

Original comment by: mhammond

ghost avatar Nov 24 '12 05:11 ghost

Passing the variant as suggested results in the original error

TypeError: The VARIANT type is unknown (0x0000001f)

Original comment by: *anonymous

ghost avatar Nov 27 '12 10:11 ghost

Updated source file with a fix to VSSolution.__del__ in case it fails during __init__:

import win32com.client


class VSCProject:
    def __init__(self, vs, proj):
        self.vs = vs
        self.proj = proj

    def addX64(self):
        self.proj.Object.AddPlatform("x64")

    def addFolder(self, name, filters):
        folder = self.proj.Object.AddFilter(name)
        folder.Filter = filters
        return folder

    def addFile(self, file):
        self.proj.Object.AddFile(file)

    def addPropertySheet(self, file):
        conf = self.proj.Object.Configurations.Item(1)
        win32com.client.gencache.EnsureModule(
            "{0168EA0D-6DB0-457E-BDDD-27FD7A3EDFBB}", 0, 10, 0
        )
        # win32com.client.gencache.EnsureModule('{BDC20262-865F-498D-87AF-4070B8459198}', 0, 10, 0)

        sheets = conf.PropertySheets
        count = sheets.Count
        for pos in range(count):
            print(sheets.Item(pos + 1))

        print(conf)
        # conf.PropertySheets.AddPropertySheet(file)

        sheet = conf.AddPropertySheet(file)
        # sheet = conf.VCProjectEngine.LoadPropertySheet(file)
        # conf.InheritedPropertySheets = file


class VSSolution:
    def __init__(self):
        win32com.client.gencache.EnsureModule("{656D8328-93F5-41a7-A48C-B42858161F25}", 0, 10, 0)
        self.vs = win32com.client.DispatchEx("VisualStudio.DTE.10.0")
        self.soln = None

    def __del__(self):
        if getattr(self, "vs", None) is not None:
            self.vs.Quit()

    def create(self, path, name):
        self.soln = self.vs.Solution.Create(path, name)
        self.soln = self.vs.Solution
        print("Created Solution: " + path + "\\" + name + ".sln")

    def open(self, path):
        self.soln = self.vs.Solution.Open(path)
        self.soln = self.vs.Solution

    def close(self, save):
        if self.soln:
            self.soln.Close(save)
            self.soln = None

    def createCProject(self, path, name):
        templatepath = "C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\VCWizards\\default.vcxproj"
        proj = self.soln.AddFromTemplate(templatepath, path, name, False)
        print("Created Project: " + path + "\\" + name + ".vcproj")
        return VSCProject(self.vs, proj)

    def createX64Configuration(self):
        proj = self.soln.Projects.Item(1)
        mgr = proj.ConfigurationManager
        mgr.AddPlatform("x64", "Win32", True)

        configs = self.vs.Solution.SolutionBuild.SolutionConfigurations
        count = configs.Count
        for index in range(count):
            config = configs.Item(index + 1)
            ctx = config.SolutionContexts.Item(1)
            if config.Name == "Debug" and ctx.PlatformName == "x64":
                config.Activate()
                break


soln = VSSolution()
soln.create("d:\\temp\\vs\\output", "TestSolution")

proj = soln.createCProject("d:\\temp\\vs\\output\\TestProject", "TestProject")
proj.addPropertySheet("d:\\temp\\vs\\boost.props")
proj.addX64()
proj.addFolder("Header Files", "h;hxx")
proj.addFolder("Source Files", "cc;cpp;cxx;c")
proj.addFile("test.h")
proj.addFile("test.cc")

proj2 = soln.createCProject("d:\\temp\\vs\\output\\TestProject2", "TestProject2")
proj2.addX64()

soln.createX64Configuration()

soln.close(True)

Unfortunately I don't have the rest of the setup to test this specific issue.

Avasam avatar Mar 17 '24 02:03 Avasam