|
From managed to unmanaged code, the size param is not needed; but, as I said, when going from unmanaged to managed it is because an array is nothing more than an allocated block of memory with no type-safety.
Use GCHandle (pinning the object when it's a reference object) instead of Marshal.AllocCoTaskMem . Use that mainly when COM request the use of CoTaskMemAlloc . A GCHandle pins the object in memory so the GC doesn't move it around, which is what you want here. All you have to do is free the GCHandle , instead of calling Marshal.FreeCoTaskMem . This unpins the object and allows the GC to move it around again.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for your advice.
So when a structure pointer is a member of another structure,can i also marshal the pointer member using marshalas too ,instead of using instptr.If so - can you give an example ,as all my attempts have failed.
Also I am yet not clear when I should prefer allocating an object into unmanaged heap(using marshal AllocCoTaskMem) ,rather than pinning it into managed heap and passing the address to unmanged code.
Sorry for the trouble.
|
|
|
|
|
It's already allocated - why allocate it again? For a reference object, all you need to do is pin it in memory (a value type doesn't need (and can't be) pinned since it will not be moved by the GC, although it could be reclaimed so still use a GCHandle or HandleRef to reference it).
You could try:
[StructLayout(LayoutKind.Sequential)]
internal struct S1
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
internal struct S2
{
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]public S1[] arr;
public int size;
} This will help the marshaler create the array when marshaling from unmanaged code.
Otherwise, you can use the GC handle to pin the S1[] array in memory till after the P/Invoke method returns:
S1[] arr = new S1[];
GCHandle handle = GCHandle.Alloc(arr, GCHandleType.Pinned);
S2 s2 = new S2();
s2.arr = (IntPtr)handle;
s2.size = arr.Length;
CallMyPInvokeMethod(ref s2);
handle.Free(); It's crude, but should hopefully give you a good idea.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If i try to marshal the pointer element using
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]public S1[] arr;
as you suggested - I am back to the
"This type cannot be marshalled as a structure field" error,which formed my original question.
|
|
|
|
|
Which is why I gave you the alternative to try below that.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a C# program the asks the user for a UserID, Password, and Domain and does a WinNT Active Directory query to return a list of groups the specified user belongs to. I start the program and enter user1/password 1 on domain 1 and it works just fine. But when I (in the same invocation) try to enter user2/password2 on domain 1 I get the following COM exception:
"Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed....."
I've tried closing and disposing the DirectoryEntry object after each use but to no avail. What do I have to do (close) to allow my program to do 2+ separate Active Directory queries for 2+ userid/password combos in the same invocation??
TIA,
Matt
|
|
|
|
|
The credential cache has to be cleared, and I'm not sure you have any control over this. The default expiration time is around 15 minutes. This is a function of Windows and not .NET.
The best solution is to not emulate users but to - if possible - log in as a user with administrative rights (or enough privileges to enumerate group membership) and query the groups for a user. This would require only 1 login and would allow you to query as many users as you want. This is the way that most programs (including the management tools in Windows) work.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Now, I don't now anything about encoding... Now lets take this text for example. It's "regular" encoding, correct? Now take this @ for example. Is the @ sign something special?
/\ |_ E X E GG
|
|
|
|
|
yeah, it's big and bold. Perhaps you need to reword your question ?
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
I don't understand...
/\ |_ E X E GG
|
|
|
|
|
The @ is 'special' in that it's been made larger and bold. Your question was somewhat enigmatic. That's why I suggested you reword it.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
ok, thank you.
/\ |_ E X E GG
|
|
|
|
|
Encoding refers to the way that bits are translated to characters. What you refer to as "regular encoding" is ASCII, which is a 7 bit encoding allowing a total of only 128 characters. Unicode encodings (UTF7, UTF8, UTF16) are more flexible, and can represent far more characters. The numbers in the UTF codes refer to the minimum number of bits that are used to represent a single character.
|
|
|
|
|
Encoding is important when working with file streams. For a good article on encoding, read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)[^].
The '@' character is a literal character (no escapes necessary), and as with everything else, you can read more about it in the C# language documentation. Read the documentation[^] on the string type for more information about @-quoted strings.
For example, "Hello, \"world\"" == @"Hello, ""world""" , and
"Hello,\nworld!" == @"Hello,
world!" (line break intentional)
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
�
So, is that character above (U+FFFD) not showing because it is unicode, and this webpage is ASCII?
/\ |_ E X E GG
|
|
|
|
|
Hi,
I have a column that is calculated (from other columns in the DataSet):
myDataTable.Columns.Add("ItemSubTotal", typeof(decimal), "ItemPrice * ItemQuantity");
When I try and save the DataSet to file with this Parameter:
myDataAdapter.InsertCommand.Parameters.Add("@ItemSubTotal", OleDbType.Decimal, 100, "ItemSubTotal");
I get this error:
The column mapping from SourceColumn 'ItemSubTotal' failed because the DataColumn 'ItemSubTotal' is a computed column.
Is there a way to save a computed column?
Thanks,
Ron
|
|
|
|
|
No, because it's a Computed Column. There's nothing to save. When you query against that column, you'll get the result of the column. Why would you want to save it again? If you want to save the value, you'll have to save it into another column in your existing table, or a different one if you want.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
|
Hi again Dave,
Sorry new to programming...
I'm having difficulty finding the code to save the computed column values to the other column (2nd column not viewed as expression). Can you show me an example?
Thanks,
Ron
|
|
|
|
|
Hi everyone.
I am working on an application which will rely heavily upon the DirectoryServices namespace. Specifically, I will be using the ADSI provider to manipulate and configure IIS.
Lately I've become a little concerned with Windows version compatibility. The application will be used extensively on Windows 2000 Server, along with several Windows 2003 Server boxes. The problem is that on a number of MSDN pages, I see notes that indicate various features of the DirectoryServices ADSI provider require Windows 2003 Server.
However, my limited tests appear to indicate otherwise. So far, I've not had a problem with any of my Windows 2000 Server boxes.
Here is one such example -- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/iis/adding_metabase_properties_to_a_collection_using_system_directoryservices.asp
The above page notes that the Virtual Directory sample requires Windows 2003 Server, however the sample compiles (after fixing the obvious source code errors) and runs without issue on at three Windows 2000 Server boxes I've tested.
So what is the issue here? Anyone run into this? Can I count on this work on Windows 2000 Server? Where can I find the definitive answer on this?
Thanks!
|
|
|
|
|
I haven't run into this, but if you want to make sure your app is going to work on all of your platforms, Test, Test, Test, Test, and Test some more, until YOU'RE happy with the functionality. Then hand it to a couple of beta testers and see if THEY'RE happy with the product on the platforms your suspicious of.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
hi, anyone know about that kind of error?
Thanks
|
|
|
|
|
It would help if you posted the code where the error occurs!
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks David,
i close the Visual Studio and reopen,
everything's just fine then,
sounds weird though..
Thanks
But i have another question, i wanna apply column style to one of the columns in my table, i wanna hide one column but setting its width to zero, i have five datacolumn object, but i only have one gridcolumnstyle with mapping name set to one of the column name i wanna hide.
but the style doesn't apply, the column is not hidden.
how am i goin to hide it?
|
|
|
|
|
You hide the column by setting it's width to 0, but since you haven't supplied any code, I can't tell you what your doing wrong. So, here's an example:
private void HideColumnOfDataSet()
{
System.Data.DataTable points = new System.Data.DataTable("Points");
points.Columns.Add(new DataColumn("X", typeof(int)));
points.Columns.Add(new DataColumn("Y", typeof(int)));
points.Rows.Add(new object[]{1, 2});
points.Rows.Add(new object[]{3, 5});
dataGrid1.DataSource = points;
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Points";
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.TableStyles["Points"].GridColumnStyles["X"].Width = 0;
}
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|