msclr is not being used

1.3k views Asked by At

So for some reason, msclr is not being used at runtime, I think, for some reason. This is the code:

#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>
#include "stdafx.h"
using namespace System;
using namespace msclr::interop;

int main() 
{
const char* message = "Test String to Marshal";
String^ result;
result = marshal_as<String^>(message);
return 0;
}

I get this error: "C2653: 'msclr' : is not a class or namespace name" which doesn't make any sense at all. What boggles my mind is that not just my computer but also any other computer I try using msclr. Furthermore, visual studio is not underlining anything so clearly the IDE recognizes the namespace. Could someone please help me? Obviously the header is positioned correctly as show so that's ot the case as what a lot of sites have stated. Thanks in advance.

1

There are 1 answers

0
Joel Rondeau On BEST ANSWER

When I build it on my machine, the first error that is displayed is the C2653 error that you mention. However, it also displays some warnings that get to the heart of the problem. The first warning I get is:

warning C4627: '#include <stdlib.h>': skipped when looking for precompiled header use.

When using a precompiled header, it is very important that the first line in your .cpp file is

#include "stdafx.h"

Once I make that line the first line of the .cpp file, everything compiles properly.