S
Steve Richter
I have a C++ forms project that I am adding some unmanaged code to. I
have a member function of the Form1 class that returns a String^
holding the text of the last win32 error. The code is failing on the
call to FormatMessage.
Cannot marshal 'parameter #7': Pointers cannot reference marshaled
structures. Use ByRef instead.
how do I pass the argument ByRef and why is managed code involved in
this api call? FormatMessage is an unmanaged api and all of the
variables are unmanaged.
thanks,
String^ FormatMessage( DWORD rc )
{
String^ sMsg ;
LPVOID pMsgBuf = NULL ;
DWORD nRc ;
nRc = ::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, rc, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&pMsgBuf, 0, NULL ) ;
if ( pMsgBuf == NULL )
sMsg = L"Unknown return code " ;
else
{
sMsg = gcnew String( (wchar_t*) pMsgBuf ) ;
}
if ( pMsgBuf != NULL )
LocalFree( pMsgBuf ) ;
return sMsg ;
}
have a member function of the Form1 class that returns a String^
holding the text of the last win32 error. The code is failing on the
call to FormatMessage.
Cannot marshal 'parameter #7': Pointers cannot reference marshaled
structures. Use ByRef instead.
how do I pass the argument ByRef and why is managed code involved in
this api call? FormatMessage is an unmanaged api and all of the
variables are unmanaged.
thanks,
String^ FormatMessage( DWORD rc )
{
String^ sMsg ;
LPVOID pMsgBuf = NULL ;
DWORD nRc ;
nRc = ::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, rc, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&pMsgBuf, 0, NULL ) ;
if ( pMsgBuf == NULL )
sMsg = L"Unknown return code " ;
else
{
sMsg = gcnew String( (wchar_t*) pMsgBuf ) ;
}
if ( pMsgBuf != NULL )
LocalFree( pMsgBuf ) ;
return sMsg ;
}