Pointer to array of strings
- From: Boon <Boon@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 10 Dec 2008 02:32:03 -0800
I am trying to implement the API cal lto RecordEvent where it should be
possible to pass pointer to an array of strings to one of the parameters so
the API call can actually write more than 32k of text by having 32k in each
string in the array.
I have seen RecordEvent defined as string [] lpStrings and following as
IntPtr lpStrings. Each case will only write the first string i nthe array.
We have used a Delphi com dll i nthe past to write this large amount of data.
Can anyone help in showing how to get the pointer to the array of strngs
needed for the API call or how to define the API call differently to use an
array of strings.
Thanks
Boon
public partial class Form1 : Form
{
#region Imports for Event logging
[DllImport("advapi32.dll", SetLastError = true)]
static extern IntPtr RegisterEventSource(string machine, string
source);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool DeregisterEventSource(IntPtr handle);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool ReportEvent(IntPtr hHandle,
ushort wType, ushort wCategory, uint dwEventID, IntPtr uSid,
ushort wStrings, uint dwDataSize, IntPtr lpStrings, byte bData);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool GetTokenInformation(IntPtr handle,
TOKEN_INFORMATION_CLASS token, IntPtr uSid, uint size, out uint tokenSize);
enum TOKEN_INFORMATION_CLASS
{
TokenUser = 1,
TokenGroups,
TokenPrivileges,
TokenOwner,
TokenPrimaryGroup,
TokenDefaultDacl,
TokenSource,
TokenType,
TokenImpersonationLevel,
TokenStatistics,
TokenRestrictedSids,
TokenSessionId,
TokenGroupsAndPrivileges,
TokenSessionReference,
TokenSandBoxInert,
TokenAuditPolicy,
TokenOrigin
}
struct TOKEN_USER
{
public SID_AND_ATTRIBUTES User;
}
struct SID_AND_ATTRIBUTES
{
public IntPtr Sid;
public int Attributes;
}
struct PP
{
public IntPtr p1;
public IntPtr p2;
}
#endregion
public Form1()
{
InitializeComponent();
if (!EventLog.SourceExists("TestApplication"))
EventLog.CreateEventSource("TestApplication", "Application");
}
private void Form1_Load(object sender, EventArgs e)
{
LogEvent();
}
private void LogEvent()
{
IntPtr eventSrcHandle = RegisterEventSource(null,
"TestApplication");
uint tokenInfoSize = 0;
IntPtr userTokenPtr = WindowsIdentity.GetCurrent().Token;
//using this first call, get the length required to hold the
token information in the tokenInfoSize parameter
bool bresult = GetTokenInformation(userTokenPtr,
TOKEN_INFORMATION_CLASS.TokenUser, IntPtr.Zero, tokenInfoSize, out
tokenInfoSize);
int error = Marshal.GetLastWin32Error();
IntPtr userTokenInfo = Marshal.AllocHGlobal((int)tokenInfoSize);
//get the user token now with the pointer allocated to the right
size
bresult = GetTokenInformation(userTokenPtr,
TOKEN_INFORMATION_CLASS.TokenUser, userTokenInfo, tokenInfoSize, out
tokenInfoSize);
if (bresult)
{
TOKEN_USER tokUser =
(TOKEN_USER)Marshal.PtrToStructure(userTokenInfo, typeof(TOKEN_USER));
string[] message = new string[10];
message[0] = "Message 1 Logged at - " +
DateTime.Now.ToString();
message[1] = "Message 2 Logged at - " +
DateTime.Now.ToString();
message[2] = "Message 3 Logged at - " +
DateTime.Now.ToString();
message[3] = "Message 4 Logged at - " +
DateTime.Now.ToString();
message[4] = "Message 5 Logged at - " +
DateTime.Now.ToString();
message[5] = "Message 6 Logged at - " +
DateTime.Now.ToString();
message[6] = "Message 7 Logged at - " +
DateTime.Now.ToString();
message[7] = "Message 8 Logged at - " +
DateTime.Now.ToString();
message[8] = "Message 9 Logged at - " +
DateTime.Now.ToString();
message[9] = "Message 10 Logged at - " +
DateTime.Now.ToString();
IntPtr[] p = new IntPtr[2];
p[0] = Marshal.StringToHGlobalAnsi(message[0]);
p[1] = Marshal.StringToHGlobalAnsi(message[1]);
PP pp;
pp.p1 = p[0];
pp.p2 = p[1];
IntPtr q = Marshal.AllocHGlobal(IntPtr.Size);
Marshal.StructureToPtr(pp, q, false);
bresult = ReportEvent(eventSrcHandle, 0, 0, 0,
tokUser.User.Sid, 2, 0, q, new byte());
}
//Clean up
DeregisterEventSource(eventSrcHandle);
Marshal.FreeHGlobal(userTokenInfo);
}
}
.
- Prev by Date: Re: .NET 1.1 and 3.5 can work in the same project
- Next by Date: Re: acess denied
- Previous by thread: .NET 1.1 and 3.5 can work in the same project
- Next by thread: Need Help Understanding How To Add "Groups" Feature to Web Site
- Index(es):
Relevant Pages
|