473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

FormatMessage with parameters in C#

i'm trying to call FormatMessage and supply some arguments for itto substitute into the message string. i can get it to grab themessage, but i can't convince it to substitute the arguments.

i've got the arguments in an ArrayList (though i started withthem in a byte array along the lines of a safe array (numparams,len p1, p1, len p2, p2, ...)). the arguments are always string,but the number of arguments varies from call to call. i'mpretty sure it's just a matter of getting it to marshalcorrectly, but so far i haven't guessed the magic format.

i currently have FormatMessage defined as:

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern int FormatMessage(
int dwFlags,
IntPtr lpSource,
int dwMessageId,
int dwLanguageId,
out IntPtr MsgBuffer,
int nSize,
[MarshalAs(UnmanagedType.SafeArray,SafeArraySubType =VarEnum.VT_BSTR)] System.Collections.ArrayListArguments
);

//i munge the byte array into an ArrayList
System.Collections.ArrayList paramList =BuildParamList(parameters, parameterSize);

int dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM ;

dwFormatFlags |= (parameterSize == 0) ?FORMAT_MESSAGE_IGNORE_INSERTS : FORMAT_MESSAGE_ARGUMENT_ARRAY;

//i successfully load my message library as hModule usingLoadLibraryEx

IntPtr pMessageBuffer;
int msgid = //whatever message id i've been passed

dwBufferLength = FormatMessage(dwFormatFlags,
hModule,
msgid,
0,
out pMessageBuffer,
8096,
paramList);

when it hits the FormatMessage, i get an error that says: Cannot marshal parameter #7: The type definition of this type hasno layout information.

i've played around with a few different flavors of the MarshalAs- so far with no luck.

any hints would be appreciated. my backup plan just is tosubstitute the parameters myself. but it would be cool if icould get FormatMessage to do it for me.

--------------------------------
From: Andy Robinson

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>/40O8HCWxEeCHPbj7ZTlMQ==</Id>
Nov 16 '05 #1
1 8083
Andy,
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern int FormatMessage(
int dwFlags,
IntPtr lpSource,
int dwMessageId,
int dwLanguageId,
out IntPtr MsgBuffer,
int nSize,
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_BSTR)] System.Collections.ArrayList Arguments
);


Using an ArrayList won't work, try replacing it with a string array
(and remove the MarshalAs attribute).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

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

Similar topics

7
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
2
by: Hai Ly Hoang [MT00KSTN] | last post by:
CString.FormatMessage and CString.Format has the same parameters and they seems to have to outcome. However, they coexist !. There must be some subtle different between they. Can you tell me the...
4
by: Egbert Nierop \(MVP for IIS\) | last post by:
Hi, I have a function that has a variable no of arguments (note! function shortened) myfunction(int someID, ...) {
4
by: Tim::.. | last post by:
Can someone tell me a better way or give me a link that shows a better way to create large numbers of SQL parameters... Example... A better way to write this code! <code> Sub...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
2
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
4
by: Steve Richter | last post by:
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...
2
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
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? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.