Calling FormatMessage WITH insertion strings

S

Scott

There are tons of examples of how to call FormatMessage from C# on the
internet, but none of these pass insertions strings to the FormatMessage.

Is there an example of how this should work?

My DllImport currently looks like this. This doesn't seem correct to me
since the 7th parameter is a va_list *. It would be nice to be able to pass
in the insertion strings this way however, right now I will take anything
that works.


[DllImport( "kernel32.dll", CharSet=CharSet.Auto )]
private static extern int FormatMessageW(
uint dwFormatFlags, IntPtr lpSource, int dwMessageId,
int dwLanguageId, out IntPtr MsgBuffer, int nSize, String []
parameters );


The C# routine is String FmtMsg( uint nMsgID, params String [] params ).

I am able to get the message text back if I set
FORMAT_MESSAGE_IGNORE_INSERTS in the attributes, but I need to get the
message with the inserts.
 
Z

Zhi-Xin Ye [MSFT]

Dear Scott,

You should add the FORMAT_MESSAGE_ARGUMENT_ARRAY flag to make it to work,
for example:

[DllImport("kernel32.dll")]
static extern uint FormatMessage(uint dwFlags, IntPtr lpSource,
uint dwMessageId, uint dwLanguageId, [Out] StringBuilder
lpBuffer,
uint nSize, string[] Arguments);

private void button1_Click(object sender, EventArgs e)
{
const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
const uint FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;

// System error message
const uint ERROR_BAD_EXE_FORMAT = 193;

uint errorCode = ERROR_BAD_EXE_FORMAT;

StringBuilder formattedMessage = new StringBuilder(100);
string[] args = new string[] { "aa" };

uint dwChars = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ARGUMENT_ARRAY,
IntPtr.Zero,
errorCode,
0, // Default language
formattedMessage,
100,
args);

MessageBox.Show(formattedMessage.ToString());
}

Should you have any questions, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top