|
|
A Windows user with regular "user" permissions is running my C# program, and it's not able to access HKEY_LOCAL_MACHINE/Software/Blah
Does that sound right?
What are the permissions rules for HKEY_LOCAL_MACHINE?
Thanks,
Elena
|
|
|
|
|
By default, everyone has permissions to read HKLM, and that's the way it should be. Users should only be able to write changes to their hive (HKEY_CURRENT_USER). If you have user-specific settings, that's where they should go.
Actually for .NET applications, nothing should go in the registry in a typical scenario. You typically use the .config file for application settings. If you have user settings, you can save them in a user's isolated storage (see the System.IO.IsolatedStorage namespace in the .NET Framework SDK). This allows for touchless deployment, or XCOPY deployment as it's sometimes called.
At the very least, your application should be sensitive to the fact that settings in the registry might not exist, using default values or displaying user-friendly errors where appropriate.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am trying to read HKLM, and it's not letting me.
Elena
|
|
|
|
|
If you're getting a specific problem, be specific. What exception is being thrown? If you're handling the exception, then break into your debugger and tell us. "it's not letting me" tells me nothing.
Also, see my other post. If this code is not running from the local machine (or the CAS user policy is more limited), code access security may be preventing the call to the registry class methods.
Finally, I told you what the default permissions are. If you want to know what they are on your machine, then open regedit.exe, right-click on HKEY_LOCAL_MACHINE and select Permissions (if you don't see that, use regedt32.exe instead since this was a new UI feature in recent Windows OSes).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Oh, and one more thing: depending on the source of the executing app, code access security may be preventing calls as well. If the application is running from the Intranet zone, it's granted limited permissions (no registry access at all). For the Internet zone, either no permissions are granted (.NET 1.0) or very few are granted (.NET 1.1).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have created a user control, on which I have place an MDI Client control. I next added my control to a project. Now, I want to open new Forms inside of the Client. Here is my problem:
childForm myChild = new childForm();
mdi1.m.Controls.Add(myChild);
<br />
myChild.Show();
The error occurs here:
<br />
mdi1.m.Controls.Add(myChild); <br />
this is the error:
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Can only add MDI child forms to an MdiClient.
The only way I can currently get around this is to make my main form an MDI Parent, and add this code before adding the form to the user control:
<br />
myChild.MdiParent = this; <br />
Then, when I try to add the form to the user control, it works beautifully. I can only assume that this is because the Form.IsMdiChild property is set to TRUE.
But, I need to do this without first adding it to an MDI Parent (making my main form an MDI Parent.)
So my question is:
1) Is there a way to write to the Form.IsMdiChild value?
or
2) Is there another way to do this?
Hopefully, this problem will interest somebody... Thanks.
Agent 86
|
|
|
|
|
Agent 86 wrote:
1) Is there a way to write to the Form.IsMdiChild value?
Nope. This is a read only property settable at design time only.
Agent 86 wrote:
2) Is there another way to do this?
Nope. You can't have an MDI Parent be a child of another MDI Parent, which is what you are trying to do. A form can either be an MDI Parent OR an MDI Child, but not both at the same time. There is no way around this...
RageInTheMachine9532
|
|
|
|
|
Hi,
I'm trying to create a dataset with an xsd file but get a 'System.InvalidOperationException' when I try to fillSchemain DataAdapter. It's prbably failing because I didn't set a connection to Dataadapter but I don't have a connection to database because this is offline. How can I get the Schema into the dataset? Here's my code:
<br />
DataSet IpacDS = new DataSet("Midnight"); <br />
<br />
try<br />
{<br />
IpacDS.ReadXmlSchema(sXmlFileName);<br />
SqlDataAdapter da = new SqlDataAdapter();<br />
da.FillSchema(IpacDS, SchemaType.Source); <-- Fails here<br />
<br />
<br />
<br />
<br />
|
|
|
|
|
Why are you even trying to do this? FillSchema builds the schema for the DataSet by using the SelectCommand . If you've already got your schema (referenced by sXmlFileName ) and are reading that into the DataSet using ReadXmlSchema , that's all you need to do. Just make sure that schema and what you'd pull from the database (yes, which requires a connection otherwise the schema can't be determined) are the same (or that the latter is a subset of the former).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I solved my problem. If I name the table in the Dataset as something other than MN, it would fail on the line I mention. Why is that so?
Here is the code I used:
<br />
<br />
DataSet IpacDS = new DataSet("MN"); <br />
<br />
try<br />
{<br />
IpacDS.ReadXmlSchema("MN.xsd");<br />
DataRow nRow = dsDiff.Tables["MN"].NewRow(); <- Would Fail here if I didn't name it MN.<br />
<br />
<br />
Any ideas why ?
Thanks,
JJ
|
|
|
|
|
Because no DataTable would be found named "MN", so dsDiff.Tables["MN"] would return null . You can't call NewRow() on null , hence the NullReferenceException you would've received. This is the result of improper checks and exception handling.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello,
i'd like to sort the child items of a node of a Windows Forms TreeView control using a function of my own that set if the node is to be listed before or after another. Every node has a logic code that i've stored internally using a derived class of TreeNode and i'd like to sort the item by this one.
In MFC there's the possibility of defining a logic compare function, is there anything similar in .NET? Or there's another way to add a new child node of an item and sort all its childs without removing them and reloading ?
Thanks,
Gianmaria
|
|
|
|
|
In the TreeView class, there is no such property or method for you to provide your own sorting algorithm. The Tree-View common control - which the TreeView class encapsulates - does, however. See the documentation for the TVM_SORTCHILDRENCB message in the Platform SDK.
You can fill TVSORTCB struct (which you must create in C#) with data from the parent tree node (use TreeNode.Handle ), a callback function (shouldn't need to be pinned using the GCHandle since the SendMessage call would be synchronous), and any other data you want to pass (like an integer value that specifies whether to sort ascending or descending).
The struct and callback would look like this:
[StructLayout(LayoutKind.Sequential)]
private struct LVSORTCB
{
public IntPtr TreeItemHandle;
public TreeViewSortCallback Callback;
public IntPtr ExtraData;
}
public delegate IntPtr TreeViewSortCallback(IntPtr node1, IntPtr node1,
IntPtr extraData); The return Type is an IntPtr because an unmanaged int is processor-dependent (32 bits on a 32-bit proc, 64 bits on a 64-bit proc). This makes it more portable.
To call it, you need to P/Invoke SendMessage :
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int msg,
IntPtr wParam,
ref TVSORTCB sort);
public void Sort(TreeNode parent, TreeViewSortCallback callback, bool ascending)
{
TVSORTCB sort = new TVSORTCB();
sort.TreeNodeHandle = parent.Handle;
sort.Callback = callback;
sort.ExtraData = new IntPtr(ascending ? 0 : 1);
IntPtr hWnd = treeView1.Handle;
SendMessage(hWnd, 4373, IntPtr.Zero, ref sort);
} Create a new TreeViewSortCallback delegate that references your function and call such a method. You can use your sorting algorithms in there.
If you want to get really fancy, you could encapsulate this all into a derivative TreeView class, perhaps called SortableTreeView or something, and let the caller pass an IComparer implementation. With a good design, you could couple the interface (common throughout the .NET FCL) with the callback (making the callback private since IComparer is the typically sorting implementation in .NET - consistency is important).
Of course, you could always use your algorithms and manually move nodes around, but then you're responsible for all the work. Using the approach above makes the Tree-View common control do it with very little information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thanks for the infos. i've implemented a sort function like this in my TreeView class:
<br />
public IntPtr Compare(IntPtr node1, IntPtr node2, IntPtr extraData)<br />
{ <br />
TreeNode node_1 = TreeNode.FromHandle(this, node1);<br />
TreeNode node_2 = TreeNode.FromHandle(this, node2);<br />
IntPtr ret= new IntPtr(0);<br />
long prog1 = ((myTreeNode)node_1).item_type;<br />
long prog2 = ((myTreeNode)node_2).item_type;<br />
if(prog1 < prog2) { ret = new IntPtr(-1); }<br />
if(prog1 > prog2) { ret = new IntPtr (1); }<br />
return ret;<br />
}<br />
and call the sort function
Sort(parent_node, new TreeViewSortCallBack(Compare), 0);
at that point, the Compare function is called but both node1 and node2 are zero valued. is because those values are not the nodes but the ItemData like in MFC?
|
|
|
|
|
As I mentioned before (and that is also documented for the TVSORTCB struct, which you should read), the first two params are the TVITEM structs. You must declare the TVITEM struct in your code and either use Marshal.PtrToStructure , or better yet declare the delegate as:
delegate IntPtr CompareFunc(ref TVITEM item1, ref TVITEM item2, IntPtr userData); This causes the CLR to marshal the references to the TVITEM structs so you don't have to. You then can get the TreeNode s using the TVITEM.hItem fields (or whatever you call the second field in the TVITEM struct, which is the HTREEITEM (handle to a tree node).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i've tried in the 2 ways, with ref TVITEM and Marshal but it doesnt work. the first two parameters are always 0 or undefined (using ref TVITEM).
i've declared the structure
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct TVITEM<br />
{<br />
public IntPtr mask;<br />
public IntPtr hItem;<br />
public IntPtr state;<br />
public IntPtr stateMask;<br />
public IntPtr pszText;<br />
public IntPtr cchTextMax;<br />
public IntPtr iImage;<br />
public IntPtr iSelectedImage;<br />
public IntPtr cChildren;<br />
public IntPtr lParam;<br />
} <br />
<br />
my second test was with
<br />
public IntPtr Compare(IntPtr node1,IntPtr node2, IntPtr extraData)<br />
{<br />
TVITEM item1 = new TVITEM(); Marshal.PtrToStructure(node1, item1);<br />
TVITEM item2 = new TVITEM(); Marshal.PtrToStructure(node2, item2);<br />
TreeNode node_1 = TreeNode.FromHandle(this, item1.hItem);<br />
TreeNode node_2 = TreeNode.FromHandle(this, item2.hItem);<br />
but always the first 2 parameters are undefined. I've checked the value of extraData and it's correct, the same value i pass when call the sorting. I cant understand why the handle values of the 2 nodes are not passed to the callback function. However forcing the sorting of the two nodes, the tree updates correcly. But there's no way of testing the values of the 2 nodes.
I've created a little test application with only these things, and the behaviour is indentical
|
|
|
|
|
Are you passing a parent node when sending the TVM_SORTCHILDRENCB ? This should work.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
yeah, a node with 2 children ones. i've tested it with a messagebox and i'm sure it's the right node.
the function is
<br />
public void Sort(TreeNode parent, TreeViewSortCallback callback, bool ascending)<br />
{<br />
TVSORTCB sort = new TVSORTCB();<br />
sort.TreeItemHandle = parent.Handle;<br />
sort.Callback = callback;<br />
sort.ExtraData = new IntPtr(ascending ? 0 : 1);<br />
IntPtr hWnd = this.Handle;<br />
SendMessage(hWnd, 4373, IntPtr.Zero, ref sort);<br />
}<br />
I'm sure that it's the right call because, like i've said, i return -1 in the Compare function, the 2 nodes are inverted. Maybe there's some style of the TreeControl to set or similar?
|
|
|
|
|
No, there's not style to set. Again, read the documentation for the TVM_SORTCHILDRENCB [^] message and related topics linked in the page.
Upon further inspection, however, I noticed that the first two params are actually the lParam values of the TVITEM struct, which - believe it or not - is not set by the TreeNode.Tag property. You would have to override the TreeNodeCollection.Add(TreeNode) method with your derivative class and use new to override the Nodes property for both the TreeView and TreeNode classes to return your TreeNodeCollection derivative (they're not virtual ).
Your Add would be very similar to the current method, which you can see using .NET Reflector[^]. In fact, since some of those method calls are private or internal, you'll have to use base.Add(TreeNode) to get the index, and then global alloc the Tag to get an IntPtr then set the TVITEM using the TVM_SETITEM message.
You could do all this, or just sort the tree as I mentioned before yourself, which the TreeView class does instead of using the TVM_SORTCHILDREN message for the Tree-View common control.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i've implemented the thing in this way:
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct TVITEM<br />
{<br />
public int mask;<br />
public IntPtr hItem;<br />
public int state;<br />
public int stateMask;<br />
public IntPtr pszText;<br />
public IntPtr cchTextMax;<br />
public int iImage;<br />
public int iSelectedImage;<br />
public int cChildren;<br />
public int lParam;<br />
}<br />
<br />
class myTreeNode : TreeNode<br />
{<br />
public Int32 ItemType;<br />
public const int TV_FIRST = 0x1100 ;<br />
public const int TVM_GETITEM = TV_FIRST + 62 ; <br />
public const int TVM_SETITEM = TV_FIRST + 63 ;<br />
<br />
public Int32 item_type<br />
{<br />
set<br />
{<br />
this.ItemType = value;<br />
<br />
TVITEM item = new TVITEM();<br />
SendMessage(this.Handle, TVM_GETITEM, new IntPtr(0), ref item);<br />
item.lParam = value;<br />
SendMessage(this.Handle, TVM_SETITEM, new IntPtr(0), ref item);<br />
}<br />
get<br />
{<br />
return this.ItemType;<br />
}<br />
}<br />
<br />
[DllImport("user32.dll")]<br />
private static extern IntPtr SendMessage(<br />
IntPtr hWnd,<br />
[MarshalAs(UnmanagedType.U4)] int msg,<br />
IntPtr wParam,<br />
ref TVITEM item);<br />
<br />
after adding the node in the TreeView i set the item_type value with my number, and it automatically set the TVITEM lParam value. the problem is that TVM_GETITEM and TVM_SETITEM messages seem to not work the TVITEM is not filled but the first message, and the second doesnt set any values in the node. Is the TVITEM structure correct? i've tried also to declared all its properties to IntPtr but with no improvements.
|
|
|
|
|
There are actually two sets of TVM_GETITEM and TVM_SETITEM message for ASCII and Unicode, if you look at the CommCtrl.h:
private const int TV_FIRST = 0x1100;
private const int TVM_GETITEMA = TV_FIRST + 12;
private const int TVM_GETITEMW = TV_FIRST + 62;
private const int TVM_SETITEMA = TV_FIRST + 13;
private const int TVM_SETITEMW = TV_FIRST + 63; You need to send the message appropriate to the Windows platform you're running on:
int msg = TVM_SETITEMA;
if (Environment.OSVersion.Platform == PlatformID.Win32NT ||
Environment.OSVersion.Platform == PlatformID.WinCE)
msg = TVM_SETITEMW;
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
with both the two set of messages there's no retrieval of the TVITEM data and setting of my TVITEM data. It's always 0 all
It's hard to think it maybe work
|
|
|
|
|
I found the following in "winuser.h".
/*
* Move Size
* EVENT_SYSTEM_MOVESIZESTART
* EVENT_SYSTEM_MOVESIZEEND
* Sent when a window enters and leaves move-size dragging mode.
*/
#define EVENT_SYSTEM_MOVESIZESTART 0x000A
#define EVENT_SYSTEM_MOVESIZEEND 0x000B
I would like to know when the movesizeend event occurs. Can you show me some code on how to utilize this? I'm at a loss.
|
|
|
|
|
In your form, override the WndProc method.
You'll then have to check for message sent, if you need to know the message value, use the Spy++ tool to spy on the messages (and their corresponding message values) that are being sent to your app.
That said, there's probably managed events already raised for you, no need to dig into Win32. What exactly are you trying to accomplish? If you're trying to know when the form or some control has been resized, for example, managed events are already raised for you, and are much easier to implement handlers for.
#include "witty_sig.h"
|
|
|
|
|