C# namespace not recognized by C++ via COM - c++

I currently have a C# class library as such
namespace Library
{
[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("aa950e58-7c6e-4818-8fc9-adecbc7a8f14")]
public interface IQuote
{
........
}
}
I then register it using the following
RegAsm.exe C:\MyFile.dll /tlb:MyFile.tlb /codebase
Now the problem in my C++ application is that
#import "c:\\MyFile.tlb" raw_interfaces_only
using namespace Library;
and then I get the message
Error 7 error C2653: 'Library' : is not a class or namespace name
Any suggestions on how to resolve this issue ?

COM itself does not have a concept of namespace (which is language-specific).
RegAsm.exe uses the assembly name as the name of the library statement in the IDL. It does not care which namespace your COMVisible class is in.
By default the C++ compiler imports a type library into a namespace with its name specified by the library statement in the IDL. Use the assembly name as the namespace name (or rename the namespace in the import statement) in your C++ code.

Took 2 hours, to figure this out.
Sample code is here:
#import "path\to\MyLibrary.tlb" // DEFAULT assembly name is namespace
namespace MyNameSpace = MyLibrary; // make alias back to your namespace
MyNameSpace::IMyClass1Ptr obj; // use namespace here
obj.CreateInstance(__uuidof(MyNameSpace::MyClass1)); // access class in my namespace
Hope that helps.

Related

namespace pqxx not recognized in my c++ project

I have a project in c++. I have successfully installed pqxx package using vcpkg but still namespace 'pqxx' is not recognized,
#include<pqxx/pqxx> //No errors
using namespace pqxx; //Error, 'name must be a namespace name'

two qt ui header file namespace issue

I have two ui header - ui_FlowChartEditor.h and ui_Notepad.h
There was a problem with this codes :
//ui_FlowChartEditor.h
namespace Ui {
class FlowChartEditorClass: public Ui_FlowChartEditorClass {};
} // namespace Ui
//ui_Notepad.h
namespace Ui {
class NotepadClass: public Ui_NotepadClass {};
} // namespace Ui
'Ui' namespace was duplicated, so I solve this problem by changing name of namespace.
like this :
//ui_FlowChartEditor.h
namespace Ui_FlowChartEditor {
class FlowChartEditorClass: public Ui_FlowChartEditorClass {};
} // namespace Ui
//ui_Notepad.h
namespace Ui_Notepad {
class NotepadClass: public Ui_NotepadClass {};
} // namespace Ui
It works at Debug configuration, but not at Release configuration.
Compiler says : [error C2653: 'Ui_FlowChartEditor': is not a class or namespace name (compiling source file FlowChartEditor.cpp)], etc..
Is there any solution to solve this problem?
Thanks in advance.
With #zgyarmati's solution, I solved this problem.
I changed objectName in Qt Designer.
ui_ header file became like this:
namespace FlowChartEditorUi {
namespace Ui {
class FlowChartEditorClass : public Ui_FlowChartEditorClass {};
} // namespace Ui
} // namespace FlowChartEditorUi
Thx.
The ui_*.h files are regenerated by uic from your forms (.ui) files. You changed the generated ui_*.h files only for the debug build, but not for the release build, so you have the pristine, uic-generated files there.
You could change the files also for the release build, but
you are not supposed to change the generated files, as they will be overwritten.
To set a namespace for your ui class, when you are creating a Designer Form Class in QtCreator, specify a name with a namespace, e.g. Ui_Notepad::NotepadClass in the "Class name" field, and the generated ui_*.h file will contain that namespace.
If you already have your forms, then you can also change the "objectName" property for them accordingly in the QtCreator form editor.

error in visual c++ code about namespace

I am just a beginner in C++. I have errors with namespaces in Visual C++. Here is the code which include files I have to add for this foundation. The error says the name must be a namespace name. I add whole code is source folder. There is not single file in header. Wow many files do I have to add?
using namespace System;
int main()
{
if (Environment::HasShutdownStarted)
Console::WriteLine("Shutting down.");
else
Console::WriteLine("Not shutting down.");
return 0;
}
You should add this:
#using <mscorlib.dll>
If you want to use System namespace. Hope this helps.. :)
Try adding this:
#using <mscorlib.dll>
It is used to let the compiler know you are working with the system.

IAR Workbench can't find std::string

I'm trying to compile CppUTest as a library on IAR Workbench v6.3.3 for the AVR UC3C0512C on Windows 7 x64 but when I compile it, it says that the std namespace is not defined.
Here is the snippet of code where I get the first error, the file is SimpleString.h:
#if CPPUTEST_USE_STD_CPP_LIB
#include <string>
#include <stdint.h>
SimpleString StringFrom(const std::string& other);
The last line contains the std::string and this brings me 190 errors all related to this. The message is:
Error[Pe276]: name followed by "::" must be a class or namespace name
C:\COM\SRC\cpputest35\include\CppUTest\SimpleString.h 143
I have tried using the line below but it does not help:
using namespace std;
Under Library Configuration I select Normal DLIB, I also tried with Full DLIB but IAR can't see the std library
Any ideas please?
According to the IAR manuals
The std namespace is not used in either standard EC++ or in Extended EC++. If you
have code that refers to symbols in the std namespace, simply define std as nothing;
for example:
#define std // Nothing here

Native dll built with /CLR flag containing a managed class

I have a old native MFC/c++ dll I have managed to get it compiled with /CLR flag.
Now I have added a managed C++/CLI class to the dll within a namaspace.
The header file is below, and the cpp file only has #include for the header file.
The native dll is a huge dll project with lot of un managed code, but it has only one managed c++ class like below.
When I add that dll as a reference to a .net winforms project I don't see that namespace / class, in the object browser,
and I get compile error for not finding the namespace "ShashiTest"
I am using Visual studio 2008.
Native dlls in mixed mode can not be added as reference to a managed project ?
Or am I missing something
Please help.
#pragma once
#using<mscorlib.dll>
#using<system.windows.forms.dll>
// Class derived from Forms
using namespace System::Windows::Forms;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Windows::Forms;
namespace ShashiTest {
/// <summary>
/// Summary for test
/// </summary>
public ref class test
{
public:
test(void)
{
};
void ShowMessage()
{
MessageBox::Show("Hello World");
}
};
}
When I simplified my problem..I created a fresh MFC dll and added a manged C++ class to it (same class as above) . Compiled with /CLR flag.
When I add this dll to winforms project and run it i get
System.BadImageFormatException. Any clue ?
However I see the class and the name space and winform project compiles fine unlike the problem I have the above.
System.BadImageFormatException is usually caused by having an AnyCPU .NET application reference a DLL containing 32-bit native code. You get an error when running on a 64-bit platform, because the AnyCPU application runs as 64-bit, and can't load the DLL. The fix for this is (easy) to mark the application as x86-only or (hard) to provide both 32-bit and 64-bit versions of all DLLs containing native code.
Of course, you could have some other problem. Checking your DLL with Red Gate Reflector as suggested by #cdleonard in the comments is a great next step. Or ILSpy, which is free.