|
There is a Job board here at CP. Use that.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
When I execute and exe using CreateProcessAsUser API it will shows the dialog with below error.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at SVCManager.clsSQLConn..ctor()
at SVCManager.frmSVCManager.frmSVCManager_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Can any one guide me?
Thanking You,
Sunil G.
|
|
|
|
|
For what you say, "No". There is not enough information. Put a breakpoint at the top of the try block, and single step through until the exception occurs.
What it is saying is that you are (probably) handling an object in your SVCManager constructor which is null. Without more info, it is not possible to help.
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Here is the code I am using to execute an executable file.
<code>
namespace ExecuteAnExeUsingCurrentLoggedOnUser
{
class Program
{
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public Int32 dwX;
public Int32 dwY;
public Int32 dwXSize;
public Int32 dwXCountChars;
public Int32 dwYCountChars;
public Int32 dwFillAttribute;
public Int32 dwFlags;
public Int16 wShowWindow;
public Int16 cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public Int32 dwProcessID;
public Int32 dwThreadID;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public Int32 Length;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
public enum SECURITY_IMPERSONATION_LEVEL
{
SecurityAnonymous,
SecurityIdentification,
SecurityImpersonation,
SecurityDelegation
}
public enum TOKEN_TYPE
{
TokenPrimary = 1,
TokenImpersonation
}
public const int GENERIC_ALL_ACCESS = 0x10000000;
public const int TOKEN_DUPLICATE = 2;
public const int TOKEN_QUERY = 0X00000008;
public const int TOKEN_IMPERSONATE = 0X00000004;
[
System.Runtime.InteropServices.DllImport("kernel32.dll",
EntryPoint = "CloseHandle", SetLastError = true,
CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)
]
public static extern bool CloseHandle(IntPtr handle);
[
DllImport("advapi32.dll",
EntryPoint = "CreateProcessAsUser", SetLastError = true,
CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)
]
public static extern bool
CreateProcessAsUser(IntPtr hToken, string lpApplicationName, string lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandle, Int32 dwCreationFlags, IntPtr lpEnvrionment,
string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation);
[
DllImport("advapi32.dll",
EntryPoint = "DuplicateTokenEx")
]
public static extern bool
DuplicateTokenEx(IntPtr hExistingToken, Int32 dwDesiredAccess,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
Int32 ImpersonationLevel, Int32 dwTokenType,
ref IntPtr phNewToken);
[
DllImport("advapi32", SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute
]
public static extern int
OpenProcessToken(
System.IntPtr ProcessHandle,
int DesiredAccess,
ref IntPtr TokenHandle
);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool OpenProcessToken(IntPtr ProcessHandle,
UInt32 DesiredAccess, out IntPtr TokenHandle);
static void Main(string[] args)
{
CreateProcessAsUser1("c:\\ServiceManager.exe", "");
}
private static void CreateProcessAsUser1(string strApplication, string strArg)
{
IntPtr hToken = IntPtr.Zero;
Process[] proc = Process.GetProcessesByName("explorer");
if (OpenProcessToken(proc[0].Handle,
TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE,
ref hToken) != 0)
{
}
IntPtr hDupedToken = IntPtr.Zero;
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
try
{
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.Length = Marshal.SizeOf(sa);
bool result = DuplicateTokenEx(
hToken,
GENERIC_ALL_ACCESS,
ref sa,
(int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
(int)TOKEN_TYPE.TokenPrimary,
ref hDupedToken
);
if (!result)
{
throw new ApplicationException("DuplicateTokenEx failed");
}
STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
si.lpDesktop = String.Empty;
result = CreateProcessAsUser(
hDupedToken,
strApplication, strArg,
ref sa, ref sa,
false, 0, IntPtr.Zero,
@"C:\", ref si, ref pi
);
if (!result)
{
int error = Marshal.GetLastWin32Error();
string message = String.Format("CreateProcessAsUser Error: {0}", error);
throw new ApplicationException(message);
}
}
catch (Exception ex)
{
MessageBox.Show("Inner Catch " + ex.Message.ToString());
}
finally
{
if (pi.hProcess != IntPtr.Zero)
CloseHandle(pi.hProcess);
if (pi.hThread != IntPtr.Zero)
CloseHandle(pi.hThread);
if (hDupedToken != IntPtr.Zero)
CloseHandle(hDupedToken);
}
}
}
}</code>
|
|
|
|
|
Firstly, when you paste code, there are two things to remember:
1) Use the "code block" widget - it preserves the formatting so the code is more readable:
finally
{
if (pi.hProcess != IntPtr.Zero)
CloseHandle(pi.hProcess);
if (pi.hThread != IntPtr.Zero)
CloseHandle(pi.hThread);
if (hDupedToken != IntPtr.Zero)
CloseHandle(hDupedToken);
}
becomes:
finally
{
if (pi.hProcess != IntPtr.Zero)
CloseHandle(pi.hProcess);
if (pi.hThread != IntPtr.Zero)
CloseHandle(pi.hThread);
if (hDupedToken != IntPtr.Zero)
CloseHandle(hDupedToken);
} And use the Preview button to check it looks ok. Unformatted code it harder to read so people will just gloss over it.
2) Only post the relevant code fragment. A large, several page fragment such as yours is mostly irrelevant and gets in the way.
OK, the problem:
Where is the exception occurring? Without actually running the code (which I am not about to do without a lot more detailed examination) no-one can tell. Which line is the exception being raised on?
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
First of all sorry for what i have posted.
Exception is not occuring in my application. I have debugged my application, it never goes to catch block. But when below line of code get executed then only it gives an error (JIT dialog box). The function runs successfully. The function returns a non-zero value.
result = CreateProcessAsUser(hDupedToken,
strApplication, strArg,
ref sa, ref sa,
false, 0, IntPtr.Zero,
@"C:\", ref si, ref pi
);
Thanking You,
Sunil G.
modified on Tuesday, January 19, 2010 6:24 AM
|
|
|
|
|
Sunil G wrote: First of all sorry for what i have posted.
No problem!
Sunil G wrote: Exception is not occuring in my application. I have debugged my application, it never goes to catch block. But when below line of code get executed then only it gives an error (JIT dialog box). The function runs successfully. The function returns a non-zero value.
Right, I see! the problem is not in the app you are debugging - it is in "ServiceManager.exe" - you need to try running that outside your app with no parameters and see what happens - it is failing for some reason. It may be that it needs better permissions, it may need a DLL it can't find, it could be anything!
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
When I manually execute the exe it runs without any error.
what kind of permissions I have to set?
|
|
|
|
|
Sunil G wrote: what kind of permissions I have to set?
I don't know! Can't you just use Process.Start? Since you don't seem to be specifying a username you (presumably) are running as the current user. Or am I missing something?
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Hi Griff,
I admire you being persistent about PRE tags; maybe you should add one more sentence, something like "you could still edit your existing message and add PRE tags in; please do so".
Cheers.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, when do we get it on regular forums?]
|
|
|
|
|
Hi Luc,
The last time I did that, I got the same multipage listing, but in tags!
finally
{
if (pi.hProcess != IntPtr.Zero)
CloseHandle(pi.hProcess);
if (pi.hThread != IntPtr.Zero)
CloseHandle(pi.hThread);
if (hDupedToken != IntPtr.Zero)
CloseHandle(hDupedToken);
}
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
I have modified my previous post. Please check it.
Thanking You,
Sunil G.
|
|
|
|
|
I am attempting to populate a ContextMenuStrip (or any ToolStripDropDown for that matter) with ToolStripMenuItem s that have AutoSize set to False and a specific Height and Width . However, I cannot get the ContextMenuStrip to AutoSize (which is set to True on the ContextMenuStrip ). The Height appears to be resized, but the Width cuts off the menu items. I have tried using varying values for Margin , Padding , AutoSize , Width , Height ... etc... but to no avail. Any ideas?
private void AddMenuItem (ContextMenuStrip oContextMenuStrip, string sText)
{
oContextMenuStrip.AutoSize = true;
ToolStripMenuItem oMenuItem = new ToolStripMenuItem(sText);
oMenuItem.AutoSize = false;
oMenuItem.Width = 300;
oMenuItem.Height = 40;
oContextMenuStrip.Items.Add(oMenuItem);
}
|
|
|
|
|
You can adjust width by yourself, use Graphics.MeasureString to get the width of the menu item and set width manually.
April
Comm100 - Leading Live Chat Software Provider
modified 27-May-14 8:52am.
|
|
|
|
|
Yes, I had tried that. However, it did not accomplish what I wanted to achieve. The menu item will be completely custom drawn, but there is some text that I'll need to assure will fit. So, I calculated the width of a space in the menu item font, measured that text that will be rendered to it (though text measuring in .NET is sometimes inaccurate), and set the menu item text to all spaces (see code below). However, every menu item in that drop down will have the same height now if there is more than one line. And, we have no way of making the menu item an exact dimension since we can only affect width/height using spaces and newlines in the menu item text. Ideally, I'd like to know if there is a clean way to resolve such drop-down menu and menu item dimension inconsistencies. Perhaps a "DoWhatIWant" flag?
private void AutoSizeCustomMenuItem(ToolStripMenuItem oMenuItem, string sText)
{
int iPreferredHeight = 50;
int iPreferredWidth = 0;
int iPadding = 50;
SizeF oSizeSpace = SizeF.Empty;
using (Graphics oGraphics = oMenuItem.Owner.CreateGraphics())
{
iPreferredWidth = (int)oGraphics.MeasureString(sText, oMenuItem.Font).Width + iPadding;
oSizeSpace = oGraphics.MeasureString(" ", oMenuItem.Font);
}
int iSpacesWidth = (int)Math.Ceiling((double)(iPreferredWidth / oSizeSpace.Width));
int iSpacesHeight = (int)Math.Ceiling((double)(iPreferredHeight / oSizeSpace.Height));
string sSpacesWidth = new String(' ', iSpacesWidth);
StringBuilder sbMenuItemText = new StringBuilder();
if (iSpacesHeight > 0)
{
string sLineDivider = String.Empty;
for (int i = 0; i < iSpacesHeight; i++)
{
sbMenuItemText.Append(sLineDivider);
sbMenuItemText.Append(sSpacesWidth);
sLineDivider = "\r\n";
}
}
oMenuItem.Text = sbMenuItemText.ToString();
}
|
|
|
|
|
hi
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
// in this line of code:
ExcelApp.Application.Workbooks.Add(Type.Missing);
error:
Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
can any one help me ?
H.R
|
|
|
|
|
1. You can export datatable that you use to bind your datagridview through OleDb into Excel file. There won't be any need of interop.
2. Did you googled the error message?
3. What are trying to do in that line of code?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Okay, I'm sure someone out there besides me has wondered this so here goes.
Lets say your like me and learning c# or your a professional either way. If your working on a project and lets say you get it all working then someone wants something different added or deleted. How do you go about editing the working code without risking screwing up the whole project?
What I mean is this. Lets say program1 had a button and you build the project and all works fine. Then later on you decide you don't want the button so you open up the project and remove it. If all crap breaks loose and it no longer runs how would you revert back to the old so you can try something different?
I have been taking and raring the project folder as a backup and then just working from there but is there a better way to do this ?
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:54am.
|
|
|
|
|
thanks. One last question before I call it quits for the night.
On the toolstrip one can have add delete save whatever.
Well if I remove the delete button how in the heck can i get it back or do I have to delete the toolstrip
and start over? Or is there an easy way to add an event that does the same thing as the delete?
Like databindingsource.delete(current) or whatever? Sorry for all the questions I'm just trying to understand this stuff.
|
|
|
|
|
If you use a source control solution you'll be able to "commit" your working code. If you then decide to remove the "delete" button and change your mind on that, you can simply put back the old code using you source control program and the button will be there again.
I have no idea what it is that you want to delete, but if you need to duplicate the function of your delete button you can just copy the code in the event handler for it's click event to the place you need it to be. If this concept is unclear you might want to pick up a book on c# as this is very very basic stuff.
Standards are great! Everybody should have one!
|
|
|
|
|
Source control is what you need. There are a number of free ones and even more that cost you. Just google for source control and you will find loads. This is how the vast majority of commercial development is controlled.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
I want save some information(gps data and coordinates and ...) in text file and then export this text file to .m file (for useing in matlab7.1).
|
|
|
|
|
Good, go ahead and do it. Or was there a question somewhere that I missed?
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Howdy,
I'm working with an widget that is getting key strokes as (System.Windows.Forms) KeyEventArgs. In particular I'm currently working on controlling input from the numeric keypad on that widget.
When this widget gets input from the numeric keypad, I want to ignore whether or not caps lock is on and always enter a number: pressing numeric keypad 4 should enter a number 4 whether or not num lock is on.
I don't think I can just ignore the num lock status (although I'm open to suggestions), so I'm trying to come up with a way to work with the KeyEventArgs. I'd like to determine how I can map the KeyEventArg that the widget gets and send a number instead.
The problem is:
I can't find anyway to differentiate the numeric key pad 4 with num lock off (that sends a '<-') from the actual '<-' key input.
I also can't tell the difference between the numeric key pad 7 with num lock off (that sends 'home') from the 'home' key input.
Its obvious how I could go the other way and tell the difference between the numeric keypad 4 with num lock on (sending a 4 or more specifically a Keys.Num4) and the '4' key (that would send Keys.D4).
Is it possible to differentiate between control characters sent from the numeric key pad and the normal control keys? Is there another way I might get the effect I'm looking for?
(Hope that's readable, that whole key pad 4 with num lock off is a bit wordy, so ask me to clarify it if you can't follow).
Thanks,
Chris
modified on Monday, January 18, 2010 6:30 PM
|
|
|
|
|