|
pro GCHandle:
- avoids copying, which doubles the memory load, and trashes the cache.
- pinned arrays are read/write, they pass data both ways.
- you can free the GCHandle much later (when all the native stuff is done dealing with the array, could be more than one native call there, even async stuff).
- conceptually simpler than a lot of the Marshal stuff
contra GCHandle:
- unknown cost, assumed irrelevant for large arrays (I often do megabytes of image data)
- pinned means GC can't move, however >80KB is large-object heap anyway, and that one never(hardly ever?) compacts (that is bad in general, not due to GCHandle).
- needs more code in simple cases
- coder is responsible for freeing (I have an LP_Pinner class that implements IDisposable, hence using construct). See TrayIconBuster article.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Just a quickie Luc, If I have a struct instance that is pinned for use in a function, and that struct has an IntPtr as a field, is there any need to pin the field, or is that taken care of automatically by pinning the struct?
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
I don't expect automatic recursion to occur on pinning; IMO it is your responsibility to make sure somehow that all managed objects are immobilized for as long as your native code uses pointers to them.
I actually hope pinning isn't using any reflection to figure what is inside the object.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Makes sense. I didn't want to unnecessarily replicate something that may have already been done by the system.
Luc Pattyn wrote: I actually hope pinning isn't using any reflection to figure what is inside the object.
Me too, but I don't think so...
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
DaveyM69 wrote: [MarshalAs(UnmanagedType.ByValArray)]
public uint[] dwParms;
You should specify SizeConst parameter for MarshalAsAttribute.You should declare your problematic field as IntPtr,if you don't know the array size.This I wrote this too into your thread before few days.When you get pointer to your struct use Marchal.Copy to copy the data to managed array.See this sample.
Life is a stage and we are all actors!
|
|
|
|
|
The size isn't known until runtime and varies hugely. I tried several different ways using an IntPtr but the data was not correct on the receiving end (a MIDI device). In fact, with the same data in, the output was different each time so I assume the pointer address was getting stored rather than the data itself!
The MIDIHDR structure takes an IntPtr in one field to the data - this data can be one or an array of MIDIEVENTs which contains the unspecified length array.
Luc's suggestion of converting the stuct to a uint[] as a whole instead of just that one field, pinning that and sending the IntPtr of the pinned array to the field in the MIDIHDR works though
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
I know you know this Dave - this is purely for anybody who's reading this thread who doesn't know about the SizeParamIndex parameter. This parameter (when applied in a MarshalAs(UnmanagedType.LPArray ), points to the field in the structure that has the size of the returned array.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I'm one of those "anybody who's reading this thread who doesn't know about the SizeParamIndex parameter".
Thanks for the info, and BTW I hope I'll never need it...
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Luc Pattyn wrote: Thanks for the info, and BTW I hope I'll never need it...
No probs. I'm a lot more awake tonight.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I hadn't come accross that one Pete
It doesn't help with this problem as the size (in bytes) of the uint array is held in the lower 24 bits of the uint that is the previous field. Whoever came up with that idea wants shooting!
They could have had the upper 8 bits of the other field as a separate byte parameter for the flags it relates to, and kept a field purely for the size of the array... it would have used just one extra byte of memory per struct when each struct can be thousands of bytes long already so not much difference
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
DaveyM69 wrote: It doesn't help with this problem
I'd realised that; I just thought people might find it interesting - which shows what a bad sastard I really am.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete O'Hanlon wrote: which shows what a bad sastard I really am.
Well Billy that's why we all like you. Together with you knowing and sharing a couple of things of course.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Luc Pattyn wrote: Well Billy
Great thread there; missed out a couple of favourites Cupid Stunt and Mary Hinge (you need to know about Kenny Everett to follow this character).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
All knowledge greatfully reaceived
(They were running some of the old 'Kenny' shows on UK TV Gold a couple of months ago!)
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi,
I have tabcontrol that i added to it 10 tabs (named 1 to 10).
i also have listbox that contains 1 to 10 items.
I'm trying to combain between those to object,
when a some tab get focus (GotFocus event) the same name in the listbox will be selected automatic.
For exemple:
i press on the tab name 4,
the item named 4 in the kistbox will marked will blue (selected).
Someone can help me here?
|
|
|
|
|
I try to do this:
void tabControl1_GotFocus(object sender, EventArgs e)
{
foreach (TabPage item in tabControl1.TabPages)
{
if (item.Focus())
{
for (int i = 0; i < listBoxUsers.ListBoxControl.Items.Count; i++)
{
if (item.Text==listBoxUsers.ListBoxControl.Items[i].ToString())
{
listBoxUsers.ListBoxControl.SelectedItem = item.Text;
}
}
}
}
}
But it dosen't work so good,
can i improve it?
|
|
|
|
|
I woud try something this: And i am sorry, i deleted my previous answer because i didn't read correctly
void tabControl1_Selected(object sender, TabControlEventArgs e)
{
listBoxUsers.ListBoxControl.SelectedItem = tabControl1.SelectedTab.Name;
}
|
|
|
|
|
Hi,
IMO you may have to put such code inside a tabPage_GotFocus() handler for each of the tab pages.
Not sure the tabControl gets a GotFocus event when switching tab pages.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Create one listbox control that is shown on all pages. Simple and each page will reflect the same ListBox list and selected item.
|
|
|
|
|
Hi.
I have a winform that creates a process(cmd) and redirects its inputstream and outputstream to streamwriter and streamreader.
im able to write a command to that process(dir for example) and get the output and display it on textbox.
however, it seems that after i write to the inputstream i have to close it , only if i close the stream the command that ive sended is executed.
i want to be able to write more command to that stream and keep it open.
using the "flush" method on the streamwriter dosent seems to work.
how can i accomplish that task?
|
|
|
|
|
Does your command end with CR+LF?
It's just a guess but I think you need to send the equivalent of pressing Enter at a command prompt (i.e. in Cmd window).
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Your app has a lot of problems with the console because console window doesn't belong to the caller process.If you want be able to read or write to console from Win forms in the same way like console application you should call AllocConsole or AttachConsole native functions to create or assign console window that belong to your windows forms app,not just to external process.To close current process console window you should call FreeConsole function.See some sample code here.
Life is a stage and we are all actors!
|
|
|
|
|
So i posted a question the other day about dynamically loading dll components. And it works great if I know the type like this(whatever.whatever):
Assembly asm = Assembly.LoadFrom(openFileDialog1.FileName);
Type myType = asm.GetType("whatever.whatever);
UserControl uc = (UserControl)Activator.CreateInstance(myType);
uc.Name = "uc";
this.Controls.Add(uc);
But now how would I do this without knowing the type before?
Thanks
Strive to be humble enough to take advice, and confident enough to do something about it.
|
|
|
|
|
roguemat wrote: But now how would I do this without knowing the type before?
asm.GetTypes()[^]
|
|
|
|
|
Shot
I had that but was being silly. Working now tho
Strive to be humble enough to take advice, and confident enough to do something about it.
|
|
|
|