Visual C++ - Throwing unhandled exception from setting forms icon?
I can compile the solution with no errors, but when I'll try to run it, I get a crash window:
An unhandled except开发者_开发知识库ion of type
'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll
Additional information: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "<myformname>.resources" was corerctly embedded or linked into assembly "<myprojectname>" at compile time, or that all the satellite assemblies required are loaded and fully signed.
And after I press Break it throws me to the line:
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
If I comment this line out, everything works just fine, but my program doesn't have icon.
Anyone else had this problem? Found a solution? I couldn't find anything clear enough for me to understand, problem is really annoying me, only solution I found was to declare my form class before any other classes, but I don't even have any other classes in my solution?
I also have only one project in this solution, ms support said something about having multiple projects, which I don't have, so it was no use either.
Take a look here :
http://www.mztools.com/articles/2005/MZ2005007.aspx
The exception is thrown because your icon cannot be located. You will probably need to compiles your resources under one .dll and put this under en-US subfolder on your project output. It did the trick for me at least. There are probably other solutions to your problem too.
Do not panic like I did. The root cause of the problem is that the compiled resource file is different from the one that is asked to load at runtime. This happens because the underlying build-script cannot detect the filename or namespace changes made after the form is created.
For example, At first we started a project named x
. And our $(RootNamespace)
becomes x
. And we created a form named y
. So our XML resource file y.resx
gets compiled into x.y.resource
. At this point the icon change works.
Now somehow we changed the project name or the namespace to z
. But our $(RootNamespace)
remains the x
. While at compile-time it wrongly generates old x.y.resource
, but at links-time it links z.y.resource
. And at this point the icon change does not work.
It can also happen if the form is under some nested namespace which is not known in the project file.
It can be fixed by changing the compilation output of the y.resx
file . It can be done by right-clicking the resource and changing the Resource Logical Name
to $(RootNamespace).%(Filename).resources
.
I will also make sure that ProjectName,AssemblyName and RootNamespace are the same in the .vcxproj
file. Somehow if the form is declared under a nested namespace like RootNamespace.gui
, then the output file of the resource should be $(RootNamespace).gui.%(Filename).resources
.
精彩评论