I read a list of SIDs from the registry, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList.
How would one resolve the display username (e.g. DOMAIN\user, BUILT-IN\user) given the SID string in C#?
Just found it on the pinvoke.net.
Alternative Managed API:
Available in .Net 2.0:
using System.Security.Principal;
// convert the user sid to a domain\name
string account = new SecurityIdentifier(stringSid).Translate(typeof(NTAccount)).ToString();
The Win32 API function LookupAccountSid() is used to find the name that corresponds to a SID.
LookupAccountSid() has the following signature:
BOOL LookupAccountSid(LPCTSTR lpSystemName, PSID Sid,LPTSTR Name, LPDWORD cbName,
LPTSTR ReferencedDomainName, LPDWORD cbReferencedDomainName,
PSID_NAME_USE peUse);
MSDN Ref.
Here's the P/Invoke reference (with sample code): http://www.pinvoke.net/default.aspx/advapi32.LookupAccountSid
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError = true)]
static extern bool LookupAccountSid (
string lpSystemName,
[MarshalAs(UnmanagedType.LPArray)] byte[] Sid,
StringBuilder lpName,
ref uint cchName,
StringBuilder ReferencedDomainName,
ref uint cchReferencedDomainName,
out SID_NAME_USE peUse);
Related
I know DDE is old fashion outdated technology. But I am creating a C# Windows Form application to send data to Excel file for research purpose. I use Win32 DDE functions and below is what I have tried so far.
Pinvoke signatures:
[DllImport("user32.dll", EntryPoint = "DdeInitialize")]
public static extern int DdeInitialize(out uint pidInst, IntPtr pfnCallback, IntPtr afCmd, IntPtr ulRes);
[DllImport("user32.dll", EntryPoint = "DdeConnect")]
private static extern IntPtr DdeConnect(uint idInst, IntPtr hszService, IntPtr hszTopic, IntPtr pCC);
[DllImport("user32.dll", EntryPoint = "DdeCreateStringHandle")]
private static extern IntPtr DdeCreateStringHandle(uint idInst, string psz, int iCodePage);
public Form1()
{
InitializeComponent();
Process application = new Process();
application.StartInfo.FileName = #"C:\Users\xxx\Desktop\DDE_Client.xlsx";
application.Start();
uint instId;
DdeInitialize(out instId, application.MainWindowHandle, IntPtr.Zero, IntPtr.Zero);
IntPtr sPtr = DdeCreateStringHandle(instId, "Excel", 1004);
IntPtr tPtr = DdeCreateStringHandle(instId, "Sheet1", 1004);
IntPtr channel = DdeConnect(instId, sPtr, tPtr, (IntPtr)null);
}
DdeConnect returns non-zero value which indicates the connection is successful.
My question:
How to send a string value to the excel sheet? For example, I want to send "Name" to excel cell A1. Which DDE function should I use for the task?
You are trying to use DDEML and it is indeed hard to find decent DDEML examples because many people who actually needed to use DDE had done so directly using raw Windows Messages.
You will have to first start client transaction using DdeClientTransaction and then corresponding api like DdeGetData or DdeAddData, DdeAccessData etc to access or get the data.
You can find more details about DDEML in the MS Documentation here.
But since you are already using C# I highly recommend that it would be much better to just add a reference to the Microsoft Excel or Office Object Library in COM Assemblies section and use Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
I work in a place where directories have such a looong name and are in such a looong tree.
And I'm having problems with too long path names for folders in an external applicatoin (I can't change this external application, but I can give it shortened path names).
I know Microsoft operating systems can shorten path names such as transforming C:\TooLongName\TooLongSubDirectory in something like C:\TooLon~1\TooLon~1.
But how can I do this in C# and still keep the nave valid and usable?
PS: I'm not using the standard FileInfo and DirectoryInfo classes, I'm using just strings that will be sent to an external application that I cannot change in any way.
If you are unable to use the long path support build into Windows 10 you are able to use the Win32 command GetShortPathName . In order to generate a suitable path.
class Program
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszLongPath,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpszShortPath,
uint cchBuffer);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetShortPathName(string lpszLongPath, char[] lpszShortPath, int cchBuffer);
static void Main(string[] args)
{
StringBuilder builder = new StringBuilder(260);
var shortPath = GetShortPathName(#"C:\Projects\Databases\ReallllllllllllllyLOOOOOOOOOOOOOOOOOOOOOONGPATHHHHHHHHHHH\StillllllllllllllllllGOoooooooooooooooooooooooing", builder, (uint)builder.Capacity);
Console.WriteLine(builder.ToString());
Console.ReadKey();
}
}
Produces C:\Projects\DATABA~1\REALLL~1\STILLL~1
I would like to move a file from Windows Mobile to a specific folder in Windows. The file in the mobile device is into the My Documents path.
The device is connected to the WiFi network, and the folder shared in windows is called "folder".
How could I do, I tried this but doesn't work:
var f= System.Enviroment.GetFolderPath(System.Enviroment.SpecialFolder.Personal);
FileInfo fi = new FileInfo(f.ToString() + #"\file.txt");
fi.CopyTo(#"\\MYPERSONAL-PC\folder",true);
the error is :
in System.IO.__Error.WinIOError(Int32 errorCode, String str)
in System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
in System.IO.FileInfo.CopyTo(String destFileName, Boolean overwrite)
in Project.MainForm.SaveButton_Click(Object sender, EventArgs e)
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
in System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
in Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
in System.Windows.Forms.Application.Run(Form fm)
in Project.Program.Main()
I also tried to use WNetAddConnection3, but still the same the connection to the network resource is ok, but return me always the same the code is here:
[StructLayout(LayoutKind.Sequential)]
internal struct NetResource
{
public uint dwScope;
public uint dwType;
public uint dwDisplayType;
public uint dwUsage;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
public string lpLocalName;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
public string lpRemoteName;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
public string lpComment;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
public string lpProvider;
}
[DllImport("coredll.dll")]
private static extern int WNetAddConnection3(IntPtr hWndOwner,
ref NetResource lpNetResource, string lpPassword, string lpUserName, int dwFlags);
[DllImport("coredll.dll")]
static extern int WNetCancelConnection2(string lpName, Int32 dwFlags, bool bForce);
var f= System.Enviroment.GetFolderPath(System.Enviroment.SpecialFolder.Personal);
NetResource logsResource = new NetResource();
logsResource.lpLocalName = "logs";
logsResource.lpRemoteName = #"\\MYPERSONAL-PC\folder";
logsResource.dwType = 0x1;
logsResource.dwScope = 0;
logsResource.dwUsage = 0;
logsResource.dwDisplayType = 0;
//try to connect the network resource
WNetAddConnection3(new IntPtr(0), ref logsResource, #"pass", #"dom\user", 0);
FileInfo fi = new FileInfo(f.ToString() + #"\file.txt");
**fi.CopyTo(#"\\MYPERSONAL-PC\folder", true);**
A few things to try:
Can you connect to the file share from a laptop or other PC on the wireless network? I want to verify it's not a configuration or authentication problem.
Assuming #1 works: try replacing MYPERSONAL-PC with the IP address of the PC?
Assuming #1 works, but #2 does not: try to P/Invoke WNetAddConnection3 to make a local connection to a network resource (like mapping a network drive) and copy to that.
I would set the redir registry entry RegisterFSRoot to 1 and see the \Network folder on the device: http://msdn.microsoft.com/en-us/library/aa922326.aspx. Then you can use the file copy function to copy to \Network\. Where shared folder is the one you mapped from the sharing server. If you did not Connect to the share using mobile File Explorer, you can use WNetAddConnection3 to add the connection http://msdn.microsoft.com/en-us/library/aa916067.aspx and http://msdn.microsoft.com/en-us/library/aa917445.aspx. When usig C# you have to p/invoke the APIs: http://www.pinvoke.net/default.aspx/mpr.wnetaddconnection3 (replace mpr.dll with coredll.dll)
In order to avoid registry redirection to Wow64 keys, how to translate the following code that uses Microsoft.Win32 APIs
public void SetKeyAccessControl(
RegistryKey rootKey, string subKeyName, string identity,
RegistryRights rights, InheritanceFlags inheritanceFlags,
PropagationFlags propagationFlags, AccessControlType accessType)
{
using (RegistryKey regKey = rootKey.OpenSubKey(subKeyName, true))
{
RegistrySecurity acl = new RegistrySecurity();
RegistryAccessRule rule = new RegistryAccessRule(identity, rights, inheritanceFlags, propagationFlags, accessType);
acl.AddAccessRule(rule);
regKey.SetAccessControl(acl);
}
}
into using advapi32 RegSetKeySecurity API
[DllImport(#"advapi32.dll", EntryPoint = "RegSetKeySecurity", SetLastError = true)]
internal static extern int RegSetKeySecurity(IntPtr handle, uint securityInformation, IntPtr pSecurityDescriptor);
To avoid Registry redirection, you could do something like this...
SafeRegistryHandle handle = rootKey.Handle;
RegistryKey rootKey32 = RegistryKey.FromHandle(handle, RegistryView.Registry32);
RegistryKey rootKey64 = RegistryKey.FromHandle(handle, RegistryView.Registry64);
You can then use rootKey32 or rootKey64 to open a subkey and you'll get the subkey of the view requested.
At least it works in the few test cases I've tried. And, according to the documentation for FromHandle...
The view parameter for this method is used in subsequent operations,
such as opening subkeys
Another native method needs to be involved and given an SDDL, the following code sets ACLs on the right registry key:
[DllImport("Advapi32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool ConvertStringSecurityDescriptorToSecurityDescriptor(string stringSecurityDescriptor, int stringSDRevision, out IntPtr ppSecurityDescriptor, ref int securityDescriptorSize);
string sddl = "...";
IntPtr secDescriptor = IntPtr.Zero;
int size = 0;
ConvertStringSecurityDescriptorToSecurityDescriptor
(
sddl,
1, // revision 1
out secDescriptor,
ref size
);
// get handle with RegOpenKeyEx
RegSetKeySecurity
(
handle,
0x00000004, // DACL_SECURITY_INFORMATION
secDescriptor
);
I am making use of an external unmanaged dll using PInvoke and the DllImport attribute. eg.
[DllImport("mcs_apiD.dll", CharSet = CharSet.Auto)]
private static extern byte start_api(byte pid, byte stat, byte dbg, byte ka);
I am wondering if it is possible to alter the dll file details (mcs_apiD.dll in this example) dynmically in some manner, if for instance I wanted to build against another dll version
Yes this is possible, you'll have to do part of the job that the P/Invoke marshaller does. Loading the DLL and finding the entry point of the exported function. Start by declaring a delegate whose signature matches the exported function:
private delegate byte start_api(byte pid, byte stat, byte dbg, byte ka);
Then use code like this:
using System.ComponentModel;
using System.Runtime.InteropServices;
...
static IntPtr dllHandle;
...
if (dllHandle == IntPtr.Zero) {
dllHandle = LoadLibrary("mcs_apiD.dll");
if (dllHandle == IntPtr.Zero) throw new Win32Exception();
}
IntPtr addr = GetProcAddress(dllHandle, "_start_api#16");
if (addr == IntPtr.Zero) throw new Win32Exception();
var func = (start_api)Marshal.GetDelegateForFunctionPointer(addr, typeof(start_api));
var retval = func(1, 2, 3, 4);
...
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr LoadLibrary(string name);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string name);
Lots of ways to get this wrong of course. Do note that you have to use the actual exported name from the DLL, you no longer get the help from the P/Invoke marshaller to help with name decoration. Use dumpbin.exe /exports on the DLL if you are not sure what the export name looks like.
you can't change the name of the dll but you can alter the path of the library being loaded (like by reading it from the registry or a configuration file) and load it manually with LoadLibrary kernel32's function: see my answer there.