Open Store App in Wpf current window using IApplicationActivationManager using C# - c#

I am using IApplicationActivationManager Interface method to start my WinStore package. But desired application is opening in separate window. I need this app to open in current window. Could you please help me for the same. My code is following:
[ComImport, Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]
class ApplicationActivationManager { }
static class MetroLauncher
{
public static uint LaunchApp(string packageFullName, string arguments = null)
{
IntPtr pir = IntPtr.Zero;
try
{
int error = OpenPackageInfoByFullName(packageFullName, 0, out pir);
Debug.Assert(error == 0);
if (error != 0)
throw new Win32Exception(error);
int length = 0, count;
GetPackageApplicationIds(pir, ref length, null, out count);
var buffer = new byte[length];
error = GetPackageApplicationIds(pir, ref length, buffer, out count);
Debug.Assert(error == 0);
if (error != 0)
throw new Win32Exception(error);
var appUserModelId = Encoding.Unicode.GetString(buffer, IntPtr.Size * count, length - IntPtr.Size * count);
var activation = (IApplicationActivationManager)new ApplicationActivationManager();
uint pid;
int hr = activation.ActivateApplication(appUserModelId, arguments ?? string.Empty, ActivateOptions.NoErrorUI, out pid);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr);
return pid;
}
finally
{
if (pir != IntPtr.Zero)
ClosePackageInfo(pir);
}
}
[DllImport("kernel32")]
static extern int OpenPackageInfoByFullName([MarshalAs(UnmanagedType.LPWStr)] string fullName, uint reserved, out IntPtr packageInfo);
[DllImport("kernel32")]
static extern int GetPackageApplicationIds(IntPtr pir, ref int bufferLength, byte[] buffer, out int count);
[DllImport("kernel32")]
static extern int ClosePackageInfo(IntPtr pir);
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("2e941141-7f97-4756-ba1d-9decde894a3d")]
interface IApplicationActivationManager {
int ActivateApplication([MarshalAs(UnmanagedType.LPWStr)] string appUserModelId, [MarshalAs(UnmanagedType.LPWStr)] string arguments,
ActivateOptions options, out uint processId);
int ActivateForFile([MarshalAs(UnmanagedType.LPWStr)] string appUserModelId, IntPtr pShelItemArray,
[MarshalAs(UnmanagedType.LPWStr)] string verb, out uint processId);
int ActivateForProtocol([MarshalAs(UnmanagedType.LPWStr)] string appUserModelId, IntPtr pShelItemArray,
[MarshalAs(UnmanagedType.LPWStr)] string verb, out uint processId);
}

Related

How to write 64 bit value as DWORD to Windows registry

I'm trying to write 64 bit value as a DWORD to Windows registry. I'm trying to do this, because that's what UnityEngine.PlayerPrefs does and I'm trying to edit those values. Funnily enough, PlayerPrefs only supports floats, but still for some reason writes them as doubles to the registry.
using (RegistryKey rk = Registry.CurrentUser.OpenSubKey("some\\valid\\path", true))
{
rk.SetValue("VALUE", double.MaxValue, RegistryValueKind.DWord);
}
Results in this error:
System.ArgumentException: 'The type of the value object did not match the specified RegistryValueKind or the object could not be properly converted.'
What would be the easiest way to do this in C#?
Found a way to do it:
[DllImport("advapi32.dll")]
static extern uint RegSetValueEx(
UIntPtr hKey,
[MarshalAs(UnmanagedType.LPStr)] string lpValueName,
int Reserved,
RegistryValueKind dwType,
IntPtr lpData,
int cbData);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern uint RegOpenKeyEx(
IntPtr hKey,
string subKey,
int ulOptions,
int samDesired,
out UIntPtr hkResult);
[DllImport("advapi32.dll")]
public static extern int RegCloseKey(UIntPtr hKey);
static public readonly IntPtr HKEY_CURRENT_USER = new IntPtr(-2147483647);
public bool SetNamedValue(string path, string valName, double value)
{
UIntPtr hKey = UIntPtr.Zero;
try
{
if (RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, 0x20006, out hKey) != 0)
return false;
int size = 8;
IntPtr pData = Marshal.AllocHGlobal(size);
Marshal.WriteInt64(pData, BitConverter.DoubleToInt64Bits(value));
if (RegSetValueEx(hKey, valName, 0, RegistryValueKind.DWord, pData, size) != 0)
return false;
}
finally
{
if (hKey != UIntPtr.Zero)
RegCloseKey(hKey);
}
return true;
}

Understanding SP_DEVICE_INTERFACE_DETAIL_DATA and processing on it [duplicate]

I've a problem trying to call SetupDiGetDeviceInterfaceDetail from C#. It always returns 1784 error code ("The supplied user buffer is not valid for the requested operation"). This is my C# code:
Guid GUID_DEVINTERFACE_DFU = new Guid(0x3fe809ab, 0xfb91, 0x4cb5, 0xa6, 0x43, 0x69, 0x67, 0x0d, 0x52,0x36,0x6e);
Guid classGuid = GUID_DEVINTERFACE_DFU;
IntPtr hDevInfo = Win32.SetupDiGetClassDevs(ref classGuid, IntPtr.Zero, IntPtr.Zero, Win32.DIGCF_DEVICEINTERFACE | Win32.DIGCF_PRESENT);
if (hDevInfo.ToInt32() == Win32.INVALID_HANDLE_VALUE)
{
Console.WriteLine("read hardware information error");
}
else
{
SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();
devInfoData.cbSize = (uint)Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
devInfoData.classGuid = Guid.Empty;
devInfoData.devInst = 0;
devInfoData.reserved = IntPtr.Zero;
bool result = Win32.SetupDiEnumDeviceInfo(hDevInfo, i, devInfoData);
if (false == result)
{
int error = Marshal.GetLastWin32Error();
if (error != Win32.ERROR_NO_MORE_ITEMS)
throw new Win32Exception(error);
}
SP_DEVICE_INTERFACE_DATA ifData = new SP_DEVICE_INTERFACE_DATA();
ifData.cbSize = (uint)Marshal.SizeOf(ifData);
ifData.Flags = 0;
ifData.InterfaceClassGuid = Guid.Empty;
ifData.Reserved = IntPtr.Zero;
bool result2 = Win32.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref classGuid, i, ifData);
if(result2 == false)
{
int error = Marshal.GetLastWin32Error();
if (error != Win32.ERROR_NO_MORE_ITEMS)
throw new Win32Exception(error);
}
uint needed;
// This returns: needed=160, result3=false and error=122 ("The data area passed to a system call is too small")
bool result3 = Win32.SetupDiGetDeviceInterfaceDetail(hDevInfo, ifData, null, 0, out needed, null);
if(result3 == false)
{
int error = Marshal.GetLastWin32Error();
}
IntPtr detailDataBuffer = IntPtr.Zero;
SP_DEVICE_INTERFACE_DETAIL_DATA ifDetailsData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
ifDetailsData.devicePath = new byte[needed - 4];
ifDetailsData.cbSize = (uint)Marshal.SizeOf(ifDetailsData);
uint nBytes = needed;
// This returns always: error = 1784
bool result4 = Win32.SetupDiGetDeviceInterfaceDetail(hDevInfo, ifData, ifDetailsData, nBytes, out needed, null);
if (result4 == false)
{
int error = Marshal.GetLastWin32Error();
if (error != Win32.ERROR_NO_MORE_ITEMS)
throw new Win32Exception(error);
}
}
Classe Win32:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace USB_test
{
[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
{
public uint cbSize;
public Guid classGuid;
public uint devInst;
public IntPtr reserved;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class SP_DEVICE_INTERFACE_DETAIL_DATA
{
public uint cbSize;
public byte[] devicePath;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class SP_DEVICE_INTERFACE_DATA
{
public uint cbSize;
public Guid InterfaceClassGuid;
public uint Flags;
public IntPtr Reserved;
}
public class Win32
{
public static uint ANYSIZE_ARRAY = 1000;
[DllImport("setupapi.dll", SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, uint Flags);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern Boolean SetupDiEnumDeviceInfo(IntPtr lpInfoSet, UInt32 dwIndex, SP_DEVINFO_DATA devInfoData);
[DllImport(#"setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, uint memberIndex, SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
[DllImport(#"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr hDevInfo, SP_DEVICE_INTERFACE_DATA deviceInterfaceData, SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, uint deviceInterfaceDetailDataSize, out uint requiredSize, SP_DEVINFO_DATA deviceInfoData);
public const int DIGCF_PRESENT = 0x02;
public const int DIGCF_DEVICEINTERFACE = 0x10;
public const int SPDRP_DEVICEDESC = (0x00000000);
public const long ERROR_NO_MORE_ITEMS = 259L;
}
}
If it can help someone, this is the solution:
IntPtr detailDataBuffer = Marshal.AllocHGlobal((int)needed);
Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);
uint nBytes = needed;
bool result4 = Win32.SetupDiGetDeviceInterfaceDetail(hDevInfo, ifData, detailDataBuffer, nBytes, out needed, null);
if (result4 == false)
{
int error = Marshal.GetLastWin32Error();
if (error != Win32.ERROR_NO_MORE_ITEMS)
throw new Win32Exception(error);
}
IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);
String devicePathName = Marshal.PtrToStringAuto(pDevicePathName);
Additional Note: If running on a 64-bit machine, or forced 64-bit mode, the above line for the pointer to pDevicePathName would reference a 64-bit pointer, not 32
IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt64() + 8);
The struct is a variable sized structure which cannot be marshalled automatically. You'll need to do so yourself.
You'll need to remove the SP_DEVICE_INTERFACE_DETAIL_DATA type. It's no use to you. Change the declaration of SetupDiGetDeviceInterfaceDetail to:
[DllImport(#"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(
IntPtr hDevInfo,
SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
IntPtr deviceInterfaceDetailData,
uint deviceInterfaceDetailDataSize,
out uint requiredSize,
SP_DEVINFO_DATA deviceInfoData
);
Pass IntPtr.Zero in the first call to SetupDiGetDeviceInterfaceDetail. Then allocate a buffer of the required size by calling Marshal.AllocHGlobal. Then write the size into the first 4 bytes of that buffer. Then call SetupDiGetDeviceInterfaceDetail again.
Something along these lines:
bool result3 = Win32.SetupDiGetDeviceInterfaceDetail(hDevInfo, ifData, IntPtr.Zero, 0,
out needed, null);
if(!result3)
{
int error = Marshal.GetLastWin32Error();
}
// expect that result3 is false and that error is ERROR_INSUFFICIENT_BUFFER = 122,
// and needed is the required size
IntPtr DeviceInterfaceDetailData = Marshal.AllocHGlobal((int)needed);
try
{
uint size = needed;
Marshal.WriteInt32(DeviceInterfaceDetailData, (int)size);
bool result4 = Win32.SetupDiGetDeviceInterfaceDetail(hDevInfo, ifData,
DeviceInterfaceDetailData, size, out needed, null);
if(!result4)
{
int error = Marshal.GetLastWin32Error();
}
// do whatever you need with DeviceInterfaceDetailData
}
finally
{
Marshal.FreeHGlobal(DeviceInterfaceDetailData);
}
For me, the answer of David Hoffmann doesn't work. But he inspired me to this solution:
IntPtr buffer = Marshal.AllocHGlobal((int)requiredSize);
int cbSize = sizeof(DWORD) + 2 * sizeof(CHAR); // cbSize + empty DevicePath
Marshal.WriteInt32(buffer, cbSize);
deviceInterfaceInfoData.cbSize = (DWORD)Marshal.SizeOf(deviceInterfaceInfoData);
if (!SetupDiGetDeviceInterfaceDetail(
deviceInfoSet,
ref deviceInterfaceData,
buffer,
requiredSize,
out requiredSize,
ref deviceInterfaceInfoData))
throw new Win32Exception(error);
int devicePathSize = (int)requiredSize - sizeof(DWORD); // cbSize
char[] devicePathChars = new char[devicePathSize / sizeof(char)];
int offset = sizeof(DWORD); // cbSize
Marshal.Copy(IntPtr.Add(buffer, offset), devicePathChars, 0, devicePathChars.Length);
string devicePath = new(devicePathChars, 0, devicePathChars.Length - 1); // Remove NULL terminator
Marshal.FreeHGlobal(buffer);

How to get a list of local machine groups / users when machine is not in active directory?

Is there a way to get in C# a list of local groups and users, when the windows machine is no AD member and an LDAP search can't be used?
You can use P/Invoke to call the native network management API to get local user and group names:
static class NativeMethods {
[DllImport("netapi32.dll")]
public static extern void NetApiBufferFree(IntPtr bufptr);
[DllImport("netapi32.dll")]
public static extern UInt32 NetUserEnum([MarshalAs(UnmanagedType.LPWStr)] String servername, UInt32 level, UInt32 filter, ref IntPtr bufptr, UInt32 prefmaxlen, ref UInt32 entriesread, ref UInt32 totalentries, IntPtr resumehandle);
[DllImport("netapi32.dll")]
public static extern UInt32 NetLocalGroupEnum([MarshalAs(UnmanagedType.LPWStr)] String servername, UInt32 level, ref IntPtr bufptr, UInt32 prefmaxlen, ref UInt32 entriesread, ref UInt32 totalentries, IntPtr resumehandle);
[DllImport("Netapi32.dll")]
public extern static UInt32 NetLocalGroupGetMembers([MarshalAs(UnmanagedType.LPWStr)] String servername, [MarshalAs(UnmanagedType.LPWStr)] String localgroupname, UInt32 level, ref IntPtr bufptr, UInt32 prefmaxlen, ref UInt32 entriesread, ref UInt32 totalentries, IntPtr resumehandle);
}
The API allows you to get various information about users. If you only want names you can use this function:
IEnumerable<String> GetUserNames() {
var buffer = IntPtr.Zero;
try {
UInt32 entriesRead = 0;
UInt32 totalEntries = 0;
var result = NativeMethods.NetUserEnum(null, 0, 0, ref buffer, UInt32.MaxValue, ref entriesRead, ref totalEntries, IntPtr.Zero);
if (result != 0)
throw new Win32Exception((Int32) result);
var userNames = Enumerable
.Range(0, (Int32) entriesRead)
.Select(
i => {
var userInfo = Marshal.ReadIntPtr(buffer, i*IntPtr.Size);
var userName = Marshal.PtrToStringAuto(userInfo);
return userName;
}
)
.ToList();
return userNames;
}
finally {
NativeMethods.NetApiBufferFree(buffer);
}
}
The LINQ statement is used to "parse" the buffer that contains USER_INFO_0 strutures. If you are querying for additional information you will have to do more elaborate "parsing".
Likewise you can get local group names:
IEnumerable<String> GetLocalGroupNames() {
var buffer = IntPtr.Zero;
try {
UInt32 entriesRead = 0;
UInt32 totalEntries = 0;
var result = NativeMethods.NetLocalGroupEnum(null, 0, ref buffer, UInt32.MaxValue, ref entriesRead, ref totalEntries, IntPtr.Zero);
if (result != 0)
throw new Win32Exception((Int32) result);
var localGroupNames = Enumerable
.Range(0, (Int32) entriesRead)
.Select(
i => {
var localGroupInfo = Marshal.ReadIntPtr(buffer, i*IntPtr.Size);
var groupName = Marshal.PtrToStringAuto(localGroupInfo);
return groupName;
}
)
.ToList();
return localGroupNames;
}
finally {
NativeMethods.NetApiBufferFree(buffer);
}
}
The structures in the buffer are LOCALGROUP_INFO_0 with the same layout as the USER_INFO_0 structure so the "parsing" code is identical.
Finally, here is how to get group membership using the LOCALGROUP_MEMBERS_INFO_3 structure:
IEnumerable<String> GetLocalGroupUsers(String localGroupName) {
var buffer = IntPtr.Zero;
try {
UInt32 entriesRead = 0;
UInt32 totalEntries = 0;
var result = NativeMethods.NetLocalGroupGetMembers(null, localGroupName, 3, ref buffer, UInt32.MaxValue, ref entriesRead, ref totalEntries, IntPtr.Zero);
if (result != 0)
throw new Win32Exception((Int32) result);
var userNames = Enumerable
.Range(0, (Int32) entriesRead)
.Select(
i => {
var membersInfo = Marshal.ReadIntPtr(buffer, i*IntPtr.Size);
var userName = Marshal.PtrToStringAuto(membersInfo );
return userName;
}
)
.ToList();
return userNames;
}
finally {
NativeMethods.NetApiBufferFree(buffer);
}
}

How to create fast and efficient filestream writes on large sparse files

I have an application that writes large files in multiple segments. I use FileStream.Seek to position each wirte. It appears that when I call FileStream.Write at a deep position in a sparse file the write triggers a "backfill" operation (writeing 0s) on all preceding bytes which is slow.
Is there a more efficient way of handling this situation?
The below code demonstrates the problem. The initial write takes about 370 MS on my machine.
public void WriteToStream()
{
DateTime dt;
using (FileStream fs = File.Create("C:\\testfile.file"))
{
fs.SetLength(1024 * 1024 * 100);
fs.Seek(-1, SeekOrigin.End);
dt = DateTime.Now;
fs.WriteByte(255);
}
Console.WriteLine(#"WRITE MS: " + DateTime.Now.Subtract(dt).TotalMilliseconds.ToString());
}
NTFS does support Sparse Files, however there is no way to do it in .net without p/invoking some native methods.
It is not very hard to mark a file as sparse, just know once a file is marked as a sparse file it can never be converted back in to a non sparse file except by coping the entire file in to a new non sparse file.
Example useage
class Program
{
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
int dwIoControlCode,
IntPtr InBuffer,
int nInBufferSize,
IntPtr OutBuffer,
int nOutBufferSize,
ref int pBytesReturned,
[In] ref NativeOverlapped lpOverlapped
);
static void MarkAsSparseFile(SafeFileHandle fileHandle)
{
int bytesReturned = 0;
NativeOverlapped lpOverlapped = new NativeOverlapped();
bool result =
DeviceIoControl(
fileHandle,
590020, //FSCTL_SET_SPARSE,
IntPtr.Zero,
0,
IntPtr.Zero,
0,
ref bytesReturned,
ref lpOverlapped);
if(result == false)
throw new Win32Exception();
}
static void Main()
{
//Use stopwatch when benchmarking, not DateTime
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (FileStream fs = File.Create(#"e:\Test\test.dat"))
{
MarkAsSparseFile(fs.SafeFileHandle);
fs.SetLength(1024 * 1024 * 100);
fs.Seek(-1, SeekOrigin.End);
fs.WriteByte(255);
}
stopwatch.Stop();
//Returns 2 for sparse files and 1127 for non sparse
Console.WriteLine(#"WRITE MS: " + stopwatch.ElapsedMilliseconds);
}
}
Once a file has been marked as sparse it now behaves like you excepted it to behave in the comments too. You don't need to write a byte to mark a file to a set size.
static void Main()
{
string filename = #"e:\Test\test.dat";
using (FileStream fs = new FileStream(filename, FileMode.Create))
{
MarkAsSparseFile(fs.SafeFileHandle);
fs.SetLength(1024 * 1024 * 25);
}
}
Here is some code to use sparse files:
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Microsoft.Win32.SafeHandles;
public static class SparseFiles
{
private const int FILE_SUPPORTS_SPARSE_FILES = 64;
private const int FSCTL_SET_SPARSE = 0x000900c4;
private const int FSCTL_SET_ZERO_DATA = 0x000980c8;
public static void MakeSparse(this FileStream fileStream)
{
var bytesReturned = 0;
var lpOverlapped = new NativeOverlapped();
var result = DeviceIoControl(
fileStream.SafeFileHandle,
FSCTL_SET_SPARSE,
IntPtr.Zero,
0,
IntPtr.Zero,
0,
ref bytesReturned,
ref lpOverlapped);
if (!result)
{
throw new Win32Exception();
}
}
public static void SetSparseRange(this FileStream fileStream, long fileOffset, long length)
{
var fzd = new FILE_ZERO_DATA_INFORMATION();
fzd.FileOffset = fileOffset;
fzd.BeyondFinalZero = fileOffset + length;
var lpOverlapped = new NativeOverlapped();
var dwTemp = 0;
var result = DeviceIoControl(
fileStream.SafeFileHandle,
FSCTL_SET_ZERO_DATA,
ref fzd,
Marshal.SizeOf(typeof(FILE_ZERO_DATA_INFORMATION)),
IntPtr.Zero,
0,
ref dwTemp,
ref lpOverlapped);
if (!result)
{
throw new Win32Exception();
}
}
public static bool SupportedOnVolume(string filename)
{
var targetVolume = Path.GetPathRoot(filename);
var fileSystemName = new StringBuilder(300);
var volumeName = new StringBuilder(300);
uint lpFileSystemFlags;
uint lpVolumeSerialNumber;
uint lpMaxComponentLength;
var result = GetVolumeInformationW(
targetVolume,
volumeName,
(uint)volumeName.Capacity,
out lpVolumeSerialNumber,
out lpMaxComponentLength,
out lpFileSystemFlags,
fileSystemName,
(uint)fileSystemName.Capacity);
if (!result)
{
throw new Win32Exception();
}
return (lpFileSystemFlags & FILE_SUPPORTS_SPARSE_FILES) == FILE_SUPPORTS_SPARSE_FILES;
}
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
int dwIoControlCode,
IntPtr InBuffer,
int nInBufferSize,
IntPtr OutBuffer,
int nOutBufferSize,
ref int pBytesReturned,
[In] ref NativeOverlapped lpOverlapped);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeviceIoControl(
SafeFileHandle hDevice,
int dwIoControlCode,
ref FILE_ZERO_DATA_INFORMATION InBuffer,
int nInBufferSize,
IntPtr OutBuffer,
int nOutBufferSize,
ref int pBytesReturned,
[In] ref NativeOverlapped lpOverlapped);
[DllImport("kernel32.dll", EntryPoint = "GetVolumeInformationW")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetVolumeInformationW(
[In] [MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName,
[Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpVolumeNameBuffer,
uint nVolumeNameSize,
out uint lpVolumeSerialNumber,
out uint lpMaximumComponentLength,
out uint lpFileSystemFlags,
[Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpFileSystemNameBuffer,
uint nFileSystemNameSize);
[StructLayout(LayoutKind.Sequential)]
private struct FILE_ZERO_DATA_INFORMATION
{
public long FileOffset;
public long BeyondFinalZero;
}
}
And sample code to test the above class.
class Program
{
static void Main(string[] args)
{
using (var fileStream = new FileStream("test", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
fileStream.SetLength(1024 * 1024 * 128);
fileStream.MakeSparse();
fileStream.SetSparseRange(0, fileStream.Length);
}
}
}
Hope this helps

C# Get File Owner from Linux File

I have a problem that I am hoping someone can help with. I have a c# application that needs to get the File Owner information from a file that resides on a Linux server. The .Net System.IO GetFileInfo throws an exception and the WMI calls fail. I know there is the PInvoke method GetFileOwner however the example on pinvoke.net is not complete and does not compile. Does anyone have a good complete example or a link to find this information?
This is a .Net c# 3.5 windows application and does have permissions to access the file, however I want to get the owner information before it does the rest of the processing.
Thanks
Below is the code sample I used. It works in my application and in my environment. Should work in others. Sorry this was not posted sooner, was new to StackOverFlow. I hope this posts properly.
public class GetUserInfo{
private const int NAME_SIZE = 0x40;
// Methods
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool ConvertSidToStringSid(IntPtr Sid, ref IntPtr StringSid);
public string ConvertSidToStringSidNT(IntPtr Sid)
{
string ans = string.Empty;
if (IsValidSid(Sid))
{
ans = "S-1-";
SID_IDENTIFIER_AUTHORITY psia = (SID_IDENTIFIER_AUTHORITY) Marshal.PtrToStructure(GetSidIdentifierAuthority(Sid), typeof(SID_IDENTIFIER_AUTHORITY));
int num = Marshal.ReadInt16(GetSidSubAuthorityCount(Sid));
if ((psia.Value[0] != 0) & (psia.Value[1] != 0))
{
ans = ((ans + Conversion.Hex(psia.Value[0]) + Conversion.Hex(psia.Value[1]).PadLeft(2, '0')) + Conversion.Hex(psia.Value[2]).PadLeft(2, '0') + Conversion.Hex(psia.Value[3]).PadLeft(2, '0')) + Conversion.Hex(psia.Value[4]).PadLeft(2, '0') + Conversion.Hex(psia.Value[5]).PadLeft(2, '0');
}
else
{
long top = psia.Value[5];
top += psia.Value[4] * 0x100;
top += (psia.Value[3] * 0x100) * 0x100;
ans = ans + ((top + (((psia.Value[2] * 0x100) * 0x100) * 0x100))).ToString();
}
int VB$t_i4$L0 = num - 1;
for (int i = 0; i <= VB$t_i4$L0; i++)
{
ans = ans + "-" + Marshal.ReadInt32(GetSidSubAuthority(Sid, i)).ToString();
}
}
return ans;
}
public string GetFileOwner(string Path)
{
string MachineName;
IntPtr OwnerSid;
int peUse;
IntPtr SD;
string UserName;
IntPtr VB$t_struct$N0;
SE_OBJECT_TYPE ObjectType = SE_OBJECT_TYPE.SE_FILE_OBJECT;
if (GetNamedSecurityInfo(ref Path, ObjectType, SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, ref OwnerSid, ref VB$t_struct$N0, ref VB$t_struct$N0, ref VB$t_struct$N0, ref SD) != 0)
{
return "Error";
}
Marshal.FreeHGlobal(SD);
if (Path.StartsWith(#"\\"))
{
MachineName = Path.Split(new char[] { '\\' })[2];
}
else
{
MachineName = "";
}
int name_len = 0x40;
int domain_len = 0x40;
string name = Strings.Space(name_len);
string domain_name = Strings.Space(domain_len);
if (!LookupAccountSid(ref MachineName, OwnerSid, ref name, ref name_len, ref domain_name, ref domain_len, ref peUse))
{
string SidString;
if (Marshal.GetLastWin32Error() != 0x534)
{
return "Error";
}
if (Environment.Version.Major == 4)
{
SidString = this.ConvertSidToStringSidNT(OwnerSid);
}
else
{
IntPtr StringPtr;
if (!ConvertSidToStringSid(OwnerSid, ref StringPtr))
{
return "Error";
}
SidString = Marshal.PtrToStringAuto(StringPtr);
Marshal.FreeHGlobal(StringPtr);
}
domain_len = 0;
name = SidString;
name_len = Strings.Len(name);
}
if (domain_len > 0)
{
UserName = Strings.Left(name, name_len);
}
else
{
UserName = Strings.Left(name, name_len);
}
return UserName;
}
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern int GetNamedSecurityInfo([MarshalAs(UnmanagedType.VBByRefStr)] ref string pObjectName, SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo, ref IntPtr ppsidOwner, ref IntPtr ppsidGroup, ref IntPtr ppDacl, ref IntPtr ppSacl, ref IntPtr ppSecurityDescriptor);
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern IntPtr GetSidIdentifierAuthority(IntPtr pSid);
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern IntPtr GetSidSubAuthority(IntPtr pSid, int nSubAuthority);
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern IntPtr GetSidSubAuthorityCount(IntPtr pSid);
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
private static extern bool IsValidSid(IntPtr pSid);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool LookupAccountSid([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpSystemName, IntPtr lpSid, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpName, ref int cchName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpReferenceDomainName, ref int cchReferencedDomainName, ref int peUse);
// Nested Types
private enum SE_OBJECT_TYPE
{
SE_UNKNOWN_OBJECT_TYPE,
SE_FILE_OBJECT,
SE_SERVICE,
SE_PRINTER,
SE_REGISTRY_KEY,
SE_LMSHARE,
SE_KERNEL_OBJECT,
SE_WINDOW_OBJECT,
SE_DS_OBJECT,
SE_DS_OBJECT_ALL,
SE_PROVIDER_DEFINED_OBJECT,
SE_WMIGUID_OBJECT,
SE_REGISTRY_WOW64_32
}
private enum SECURITY_INFORMATION
{
DACL_SECURITY_INFORMATION = 4,
GROUP_SECURITY_INFORMATION = 2,
OWNER_SECURITY_INFORMATION = 1,
PROTECTED_DACL_SECURITY_INFORMATION = 0x20,
PROTECTED_SACL_SECURITY_INFORMATION = 0x10,
SACL_SECURITY_INFORMATION = 8,
UNPROTECTED_DACL_SECURITY_INFORMATION = 0x80,
UNPROTECTED_SACL_SECURITY_INFORMATION = 0x40
}
[StructLayout(LayoutKind.Sequential)]
private struct SID_IDENTIFIER_AUTHORITY
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
public byte[] Value;
} }

Categories

Resources