473,499 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cannot marshall parameter error on call to win32 api - FormatMessage

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 ;
}

Mar 5 '07 #1
4 5249
Steve Richter wrote:
Cannot marshal 'parameter #7': Pointers cannot reference marshaled
structures. Use ByRef instead.
You must have a name collision. You're probably not calling the
unmanaged Win32 FormatMessage, but another function that happens to have
the same name.

Your code compiles for me without any errors (VC++2005 SP1). Just try to
put it into an empty unit, like this:

// FormatMessage.cpp
#include "stdafx.h"
#include <windows.h>

using namespace System;

String^ FormatMessage( DWORD rc )
<snip// your code unchanged

If that still doesn't work, remove #include "stdafx.h" and disable
precompiled headers for that unit.

Tom
Mar 5 '07 #2
On Mar 5, 5:30 pm, Tamas Demjen <tdem...@yahoo.comwrote:
Steve Richter wrote:
Cannot marshal 'parameter #7': Pointers cannot reference marshaled
structures. Use ByRef instead.

You must have a name collision. You're probably not calling the
unmanaged Win32 FormatMessage, but another function that happens to have
the same name.

Your code compiles for me without any errors (VC++2005 SP1). Just try to
put it into an empty unit, like this:

// FormatMessage.cpp
#include "stdafx.h"
#include <windows.h>

using namespace System;

String^ FormatMessage( DWORD rc )
<snip// your code unchanged

If that still doesn't work, remove #include "stdafx.h" and disable
precompiled headers for that unit.

Tom
thanks for the help. The solution ended up being a change in the
project from "pure MSIL CLR support" to what I guess is a more
flexible "clr support".

now I am reading in the archives that I cant call my C++ code from C#
unless the c++ is pure MSIL? The FormatMessage API bombed on the last
argument, a VARARG or whatever it is called. How do I know what
unmanaged code can be included in a "pure MSIL" project and what cant?

-Steve
Mar 6 '07 #3

"Steve Richter" <St************@gmail.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
On Mar 5, 5:30 pm, Tamas Demjen <tdem...@yahoo.comwrote:
>Steve Richter wrote:
Cannot marshal 'parameter #7': Pointers cannot reference marshaled
structures. Use ByRef instead.

You must have a name collision. You're probably not calling the
unmanaged Win32 FormatMessage, but another function that happens to have
the same name.

Your code compiles for me without any errors (VC++2005 SP1). Just try to
put it into an empty unit, like this:

// FormatMessage.cpp
#include "stdafx.h"
#include <windows.h>

using namespace System;

String^ FormatMessage( DWORD rc )
<snip// your code unchanged

If that still doesn't work, remove #include "stdafx.h" and disable
precompiled headers for that unit.

Tom

thanks for the help. The solution ended up being a change in the
project from "pure MSIL CLR support" to what I guess is a more
flexible "clr support".

now I am reading in the archives that I cant call my C++ code from C#
unless the c++ is pure MSIL? The FormatMessage API bombed on the last
That is most assuredly false. MSIL vs native code does have implications
for security (native code cannot run without full trust), but C# can call
into mixed assemblies just fine.
argument, a VARARG or whatever it is called. How do I know what
unmanaged code can be included in a "pure MSIL" project and what cant?
In pure MSIL, every API call has to be converted to a DllImport statement.
If there's no conversion, it won't work.

In the case of FormatMessage, it's not that MSIL can't make the call, it's
that the compiler hasn't enough information to construct the P/Invoke
signature. However, you could do that yourself, just as doing P/Invoke from
C# (http://www.pinvoke.net/default.aspx/...atMessage.html)
>
-Steve


Mar 6 '07 #4
>In the case of FormatMessage, it's not that MSIL can't make the call, it's
>that the compiler hasn't enough information to construct the P/Invoke
signature. However, you could do that yourself, just as doing P/Invoke from
C# (http://www.pinvoke.net/default.aspx/...atMessage.html)
Or take advantage of the fact that the Win32Exception class calls
FormatMessage internally. Something like this should do it I think

String^ FormatMessage( DWORD rc )
{
Win32Exception^ ex = gcnew Win32Exception((int)rc);
return ex->Message;
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
16020
by: John Smith | last post by:
What's wrong with the use of atoi in the following code? Why do I get the error message: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' char cBuffer; void...
1
3718
by: Gary | last post by:
I have a strange compile error. C2664 cannot convert parameter 2 from int to int... Earlier in my code I was setting up my dataset... I add an int field like so... ...
2
10926
by: Steve Wilkinson | last post by:
I've just started using managed C++ with VS2005, so please forgive my ignorance. I'm investigating producing a managed wrapper for some functionality of the Windows Media Format SDK. I have the...
1
3001
by: DukeRock | last post by:
This is the function I'm getting the error on: S32 GuiAviBitmapCtrl::fileOpen() { S32 rval; if (!dStrcmp(mAviFilename,"")) return MOVERR_NOVIDEOSTREAM; rval =...
1
2870
by: stillh2os | last post by:
Hello. I'm new to .NET, and I'm trying to implement a callback function. I want my managed C++ code to call an unmanaged function, passing in a callback function that the unmanaged C/C++ code...
8
6232
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get...
2
9141
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
1
3413
by: td0g03 | last post by:
I have no idea why am I getting an error C2664: 'parseInput' : cannot convert parameter 2 from 'main::WORD_STRUCT ' to 'struct WORD_STRUCT ' #include <stdio.h> #include <stdlib.h> #include...
1
1701
by: Khaled Mohamed | last post by:
Hi guys, I'm trying to setup apache v2.2.17 win 32, php v5.2.17 Win32 VC6 and mysql v5.5.10 win 32 on a winxp sp3 computer. Up till now php is working with well with apache as I write in a .php...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.