I just started working with C++ yesterday and I’m stuck on what I suspect is a trivial problem. The main DLL class is generating error C2653: ‘Marshal’: is not a class or namespace name.
I’m pretty sure I’ve copied the syntax almost verbatim from the MSDN page on using this class. Here is the code:
// VideoInfoAssembly.cpp
// compile with: /clr
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;
#include "stdafx.h"
#include "VideoInfoAssembly.h"
#pragma managed
namespace VideoInfoAssembly
{
short VideoInfo::GetWidth(System::String^ managedString)
{
// Marshal the managed string to unmanaged memory.
char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(managedString).ToPointer();
// Always free the unmanaged string.
Marshal::FreeHGlobal(IntPtr(stringPointer));
return 9001;
}
}
At a high level, I have a C# app that needs to read the video properties of video files and the Microsoft Media Foundation requires C++. I’ve successfully built the DLL and had it return over 9000 as a test. Now, I’m trying to pass a managed string to the DLL (which will be video file name); however, I’ve been struggling for over an hour to get the Marshal class to function. As stated earlier, I’ve been using the MSDN page without success. What am I missing?
This is a problem induced by pre-compiled headers. The compiler will skip everything until it finds the #include for the precompiled header file. The mistake you made is not putting
#include "stdafx.h"at the top of the file. So fix it like this: