managed type or function cannot be used in an unmanaged function

G

Guest

I am switching from VS2003 .NET to VS2005 and I receive the following
errorwhen compiling my project:

error C3821: 'File': managed type or function cannot be used in an unmanaged
function c:\...\DataBase.cpp

I looked at the knowledgebase:
http://msdn2.microsoft.com/en-us/library/4756a450.aspx and tried to fix the
problem from there but with little success.

Can anyone make a suggestion how to overcom this problem?

File: Database.cpp

int CSendDoc::DbOpenIMDOFile (String * File)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
int nDotPos;
CString sTextFileName;
CString sIMDOFileName;
char * st,st2[100];
int l,x;
int DbCt;
CFileStatus status;
CFile * pScript;

CString FileName;

---> FileName = File;
// open the script file specified by the user
if (!CFile::GetStatus(FileName, status))
{
return 0;
}

try
{
pScript = new CFile(FileName,CFile::modeRead | CFile::shareDenyWrite);
}
catch( CFileException* e )
{
CString sError = "IMDO to text file input file: " + sIMDOFileName;
GetErrorText (sError, e->m_lOsError);
e->Delete();
//return SCRIPT_FILE_OPEN_FAILED;
}

CArchive ar(pScript,CArchive::load);
// read in the script file contents
ASSERT( DatabaseIMDOList.IsSerializable() );

DatabaseIMDOList.Serialize(ar);

ar.Close();
delete pScript;

if(!DatabaseIMDOList.IsEmpty())
{
DbCt = DatabaseIMDOList.GetCount();
DbPos = DatabaseIMDOList.GetHeadPosition();
}
else
DbCt = 0;
return DbCt;
}
 
G

Guest

This could be beacuse File is a class in the .Net framework, part of the
System::IO namespace. I'd suggest you rename the variable and see if that
helps.

Colin
 
H

Holger Grund

RedJoy said:
I am switching from VS2003 .NET to VS2005 and I receive the following
errorwhen compiling my project:

error C3821: 'File': managed type or function cannot be used in an
unmanaged
function c:\...\DataBase.cpp
As I understand, you get this diagnostic when you use managed
functionality in the function's body. In that case, MSIL must be
emitted for the function. For some reason, however, managed
code is not an option for the function.

The String^ to CString conversion requires managed code
in your case.

That could be due to:
#pragma unmanaged or
inline assembler or
?

I don't see the later in your code. Can you try to add a
#pragma managed
above the function?

I don't see though, why this should be different in VC 7.1.

-hg
 
G

Guest

Thank you Colin. I have tried that before and it did not solve my problem.

Colin Desmond said:
This could be beacuse File is a class in the .Net framework, part of the
System::IO namespace. I'd suggest you rename the variable and see if that
helps.

Colin

RedJoy said:
I am switching from VS2003 .NET to VS2005 and I receive the following
errorwhen compiling my project:

error C3821: 'File': managed type or function cannot be used in an unmanaged
function c:\...\DataBase.cpp

I looked at the knowledgebase:
http://msdn2.microsoft.com/en-us/library/4756a450.aspx and tried to fix the
problem from there but with little success.

Can anyone make a suggestion how to overcom this problem?

File: Database.cpp

int CSendDoc::DbOpenIMDOFile (String * File)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
int nDotPos;
CString sTextFileName;
CString sIMDOFileName;
char * st,st2[100];
int l,x;
int DbCt;
CFileStatus status;
CFile * pScript;

CString FileName;

---> FileName = File;
// open the script file specified by the user
if (!CFile::GetStatus(FileName, status))
{
return 0;
}

try
{
pScript = new CFile(FileName,CFile::modeRead | CFile::shareDenyWrite);
}
catch( CFileException* e )
{
CString sError = "IMDO to text file input file: " + sIMDOFileName;
GetErrorText (sError, e->m_lOsError);
e->Delete();
//return SCRIPT_FILE_OPEN_FAILED;
}

CArchive ar(pScript,CArchive::load);
// read in the script file contents
ASSERT( DatabaseIMDOList.IsSerializable() );

DatabaseIMDOList.Serialize(ar);

ar.Close();
delete pScript;

if(!DatabaseIMDOList.IsEmpty())
{
DbCt = DatabaseIMDOList.GetCount();
DbPos = DatabaseIMDOList.GetHeadPosition();
}
else
DbCt = 0;
return DbCt;
}
 
G

Guest

Holger,

I tried the '#pragma managed' above the function but ended up putting it at
the beginning of my *.h file instead and it compiled without any warnings or
errors.


Danke,
Michael
 
Top