|
Correct.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi,
I know how to resize one dimensional array,
Array.Resize(ref ArrayName, newSize)
But if my array is of 2 dimension, how can i resize to new 2 dimension ?
|
|
|
|
|
Is it a real two dimensional array or a jagged array?
two dimensional:
object [,] myArray = new object[X,Y];
jagged array
object [][] myArray = new object[X][];
foreach (object[] myObject in myArray)
{
myObject = new object[Y];
}
A two dimensional array is X * Y in size if you read the length, where an jagged array is just X with a Y value of each X.
|
|
|
|
|
thanks
My array is real two dimensional array.
string [,] = new string[2,4];
Now, I want to resize this array to new value of string[7,9] but I dont find any way to do this.
|
|
|
|
|
What you may need to do, ugly as it is, is create a new one of the new size, and copy it in.
Learn to write self marginalizing code!
Call 1-888-BAD-CODE
|
|
|
|
|
Oh, far as I'm aware that is what you HAVE to do for a multidimensional array.
Jagged array's are normally the "defacto" standard because of this...
I guess you could make something like this:
object[,] ResizeTwoDimArray(object[,] oldObject, uint x, uint y)
{
object[,] newObject = new object[x,y];
Array.Copy(oldObject,newObject,oldObject.Length);
return newObject;
}
|
|
|
|
|
You can't resize an array in .NET. The Array.Resize method creates a new array and copies the data to it.
That is what you have to do to resize a two dimensional array too, only there is no built in method for it so you have to do it yourself.
If you frequently resize the array, you should consider using a dynamic collection instead that actually supports resizing, like an list of arrays or a list of lists.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hello, I wrote a windows service that is supposed to start an interactive GUI app. I realize a service will not readily do this so I've pieced together the code below to bypass that. However, the call to CreateProcessAsUser always returns a 1314 error: 'A required privilege is not held by the client.'
I realize from the doc's that the calling process must have SE_TCB_NAME privilege to assign new privileges to the new process and I have done that in a method further below.
So I'm not sure why this is not launching the app. To keep things simple, I'm running notepad.exe from a cmd console, I'm just running this code as its own app so there's no service on top to muddle things up. Also I'm running it locally to make things simpler, but still that error keeps popping up. Also I'm on XP.
Can anyone see what I have done wrong in my code? Thanks in advance!!
public static void Main(string[] args)
{
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);
try
{
string userName, domainName;
domainName = "mydomain";
userName = "user";
pwd="mypass";
tokenHandle = IntPtr.Zero;
dupeTokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName, domainName, pwd,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if(false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine("LogonUser failed with error code : {0}", ret);
int errorCode = 0x5;
throw new System.ComponentModel.Win32Exception(errorCode);
}
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);
bool retVal = DuplicateTokenEx(tokenHandle, (uint)TokenAccessLevels.AllAccess, ref pSec,
SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, TOKEN_TYPE.TokenPrimary,
out dupeTokenHandle);
if(false == retVal)
{
CloseHandle(tokenHandle);
Console.WriteLine("Exception thrown in trying to duplicate token.");
return;
}
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
PROFILEINFO aPI = new PROFILEINFO();
aPI.dwSize = 32;
aPI.lpUserName = WindowsIdentity.GetCurrent().Name;
bool retLoadProfile = LoadUserProfile(newId.Token, ref aPI);
StringBuilder CommandLine = new StringBuilder(@"cmd /c C:\windows\system32\notepad.exe");
const uint NORMAL_PRIORITY_CLASS = 0x0020;
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
STARTUPINFO sInfo = new STARTUPINFO();
IntPtr aToken=newId.Token;
SetPrivs(ref aToken);
sInfo.wShowWindow = 5;
sInfo.lpTitle = "Hello!";
sInfo.lpDesktop="winsta0\\default";
uint dwCreationFlags = NORMAL_PRIORITY_CLASS| CREATE_NEW_CONSOLE;
IntPtr pEnv = IntPtr.Zero;
if(CreateEnvironmentBlock(out pEnv, dupeTokenHandle, true))
{
dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT;
}
else
{
pEnv = IntPtr.Zero;
}
bool retCreateProcess = CreateProcessAsUser(newId.Token,
null,
CommandLine,
ref pSec,
ref tSec,
false,
NORMAL_PRIORITY_CLASS,
pEnv,
null,
ref sInfo,
out pInfo);
int err = Marshal.GetLastWin32Error();
Console.WriteLine("CreateProcess="+retCreateProcess.ToString()+". With error:"+ err.ToString());
Console.WriteLine("After impersonation: " + WindowsIdentity.GetCurrent().Name);
Console.ReadKey();
impersonatedUser.Undo();
if(tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
if(dupeTokenHandle != IntPtr.Zero)
CloseHandle(dupeTokenHandle);
}
catch(Exception ex)
{
Console.WriteLine("Exception occurred. " + ex.Message);
}
}
}
In this method as an experiment I tried setting all privileges to see if perhaps I had been missing one but generally I will be setting TCB(if that's the right one to set.)
You'll note that I'm setting the Current Process Privilage to TCB because the Doc's say that that needs to be done.
private static bool SetPrivs(ref IntPtr theToken)
{
const int SE_PRIVILEGE_ENABLED = 0x0002;
List<string> aPrivs = new List<string>();
aPrivs.Add(SE_CREATE_TOKEN_NAME);
aPrivs.Add(SE_ASSIGNPRIMARYTOKEN_NAME);
aPrivs.Add(SE_LOCK_MEMORY_NAME);
aPrivs.Add(SE_INCREASE_QUOTA_NAME);
aPrivs.Add(SE_UNSOLICITED_INPUT_NAME);
aPrivs.Add(SE_MACHINE_ACCOUNT_NAME);
aPrivs.Add(SE_TCB_NAME);
aPrivs.Add(SE_SECURITY_NAME);
aPrivs.Add(SE_TAKE_OWNERSHIP_NAME);
aPrivs.Add(SE_LOAD_DRIVER_NAME);
aPrivs.Add(SE_SYSTEM_PROFILE_NAME);
aPrivs.Add(SE_SYSTEMTIME_NAME);
aPrivs.Add(SE_PROF_SINGLE_PROCESS_NAME);
aPrivs.Add(SE_INC_BASE_PRIORITY_NAME);
aPrivs.Add(SE_CREATE_PAGEFILE_NAME);
aPrivs.Add(SE_CREATE_PERMANENT_NAME);
aPrivs.Add(SE_BACKUP_NAME);
aPrivs.Add(SE_RESTORE_NAME);
aPrivs.Add(SE_SHUTDOWN_NAME);
aPrivs.Add(SE_DEBUG_NAME);
aPrivs.Add(SE_AUDIT_NAME);
aPrivs.Add(SE_SYSTEM_ENVIRONMENT_NAME);
aPrivs.Add(SE_CHANGE_NOTIFY_NAME);
aPrivs.Add(SE_REMOTE_SHUTDOWN_NAME);
aPrivs.Add(SE_UNDOCK_NAME);
aPrivs.Add(SE_SYNC_AGENT_NAME);
aPrivs.Add(SE_ENABLE_DELEGATION_NAME);
aPrivs.Add(SE_MANAGE_VOLUME_NAME);
aPrivs.Add(SE_IMPERSONATE_NAME);
aPrivs.Add(SE_CREATE_GLOBAL_NAME);
SetCurrProcPriv(SE_TCB_NAME);
foreach(string aPriv in aPrivs)
{
TOKEN_PRIVILEGES aTP = new TOKEN_PRIVILEGES();
LUID luid = new LUID();
aTP.PrivilegeCount = 1;
aTP.Privileges = new int[3];
aTP.Privileges[2] = SE_PRIVILEGE_ENABLED;
aTP.Privileges[1] = luid.HighPart;
aTP.Privileges[0] = luid.LowPart;
try
{
LookupPrivilegeValue(IntPtr.Zero, aPriv, ref luid);
bool isSuccess=AdjustTokenPrivileges(theToken, false, ref aTP, Marshal.SizeOf(aTP), IntPtr.Zero, IntPtr.Zero);
if(!isSuccess)
return false;
else
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadKey();
}
}
return false;
}
private static IntPtr SetCurrProcPriv(string thePrivilegeName)
{
IntPtr hToken =new IntPtr();
bool isSuccess=false;
LUID luid = new LUID();
IntPtr aCurrProc = GetCurrentProcess();
TOKEN_PRIVILEGES aTP=new TOKEN_PRIVILEGES();
STARTUPINFO si=new STARTUPINFO();
si.cb = (int)Marshal.SizeOf(si);
isSuccess = OpenProcessToken(aCurrProc,
TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES |
TOKEN_ADJUST_SESSIONID |TOKEN_ADJUST_DEFAULT |
TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE, out hToken);
if(!isSuccess)
Console.WriteLine("Error OpenProcessToken " + Marshal.GetLastWin32Error().ToString());
isSuccess = LookupPrivilegeValue(IntPtr.Zero, thePrivilegeName, ref luid);
if(!isSuccess)
Console.WriteLine("Error OpenProcessToken " + Marshal.GetLastWin32Error().ToString());
aTP.PrivilegeCount = 1;
aTP.Privileges = new int[3];
aTP.Privileges[0] = luid.LowPart;
aTP.Privileges[1] = luid.HighPart;
aTP.Privileges[2] = SE_PRIVILEGE_ENABLED;
isSuccess = AdjustTokenPrivileges(hToken, false, ref aTP, Marshal.SizeOf(aTP), IntPtr.Zero, (IntPtr)Marshal.SizeOf(aTP));
if(!isSuccess)
return IntPtr.Zero;
else
return hToken;
}
</string></string>
PhrankBooth
|
|
|
|
|
Did you try this[^]?
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi,
This is how I am opening an excel spreadsheet.
But when it is opened, I do not see the addins
Is there a parameter to set to leave the addins as it is?
Microsoft.Office.Interop.Excel.Application EApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Worksheet WS = ((Microsoft.Office.Interop.Excel.Worksheet)(EApp.Workbooks.Open(@"C:\Data.xls",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing).ActiveSheet));
EApp.Visible = true;
|
|
|
|
|
Hi
I have to read all the link lines in HTML file. If the links path is not already there, then append link. I have done like below but I could not append the link if I use my code.
StreamReader sr = File.OpenText(navigationFile);
String line = sr.ReadToEnd();
if (!(line.IndexOf(pubPath) > 1))
{
sr.Close();
using (StreamWriter sw = File.AppendText(navigationFile))
{
sw.WriteLine("
");
sw.WriteLine("");
sw.WriteLine(pubTitle);
sw.WriteLine("");
sw.Close();
}
Does any one have Idea about it.
thanks.
|
|
|
|
|
Are any changes being appended to the file? Are any exceptions being thrown?
From your code, you are appending pubTitle, not pubPath (which is the one you are searching for).
Also, you should probably use the String.Contains[^] method. Like so:
if (!line.Contains(pubPath)) {
}
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi Tom,
thank you very much for your reply. I missed the anchor tags before and after the pubtitle while uploading the post. I have looked for Contains(), I dont have Contains() for String.
Is there any other way.
|
|
|
|
|
Are you using .Net 1.x? Contains is supported in .NET Framework 3.5, 3.0 SP1, 3.0, 2.0 SP1, 2.0.
But you can use String.IndexOf[^], like you had before. But you should be checking for -1, because 0 and all positive integers indicates that it found the string.
if (-1 == line.IndexOf(pubPath)) {
}
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
I am using in .net compact framwork 2.0.
|
|
|
|
|
I am attempting to write a new class, System.UInt128, and to do so I am trying to get the carry-out information for arithmetic operations such as long +/- long (1 bit carry), and long * long (long carry). Is there any way to get this information from within C#? (and for those of you who will suggest that I just use decimal, I cannot do so because I need 128 significant bits, and decimal only offers 96). Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Check this[^] out.
I'm not sure if there is a download link for the source, but it appears to all be in the article (so you can piece it together). You can probably get it directly from the author also.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Thanks.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
The J# dll has the BigInteger class.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
|
Check this[^] out. It should get you started.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
thanks,
it is interesting.
but have you also some sample with explicit lines between lists ?
lines may be not horisontal, what is common practice.
|
|
|
|
|
I'm afraid not. You will have to modify the drawing code to include the lines yourself.
There are some commerical versions also (here[^] is one).
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
thanks.
I would probably use and extend proposed controls...
It is some pity, that I was seen such controls but can not find it now...
It was probably some kind of Splitter.
|
|
|
|
|
Hello to All,
Please help me to find out what type of code required for Displaying Word document in my aspx page.
Thanking to all in advance.
BHAVESH PATEL
|
|
|
|
|