Changing namespace name of C++ component in Windows Phone causes exception
Asked Answered
W

1

3

I have a C++ runtime component in a WP8 application, and if I change the namespace name, I get a "TargetInvocation" exception thrown whenever I try to instantiate a class in that namespace.

As an example, if I create the default C++ Windows Runtime Component, the header looks like this:

#pragma once

namespace CppComponent1
{
    public ref class WindowsPhoneRuntimeComponent sealed
    {
    public:
        WindowsPhoneRuntimeComponent();
    };
}

If I change CppComponent1 to CppComponent2 in the .h and the .cpp, and then try to instantiate a WindowsPhoneRuntimeComponent object in my C# code, I get the following error:

A first chance exception of type 'System.TypeLoadException' occurred in Unknown Module.
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in Microsoft.Phone.ni.dll

How can I change the namespace of a native module in a WP8 app? Thanks!

Woolley answered 14/1, 2013 at 3:19 Comment(0)
H
6

The name of the Windows Metadata (WinMD) file that declares the component must be a prefix of the namespace in which the public types are declared. (I provided a slightly more detailed explanation of the namespace rules in an answer to another question.)

If you rename the namespace from CppComponent1 to CppComponent2, you also need to rename the WinMD file produced by the build from CppComponent1.winmd to CppComponent2.winmd.

Hudis answered 14/1, 2013 at 4:40 Comment(3)
I can't find the setting to change the name of the .winmd file. Indeed, I can't find a mention of a .winmd file at all. Would I need to go in and manually edit files, or is there a way to set this within the visual studio GUI?Woolley
In the project properties, under Linker -> Windows Metadata, see the Windows Metadata File property. It defaults to $(OutDir)$(RootNamespace).winmd. I don't see where $(RootNamespace) is defined; it may be defined in the targets. If I recall correctly, it defaults to the name of the ($(ProjectName)).Hudis
Great! You can change the $(RootNamespace) macro by clicking on your C++ project, hitting ALT-ENTER to open up the "Properties" window (NOT the same "Properties" as right-clicking on the project and clicking "Properties") and changing the value of "Root Namespace"! You may want to update your answer with this info so other people searching for this can find it.Woolley

© 2022 - 2024 — McMap. All rights reserved.