I'm having a problem building an array of structs from an IntPtr in another struct.
This structure is returned by a Windows API I'm using:
public struct DFS_INFO_9 {
[MarshalAs(UnmanagedType.LPWStr)]
public string EntryPath;
[MarshalAs(UnmanagedType.LPWStr)]
public string Comment;
public DFS_VOLUME_STATE State;
public UInt32 Timeout;
public Guid Guid;
public UInt32 PropertyFlags;
public UInt32 MetadataSize;
public UInt32 SdLengthReserved;
public IntPtr pSecurityDescriptor;
public UInt32 NumberOfStorages;
public IntPtr Storage;
}
[DllImport("netapi32", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int NetDfsEnum([MarshalAs(UnmanagedType.LPWStr)]string DfsName, int Level, int PrefMaxLen, out IntPtr Buffer, [MarshalAs(UnmanagedType.I4)]out int EntriesRead, [MarshalAs(UnmanagedType.I4)]ref int ResumeHandle);
I'm trying to get the array of DFS_STORAGE_INFO_1s referenced by IntPtr Storage.
Here's that structure (if it matters):
public struct DFS_STORAGE_INFO_1 {
public DFS_STORAGE_STATE State;
[MarshalAs(UnmanagedType.LPWStr)]
public string ServerName;
[MarshalAs(UnmanagedType.LPWStr)]
public string ShareName;
public IntPtr TargetPriority;
}
So far this code has been working to get the DFS_INFO_9s with only one storage, but fails when trying to marshal the second item in the array.
DFS_INFO_9 info = GetInfoFromWinApi();
List<DFS_STORAGE_INFO_1> Storages = new List<DFS_STORAGE_INFO_1>();
for (int i = 0; i < info.NumberOfStorages; i++) {
IntPtr pStorage = new IntPtr(info.Storage.ToInt64() + i * Marshal.SizeOf(typeof(DFS_STORAGE_INFO_1)));
DFS_STORAGE_INFO_1 storage = (DFS_STORAGE_INFO_1)Marshal.PtrToStructure(pStorage, typeof(DFS_STORAGE_INFO_1));
Storages.Add(storage);
}
I'm getting a FatalExecutionEngineError that spits out error code 0x0000005 (Access Denied). I'm assuming the size of the DFS_STORAGE_INFO_1 is being miscalculated causing the marshal to attempt to access memory outside that allocated for the array. But this is happening on i = 1, when there might be 7 storages to get through. Maybe my thinking is flawed, but I have no idea how to rectify this.
The actual definition of DFS_STORAGE_INFO_1 is this:
public struct DFS_STORAGE_INFO_1 {
public DFS_STORAGE_STATE State;
[MarshalAs(UnmanagedType.LPWStr)]
public string ServerName;
[MarshalAs(UnmanagedType.LPWStr)]
public string ShareName;
DFS_TARGET_PRIORITY TargetPriority;
}
TargetPriority is a structure, defined here:
public struct DFS_TARGET_PRIORITY {
public DFS_TARGET_PRIORITY_CLASS TargetPriorityClass;
public UInt16 TargetPriorityRank;
public UInt16 Reserved;
}
public enum DFS_TARGET_PRIORITY_CLASS {
DfsInvalidPriorityClass = -1,
DfsSiteCostNormalPriorityClass = 0,
DfsGlobalHighPriorityClass = 1,
DfsSiteCostHighPriorityClass = 2,
DfsSiteCostLowPriorityClass = 3,
DfsGlobalLowPriorityClass = 4
}
As for the FatalExecutionEngineError, I believe that the size of the structure DFS_STORAGE_INFO_1 was being miscalculated since it was defined incorrectly. When trying to convert the pointers to the structures they referenced, the next index was wrong because the size was off. When converting the block of memory, it presumably referenced a block that shouldn't have been accessible, throwing the "Access Denied (0x0000005)" error.
Related
My unit tests crash on this bit of code when running in 64bit.
The crash happens on the Marshal.PtrToStructure call on the 2nd iteration of the loop. The "entriesRead" says 4 so it should be able to read correctly, but it does not. The Marshal.SizeOf(typeof(WinAPI.NETAPI32.USER_INFO_4)) is 192 bytes in 64bit. Is this the source of the error?
....
try {
int entriesRead;
int totalEntries;
int resumeHandle;
var result = WinAPI.NETAPI32.NetUserEnum(
this.NTCompatibleHostName,
3,
2,
out bufPtr,
-1,
out entriesRead,
out totalEntries,
out resumeHandle
);
if (result != 0) {
throw new NetApiException(
result,
"Failed to enumerate local users on host '{0}'",
Host
);
}
var structSize = Marshal.SizeOf(typeof(WinAPI.NETAPI32.USER_INFO_4));
var startAddr = bufPtr.ToInt64();
var endAddr = startAddr + entriesRead * structSize;
for (var offset = startAddr; offset < endAddr; offset += structSize) {
var userInfo =
(WinAPI.NETAPI32.USER_INFO_4)Marshal.PtrToStructure(
new IntPtr(offset),
typeof(WinAPI.NETAPI32.USER_INFO_4)
);
}
} catch (Exception error) {
}
[StructLayout(LayoutKind.Sequential)]
public struct USER_INFO_4 {
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_name;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_password;
public uint usri4_password_age;
public uint usri4_priv;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_home_dir;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_comment;
public uint usri4_flags;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_script_path;
public uint usri4_auth_flags;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_full_name;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_usr_comment;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_parms;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_workstations;
public uint usri4_last_logon;
public uint usri4_last_logoff;
public uint usri4_acct_expires;
public uint usri4_max_storage;
public uint usri4_units_per_week;
public IntPtr usri4_logon_hours;
public uint usri4_bad_pw_count;
public uint usri4_num_logons;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_logon_server;
public uint usri4_country_code;
public uint usri4_code_page;
public IntPtr usri4_user_sid;
public uint usri4_primary_group_id;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_profile;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri4_home_dir_drive;
public uint usri4_password_expired;
}
[DllImport("netapi32.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern NET_API_STATUS NetUserEnum([MarshalAs(UnmanagedType.LPWStr)] string servername, int level, int filter, out IntPtr bufptr, int prefmaxlen, out int entriesread, out int totalentries, out int resume_handle);
The struct is translated correctly. Its size is correct. Your translation of the function call is correct.
The problem is that you are passing level 3. Which means that the function returns USER_INFO_3 rather than USER_INFO_4. The documentation of NetUserEnum makes absolutely no mention of it ever returning USER_INFO_4 values. In order to get USER_INFO_4 values you must call NetUserGetInfo.
Call NetUserEnum passing the server name and a level value of 0. This will enumerate the user names. Then pass each of those user names, along with the server name, to NetUserGetInfo with a level of 4.
I'm calling the Win32 function EnumJobs (http://msdn.microsoft.com/en-us/library/windows/desktop/dd162625(v=vs.85).aspx) from managed code (C#).
[DllImport("Winspool.drv", SetLastError = true, EntryPoint = "EnumJobsA")]
public static extern bool EnumJobs(
IntPtr hPrinter, // handle to printer object
UInt32 FirstJob, // index of first job
UInt32 NoJobs, // number of jobs to enumerate
UInt32 Level, // information level
IntPtr pJob, // job information buffer
UInt32 cbBuf, // size of job information buffer
out UInt32 pcbNeeded, // bytes received or required
out UInt32 pcReturned // number of jobs received
);
EnumJobs(_printerHandle, 0, 99, 1, IntPtr.Zero, 0, out nBytesNeeded, out pcReturned);
I'm specifying Level 1 to receive a JOB_INFO_1 but the problem I'm having is the above function is returning nBytesNeeded as 240 per struct while the Marshal.SizeOf(typeof(JOB_INFO_1)) is 64 bytes causing a memory exception when I run Marshal.PtrToStructure. Counting the bytes manually for the struct gives 64 so I'm at a bit of a loss as to why I'm receiving the 240 byte structures from function, any insight would be appreciated.
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet=CharSet.Unicode)]
public struct JOB_INFO_1
{
public UInt32 JobId;
public string pPrinterName;
public string pMachineName;
public string pUserName;
public string pDocument;
public string pDatatype;
public string pStatus;
public UInt32 Status;
public UInt32 Priority;
public UInt32 Position;
public UInt32 TotalPages;
public UInt32 PagesPrinted;
public SYSTEMTIME Submitted;
}
The size of 64 is indeed correct for JOB_INFO_1 but if you look closely at the documentation, it talks about an array of structs :
pJob [out]
A pointer to a buffer that receives an array of JOB_INFO_1, JOB_INFO_2, or JOB_INFO_3 structures.
Additionally it is written :
The buffer must be large enough to receive the array of structures and any strings or other data to which the structure members point.
So there are bytes here for extra data beside the structs themselves.
Solution:
Populate the structs, increment pointer for next struct and ignore the remaining bytes.
Complete example:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
namespace WpfApplication3
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Get handle to a printer
var hPrinter = new IntPtr();
bool open = NativeMethods.OpenPrinterW("Microsoft XPS Document Writer", ref hPrinter, IntPtr.Zero);
Debug.Assert(open);
/* Query for 99 jobs */
const uint firstJob = 0u;
const uint noJobs = 99u;
const uint level = 1u;
// Get byte size required for the function
uint needed;
uint returned;
bool b1 = NativeMethods.EnumJobsW(
hPrinter, firstJob, noJobs, level, IntPtr.Zero, 0, out needed, out returned);
Debug.Assert(!b1);
uint lastError = NativeMethods.GetLastError();
Debug.Assert(lastError == NativeConstants.ERROR_INSUFFICIENT_BUFFER);
// Populate the structs
IntPtr pJob = Marshal.AllocHGlobal((int) needed);
uint bytesCopied;
uint structsCopied;
bool b2 = NativeMethods.EnumJobsW(
hPrinter, firstJob, noJobs, level, pJob, needed, out bytesCopied, out structsCopied);
Debug.Assert(b2);
var jobInfos = new JOB_INFO_1W[structsCopied];
int sizeOf = Marshal.SizeOf(typeof (JOB_INFO_1W));
IntPtr pStruct = pJob;
for (int i = 0; i < structsCopied; i++)
{
var jobInfo_1W = (JOB_INFO_1W) Marshal.PtrToStructure(pStruct, typeof (JOB_INFO_1W));
jobInfos[i] = jobInfo_1W;
pStruct += sizeOf;
}
Marshal.FreeHGlobal(pJob);
// do something with your structs
}
}
public class NativeConstants
{
public const int ERROR_INSUFFICIENT_BUFFER = 122;
}
public partial class NativeMethods
{
[DllImport("kernel32.dll", EntryPoint = "GetLastError")]
public static extern uint GetLastError();
}
public partial class NativeMethods
{
[DllImport("Winspool.drv", EntryPoint = "OpenPrinterW")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool OpenPrinterW([In] [MarshalAs(UnmanagedType.LPWStr)] string pPrinterName,
ref IntPtr phPrinter, [In] IntPtr pDefault);
[DllImport("Winspool.drv", EntryPoint = "EnumJobsW")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumJobsW([In] IntPtr hPrinter, uint FirstJob, uint NoJobs, uint Level, IntPtr pJob,
uint cbBuf, [Out] out uint pcbNeeded, [Out] out uint pcReturned);
}
[StructLayout(LayoutKind.Sequential)]
public struct JOB_INFO_1W
{
public uint JobId;
[MarshalAs(UnmanagedType.LPWStr)] public string pPrinterName;
[MarshalAs(UnmanagedType.LPWStr)] public string pMachineName;
[MarshalAs(UnmanagedType.LPWStr)] public string pUserName;
[MarshalAs(UnmanagedType.LPWStr)] public string pDocument;
[MarshalAs(UnmanagedType.LPWStr)] public string pDatatype;
[MarshalAs(UnmanagedType.LPWStr)] public string pStatus;
public uint Status;
public uint Priority;
public uint Position;
public uint TotalPages;
public uint PagesPrinted;
public SYSTEMTIME Submitted;
}
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
}
Result:
Job 1:
Job 2 :
EDIT
It seems that you will have to get a little more robust checking than I did, because EnumJobs seems to return true when there are no jobs in queue. In the case of my example the assertion will fail but that won't mean that the code is wrong; just make sure that you have some jobs in the queue for the purpose of testing the function.
I am using RawPrinterHelper for printing. And it works fine with Windows 7 and previous versions.
When we tried it with a printer installed on Windows 8 pc, it did not work.
After reading this post I've learned that I have to set dataType variable to "XPS_PASS" instead of "RAW". Setting it to "XPS_PASS" works fine on windows 8 by the way.
But in my environment, there are windows 8s and windows 7s and XPs also.
Is it possible to make this switch programmatically?
How can I set pDataType variable to "RAW" for windows 7 and lower operating systems, and to "XPS_PASS" to windows 8?
Edit: After a couple of hours digging google I've found this article. Here it says:
Call GetPrinterDriver to retrieve the DRIVER_INFO_8 struct.
Check DRIVER_INFO_8::dwPrinterDriverAttributes for the PRINTER_DRIVER_XPS flag.
Choose your datatype based on the presence or absence of the
flag:
If the flag is set, use ‘XPS_PASS’
If the flag is not set, use ‘RAW’
I am not familiar with unmanaged code, but I've tried the followig:
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetPrinterDriver(IntPtr hPrinter, string pEnvironment, uint Level, IntPtr pDriverInfo, int cbBuf, out int pcbNeeded);
private static void GetPrinterDataType(IntPtr hPrinter )
{
IntPtr driverInfo = new IntPtr();
driverInfo = IntPtr.Zero;
int buf_len = 0;
int IntPtrSize = Marshal.SizeOf(typeof(IntPtr));
int a = GetPrinterDriver(hPrinter, "", 8, driverInfo, 0, out buf_len);
driverInfo = Marshal.AllocHGlobal(buf_len);
a = GetPrinterDriver(hPrinter, "", 8, driverInfo, buf_len, out buf_len);
for (int i = 0; i <= 24; i++)
{
if (i == 12 || i == 15 || i == 11 || i == 14)
continue;
IntPtr ptr = Marshal.ReadIntPtr(driverInfo, IntPtrSize * i);
Console.WriteLine("DRIVER INFO {0}: {1}", i, Marshal.PtrToStringUni(ptr));
}
}
I am calling this method after OpenPrinter() method of the RawPrinterHelper class. But the dwPrinterDriverAttributes (number 21) is empty.
Am I doing something wrong?
Okay, I've managed to work out how to get a value showing for the dwPrinterDriverAttributes field.
I added this definition of the DRIVER_INFO_8 structure to my solution (found here).
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DRIVER_INFO_8
{
public uint cVersion;
[MarshalAs(UnmanagedType.LPTStr)]
public string pName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pEnvironment;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDriverPath;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDataFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string pConfigFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string pHelpFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDependentFiles;
[MarshalAs(UnmanagedType.LPTStr)]
public string pMonitorName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDefaultDataType;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszzPreviousNames;
FILETIME ftDriverDate;
UInt64 dwlDriverVersion;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszMfgName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszOEMUrl;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszHardwareID;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszProvider;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszPrintProcessor;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszVendorSetup;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszzColorProfiles;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszInfPath;
public uint dwPrinterDriverAttributes;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszzCoreDriverDependencies;
FILETIME ftMinInboxDriverVerDate;
UInt64 dwlMinInboxDriverVerVersion;
}
Then I added this line of code to what you have above in your GetPrinterDriverDataType() method:
var info = (DRIVER_INFO_8)Marshal.PtrToStructure(driverInfo, typeof(DRIVER_INFO_8));
Now you will be able to see what the dwPrinterDriverAttributes field is populated with.
EDIT: Updated the protection level of dwPrinterDriverAttributes to be public so it can be accessed/viewed.
Also worth noting this (found here):
dwPrinterDriverAttributes: A bit field that specifies attributes of the printer driver.
So I've converted the uint to a BitArray and checked to see if the PRINTER_DRIVER_XPS flag/bit is set.
i.e.
PRINTER_DRIVER_XPS flag = 0x00000002
So we need to check the second bit. I do this with the following:
var value = (int)info.dwPrinterDriverAttributes;
BitArray b = new BitArray(new int[] { value } );
bool[] bits = new bool[b.Count];
b.CopyTo(bits, 0);
if (bits[1])
Console.WriteLine("flag set");
else
Console.WriteLine("flag not set");
I've got a c dll that contains an exposed function, that takes three parameters :
int ParseInput(char* opt_path, char* input, SENNA_RESULT_ARRAY* result);
I want to call this from C#, which actually works. The problem is that the result struct is not affected.
Here is the structure defined in c code :
typedef struct RESULT_
{
char* word;
int pos_start;
int pos_end;
char* pos;
char* chk;
char* ner;
char* psg;
} RESULT;
typedef struct RESULT_ARRAY_
{
int size;
RESULT* Results;
} RESULT_ARRAY;
and my c# code :
[StructLayout(LayoutKind.Sequential)]
public struct SENNA_RESULT
{
[MarshalAs(UnmanagedType.LPStr)]
public string word;
[MarshalAs(UnmanagedType.I4)]
public int pos_start;
[MarshalAs(UnmanagedType.I4)]
public int pos_end;
[MarshalAs(UnmanagedType.LPStr)]
public string pos;
[MarshalAs(UnmanagedType.LPStr)]
public string chk;
[MarshalAs(UnmanagedType.LPStr)]
public string ner;
[MarshalAs(UnmanagedType.LPStr)]
public string psg;
}
[StructLayout(LayoutKind.Sequential)]
public struct SENNA_RESULT_ARRAY
{
public SENNA_RESULT[] Results;
public int size;
}
[DllImport("Senna-32.dll", CharSet = CharSet.Ansi)]
static extern int Parse(string msg, string stream, ref SENNA_RESULT_ARRAY results);
Parse(#"path", "sentence", ref result_array)
I've tried many things, like :
1-use classes instead of struct without ref keyword
2-use a pointer instead of passing a struct
Each time i got a different error like
array is not of the specified type
low level error( corrupted heap)
even if i don't specify the array in the first struct, the size member has not the correct value (the C code prints the value in the console)
Any advice ?
Thanks
Consider using code below.
[StructLayout(LayoutKind.Sequential)]
public struct SENNA_RESULT
{
public IntPtr word;
public int pos_start;
public int pos_end;
public IntPtr pos;
public IntPtr chk;
public IntPtr ner;
public IntPtr psg;
}
[StructLayout(LayoutKind.Sequential)]
public struct SENNA_RESULT_ARRAY
{
public IntPtr Results;
public int size;
}
[DllImport("Senna-32.dll")]
static extern int Parse(string msg, string stream, out SENNA_RESULT_ARRAY results);
Here is usage example
SENNA_RESULT_ARRAY array = new SENNA_RESULT_ARRAY();
int result = Parse("path", "sentence", out array);
if (result == SUCCESS && array.Results != IntPtr.Zero)
{
for (int index = 0; index < array.size; index++)
{
IntPtr offset = (IntPtr)((int)array.Results + index * Marshal.SizeOf(typeof(SENNA_RESULT)));
SENNA_RESULT senna = (SENNA_RESULT)Marshal.PtrToStructure(offset, typeof(SENNA_RESULT));
}
}
I hope that you understand that it is just idea. Make sure that code is working properly and then improve it to make it more comfotable to use. I talking about replacing IntPtr with string.
I am using the SetupDiEnumDeviceInterfaces function to get the device interfaces that are contained in a device information set. But the GUID is not passing "SP_DEVICE_INTERFACE_DATA" structure. Here is my code snippet.
I have tried to see what is the issue by using GetLastError.It always returns zero.
//GUID.
GetHidGuid(Myguid)
[DllImport("hid.dll", SetLastError = true)]
static extern unsafe void GetHidGuid(
ref GUID lpHidGuid);
[StructLayout(LayoutKind.Sequential)]
public unsafe struct GUID
{
public int Data1;
public System.UInt16 Data2;
public System.UInt16 Data3;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] data4;
}
// SetupDiEnumDeviceInterfaces function.
public unsafe int CT_SetupDiEnumDeviceInterfaces(int memberIndex)
{
int ErrorStatus;
mySP_DEVICE_INTERFACE_DATA = new SP_DEVICE_INTERFACE_DATA();--> here is where i Have problem.GUID is zero.
mySP_DEVICE_INTERFACE_DATA.cbSize = Marshal.SizeOf(mySP_DEVICE_INTERFACE_DATA);
int result = SetupDiEnumDeviceInterfaces(
hDevInfo,
0,
ref MYguid,
memberIndex,
ref mySP_DEVICE_INTERFACE_DATA);
return result;
ErrorStatus = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
}
public unsafe struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public GUID InterfaceClassGuid;
public int Flags;
public int Reserved;
}
any help is appreciated. Thanks in adv.
From pInvoke, it seems your GetHidGuid should be declared as
[DllImport("hid.dll", EntryPoint="HidD_GetHidGuid", SetLastError=true)]
static extern void HidD_GetHidGuid(out Guid hidGuid);
Another complete example is here