C++ expected a type specifier (Visual Studio) - c++-cli

I have tried many things. please help me.....
Full Source Code:
Source Code
Troubled Code:
MySqlDataAdapter adapter;
DataTable table;
private: System::Void AcceptedApplications_Load(System::Object^ sender,
System::EventArgs^ e) {
MySqlCommand^ com = gcnew MySqlCommand("command", conn);
adapter = new MySqlAdapter(com);
}`

Standard warning: While it's certainly possible to write the main body of your application in C++/CLI, or even write the GUI in C++/CLI using WinForms, it is not recommended. C++/CLI is intended for interop scenarios: where C# or other .Net code needs to interface with unmanaged C++, C++/CLI can provide the translation between the two. Because of that, C++/CLI has all of the complexities of C++, all of the complexities of C#, and some complexities of its own. For primary development, it is recommended to use C# with either WinForms or WPF if you want managed code, or C++ with MFC if you want unmanaged.
MySqlDataAdapter adapter;
...
adapter = new MySqlAdapter(com);
A couple issues:
It looks like you forgot the word "Data" when creating the object.
According to MySQL's reference site, that's a managed class. Therefore, it should have a ^ at the declaration and gcnew when instantiated.

public ref class types generally need the ^ in C++/cli and gcnew.
MySqlDataAdapter^ adapter = gcnew MySqlDataAdapter();
DataTable^ table = gcnew DataTable();

Related

What is the difference between gcnew and ref new

I just have stumbled upon ref new here and am wondering whether there is a difference to gcnew.
The code example here shows how a handle to an object on the managed heap is being created using ref new:
Foo^ spFoo = ref new Foo();
and using gcnew here
int ^ i = gcnew int(13);
So are they conceptually equivalent?
In case you are simply confused by the layout of the documentation:
C++/CLI is for the Common Language Runtime (CLR).
C++/CX is for the Windows Runtime (WinRT).
They are similar enough to be documented together but they are not the same.
That's why they have three headings in the documentation that you linked:
All Platforms
Windows Runtime
Common Language Runtime
For C++/CLI, gcnew is what you use. Unless you are using C++/CX, the "Windows Runtime" sections don't apply.

New to coding and having trouble with "identifier "string" is undefined" error

#include "MyForm.h"
using namespace System::Windows::Forms;
[STAThread];
void main(array<string^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Organizer::MyForm form;
Application::Run(%form);
}
`
I keep getting the error "identifier "string" is undefined"
how can i fix this? im new to coding and have minimal knowledge, any help is appreciated
The .Net managed System::String class starts with a capital "S".
You'll also need the namespace System:: or a using namespace System; directive at the top of the file. (Which might already be there, and you just didn't copy it to the web.)
Now, that said: If you're new to coding, please do yourself a favor and don't start with C++/CLI. Either start with C#, or with plain C++. C++/CLI is intended for .Net managed code to interact with unmanaged C/C++ code. It has all the complexities of C#, C++, and a few of its own, and is therefore not a good language to learn with.

Pass objects from referenced managed dll to a C++/CLI wrapper

So, I'm a writing a C++/CLI wrapper and I would like it to interact with objects from a referenced, managed (C#) dll. More specifically, in my C++/CLI project, I would like to have classes like this:
public ref class MyClass
{
public:
void InputPoints(array<Point3d^>^ ptsIn);
};
Where, "Point3d" is an object from the referenced, managed dll - call it "MyGeometryDLL.dll". This compiles without any problems, however when I use the wrapper in a C# project (that also references "MyGeometryDLL.dll") , I can't pass Point3d directly to the wrapper, i.e., intellisense detects:
void MyClass.InputPoints(ValueType[] ptsIn);
How would I pass Point3d directly to the wrapper?
So class Point3d is already defined in a C# DLL, correct? In that case, I think it may be an issue of carats.
If it's declared in C# as public struct Point3d, then it's a value type, not a reference type. That means that whenever you use it in C++/CLI, you don't use the ^, and you don't call gcnew. If that's the case, then you need to change the method declaration to this:
void InputPoints(array<Point3d>^ ptsIn); // No carat on Point3d
The reason that C# sees this as ValueType[] is because Point3d^ is a legal type in C++/CLI, but it doesn't exist in C#. A general ValueType is the closest thing, so that's what C# sees.

How to dispose/delete managed object from C++/CLI

I'm working on a native C++ project (/clr enabled) that must use a couple of managed, COM visible C# DLLs. Some of the managed objects implement IDisposable and I would like to call Dispose() on them. How can I do that?
The code looks something like this:
HRESULT hr = CoInitialize(NULL);
IManagedClassPtr pIObj(__uuidof(ManagedClass));
//do stuff with pIObj
...
//dispose of pIObj somehow
...
CoUninitialize();
First of all, if you don't have to, just don't use COM.
C++/CLI is designed to give an easy interface between C++ and .Net languages.
If you want to create a .Net object in C++/CLI you just use gcnew with a reference variable.
.Net classes that have a Dispose() method will have it in C++/CLI too. The difference is that if you declare a ref class (.Net reference class) in C++/CLI then the ~destructor turns into a Dispose method.
The !finalizer is what's actually called by the GC.
So anyway, if you create a .Net object with a Dispose() method, you would be able to do this:
MyDisposable^ m = gcnew MyDisposable();
m->Dispose();

.NET COM Interop with references to other libraries

I'm up against a problem when loading a class in a managed library from a COM Interop library.
basically I have some Unmanaged C++ code and a COM Interop library written in C#. And finally a 3rd library which is referenced by the COM Interop library which contains a class:
public class MyClass{
public MyClass(){}
}
What I'd like to do is from my unmanaged c++ code, call a function in the Interop library
The C++ code doesn't need to know of the existence of the third library, it's only used within the Interop.
Init(){
MyClass _class = new MyClass();
}
for some reason this line in Init fails "MyClass _class = new MyClass();", and I don't get very usefull error messages, all I have to go on is a few of these in my output window:
"First-chance exception at 0x7c812afb in DotNet_Com_Call.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.."
and the HRESULT of the "HRESULT hr = pDotNetCOMPtr->Init();" line in my C++ code is "The system cannot find the specified file"
I'm new to COM so if anyone has any ideas or pointer to get me going the right direction, I'd appreciate it,
Thanks
Nevermind, I think I just figured it out on my own. It looks like the Interop DLL and the third DLL referenced by the interop need to be located in the same directory.