|
please correct me if anything i state is incorrect about ref in C#. i'm trying to make sure i understand these 100%.
- using a ref parameter in C# for VALUE types (eg ref Int32 i) is basically the same thing as using "int* pInt" in C++.
- for reference types, C# defaults to passing references, so any function in C# (Without ref or out keyword) when passing references is basically also "classType* pObj" in C++.
- when using the REF keyword with a REFERENCE type in C#, this is essentially the same as pointers to pointers in C++. that is, in C# a function "void func (ref ClassType p)" would be written equivalently in C++ as "void func (ClassType** p), and when p is used in the C# function, such as p.memberFunction(), the equivalent in the C++ function would be (*p)->memberFunction(). in other words, in C# the levels of dereferencing are hidden from the programmer, wheras in C++ you must dereference the proper levels yourself.
is my understanding of how C# implements references correct? thanks.
|
|
|
|
|
Yes, but with one other thing. If you reassign a parameter reference without ref or out , the variable that was passed to the method will not reference the new object.
With this comes something very important, one of the few things that can truly crash the CLR. If you are P/Invoking a function that takes an LPTSTR or something as an [out] variable, do not use out or ref in your managed declaration (unless the function parameter type is LPTSTR* ). To make sure the marshaler knows to reassign the parameter, use the OutAttribute instead.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
you mean like so:
void function (MyClass myObj)
{
myobj = new MyClass();
}
so in this case, it appears that after the function call the reference to the new MyClass obj is lost and is just garbage collected, and the ref passed in would remain referring to whatever it was before the function call. right?
|
|
|
|
|
Yes, that is correct. Consider the following example:
using System;
class Test
{
static void Main()
{
Exception e = new Exception("From Main()");
Console.WriteLine(e.Message);
A(e);
Console.WriteLine(e.Message);
B(ref e);
Console.WriteLine(e.Message);
}
static void A(Exception e)
{
e = new Exception("From A()");
}
static void B(ref Exception e)
{
e = new Exception("From B()");
}
} The output would be:
From Main()
From Main()
From B() Exception was just the first object that came to mind, but any reference type - and in this case, value types - would do.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
This is a side question, but so similar I didn't want to start a whole new thread for it. I have a Managed DLL that takes a int* but I can't seem to pass anything that works (ref, out) it's a Managed DLL so I'm not using Pinvoke, but I am wondering if there is a way to do this without using unsafe code or changing the Managed DLL code (Although that probably what I'll have to do).
Thanks!!
|
|
|
|
|
An int* in unmanaged code (even though your targeting the CLR with your C++ compiler) is still a pointer to a native int, not a managed int. Try using System::Int32* . If that doesn't work, I recommend you ask in the Managed C++ forum. I've used MC++ in the past but not a whole lot. Sorry.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm developing add-in for VS and am having extreme trouble with getting ConnectionString from node in Server Explorer. I've done most of the job getting one of the node as UIHierarchyItem but I can't cast either it or it's .Object to IDispDataVwDataConn which is type when I select node and then look into property window (I found that interface in VS7DataProject)...
Maybe the problem is that the object I am casting is a COM object, the object I am casting to is a .Net wrapper - they are not really the same thing, though I was hoping all the gory details would have been taken care off for me (interop being so seemless). If anyone has a suggestion as to how to get said COM object to cast to the .Net interop type or some other way of accessing the COM object's properties directly...then that would possibly solve the whole problem....
Tnx in advance to anyone who tries to help...
|
|
|
|
|
The IDispDataVwDataConn interface must be declared in your managed code (resumably C# since you're in this forum) with the ComImportAttribute (so that the CLR QI's for the interface instead of doing a type cast), the correct GuidAttribute (with the right IID), and the InterfaceTypeAttribute with the correct value that dictates from which interface(s) the interface in question derives. The methods must also be in the correct order (when derived from IUnknown or a dual interface) and have the correct DispIdAttribute values (when derived from IDispatch or a dual interface).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
First of all tnx...
Second - am still at newbee level I guess as I can't get this thing working... So if you can I would be grateful if you could provide more pointers or answers to my questions. I included code I tried to made.
Question:
---------
Is IDispDataVwDataConn really the one needed to be set with all those attributes and defined in mine project? As it is already defined in VS7DataProject and it's managed code.
UIHierarchyItem.Object is COM object...
Code:
-----
<br />
public class TempClass<br />
{ <br />
EnvDTE containerApplication; <br />
ComboBox cmbConnection; <br />
<br />
void LetsPresumeThisFunctionGetsCalledWhenItsNeeded()<br />
{<br />
EnvDTE.Window win = containerApplication.Windows.Item(Constants.vsWindowKindServerExplorer);<br />
UIHierarchyItem uih = ((UIHierarchy)win.Object).GetItem("Data Connections");<br />
<br />
if (uih.Collection.Count > 0)<br />
{<br />
cmbConnections.Enabled = true;<br />
<br />
foreach(UIHierarchyItem item in uih.UIHierarchyItems)<br />
{<br />
} <br />
} <br />
} <br />
}<br />
tnx again mate...
|
|
|
|
|
I wasn't able to find the assembly that defines the IDispDataVwDataConn interface you mentioned to make sure it is attributed correctly, but there is the Microsoft.VSDesigner.dll assembly (in the Common7\IDE sub-directory of your VS.NET installation directory) that has the IDataConnectionsService which could accomplish the same thing.
Perhaps if you told me what the full assembly name is and where I might find it (I've looked to no avail) I might be able to help you better.
Make sure you also have referenced that assembly in your project, otherwise the interface won't be found when compiling (but I'm assuming you've done that and are having runtime problems).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Add Reference => COM => Visual Studio.NET Data Projects (DLL can be found at <path to="" vs.net2003="" dir="">\Common7\Tools\VDT\vdt70p.dll).
I also saw that interface (IDataConnectionsService) but I guess it can do no good as first one is used. If you wish to see what I mean start VS.NET => Server Explorer => expand Data Connections node => right click on any exiting one => Properties => Next to name of is type and it's IDispDataVwDataConn.
Cheers mate...
|
|
|
|
|
Since it's being imported from a typelib, it is a COM wrapper (RCW) and is an imported interface (just to be sure it was correct, I double-checked). Casting the Object property should work (and obviously doesn't). Have you tried just casting the node itself?
If anything, there is a developer forum for the VSIP (developing and integrating components with VS.NET itself) on the MSDN[^] web site somewhere. You might consider that approach.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
As said in first post I tried casting both Object and node... unsuccesfuly. Also I found extensibility newsgroup (microsoft.public.vstudio.extensibility) with same subject but no answer (guys came as close as I did). Google search => also founde one unfinished discussion. So I guess I should just quit it...?
|
|
|
|
|
how do I check if a file with a given path is read only?
|
|
|
|
|
Use =>
System.IO.File.GetAttributes(string pathToFile);
and then compare it using System.IO.FileAttributes enum...
|
|
|
|
|
But the following lines never give anything but '1'. How am I to distinguish between read-only and read/write files?
System.IO.FileAttributes att = System.IO.File.GetAttributes( this.currentFolder);
MessageBox.Show(att.CompareTo(System.IO.FileAttributes.ReadOnly)+"");
|
|
|
|
|
Bitwise conditions:
FileAttributes attribs = File.GetAttributes("file.txt");
if ((attribs & FileAttributes.ReadOnly) != 0) Console.WriteLine("Read-only");
else Console.WriteLine("Read-write");
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you, but now I only get 'False'. I'm changing the file's read-only attribute by hand because my program does not create or write to these files directly, but my program does need to know if the file it's dealing with is read-only or not. Any suggestions?
|
|
|
|
|
Judging by your variable name, you're trying to get attributes for a directory. Directories don't have a read-only attribute. Only their containing files do. In fact, directories don't really have many attributes at all.
Trust me, the code works. We use it a lot in our application. You might also want to take a look at the FileInfo class which provides the same information in a different way.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
your right; I grabed the wrong variable. Thank you.
|
|
|
|
|
I have a placeholder sitting on a page with a button above it. When the button is clicked, I want it to add a textbox and to the placeholder so I have the following code execute for the onclick...
<br />
TextBox temp = new TextBox();<br />
<br />
temp.TextMode = TextBoxMode.MultiLine;<br />
temp.CssClass = "txtArea";<br />
<br />
temp.ID = "txtArea" + phModNotes.Controls.Count.ToString();<br />
<br />
phModNotes.Controls.Add(temp);<br />
The expectation being that the controls will be named sequentially starting at txtArea0 and counting up. However, everytime I click the button, phModNotes gets reloaded and is empty.
|
|
|
|
|
|
Hello all.
I have encountered some difficulties while writing a program that is supposed to use memcpy. To be honest, I can't get it to work at all, and I don't have a clue what I'm doing wrong.
Here's the most important part of the code:
<br />
DllImport("msvcrt.dll")]<br />
public static extern unsafe void memcpy(void* to, void* from, int len);<br />
<br />
[StructLayout(LayoutKind.Explicit)]<br />
private struct Buffer<br />
{<br />
[FieldOffset(8)]<br />
public byte Buff0;<br />
[FieldOffset(0)]<br />
public int Length;<br />
FieldOffset(4)]<br />
public int Start;<br />
<br />
private static unsafe void CopyToBuffer(Buffer* buffer, byte* data, int len){<br />
if ((( buffer.Length + buffer.Start) + len) >= 262144)<br />
{<br />
throw new NullReferenceException(string.Format("Buffer overflow, or something"));<br />
}<br />
memcpy(((&buffer.Buff0 + buffer.Start) + buffer.Length), data, len);<br />
buffer.Length += len;<br />
}<br />
}<br />
The errors I get are
"Operator '.' cannot be applied to operand of type Buffer*".
I thought that I declared buffer.Lenght as int, so why isn't it working?
I call CopyToBuffer like:
<br />
buffx = p.Finalize();
unsafe {<br />
fixed (byte* buff= buffer1)<br />
{<br />
CopyToBuffer(m_Buffy, buff, buffx.Length);<br />
}<br />
}<br />
return;<br />
m_Buffy is declared as
<br />
private unsafe static Buffer* m_Buffy;<br />
and gets its value on runtime (the address of the recieve buffer).
It's a pretty simple thing, really. I just want to send a text message to a process through its recieve buffer.
I'm a little new to all of this, so I haven't yet fully gotten then hang of API hooking, dll injection and packet filtering and all that which migth have aided me here, but I do my best.
Any response is deeply appreciated.
|
|
|
|
|
Before using memcpy (or whatever variation you may be using), I'd strongly suggest you to take a look at Marshal.Copy method.
Yes, even I am blogging now!
|
|
|
|
|
Dear Friends
I m making Internet Club Management Software, i Want to ask 2 question's Please help me
How I can Stop / Execution of the Software ruuning on Client Machine.???
How I can Block IP/or unblock IP and banning a site on a client????
|
|
|
|