|
Hello everyone,
I have been trying to cast some pointers in 'unsafe' code block but running into the following error:
error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('DataType')
My code is as follows:
protected unsafe int MyFunc<DataType, TagType>(ref int nValues, ref DataType* values)
{
....
DataType[] valuesArr = new DataType[nValues];
GCHandle valuePtr = GCHandle.Alloc(valuesArr, GCHandleType.Pinned);
values = (DataType *)valuePtr.AddrOfPinnedObject().ToPointer();
}
This code is called from unmanaged side and basically I would like to cast the pointer from GCAlloc to the correct type... I am guessing it is not allowed even within an unsafe code block.
Just wondering if there is any way around this?
Many thanks,
Keith
|
|
|
|
|
That does not make sense to me, for a couple of reasons.
1.
Your DataType is a managed type, so why would you want that as a parameter in an unmanaged callback? Pointers that cross the managed/unmanaged border should be understood on both sides, good examples would be int* for a simple array, or IntPtr for just almost everything. So you don't need AddrOfPinnedObject().ToPointer(), AddrOfPinnedObject() suffices.
2.
The GCHandle you create needs to stay alive, so it cannot be a local variable inside a callback method. Remember, you need to call Free() on it when done, otherwise the object remains pinned forever.
Note: if your callback were called more than once, a single class-level IntPtr would not be sufficient either.
FWIW: I have this unfinished article[^] on P/Invoke.
|
|
|
|
|
Luc Pattyn wrote: FWIW: I have this unfinished article[^] on P/Invoke.
I quickly skimmed through it. Already looks great. When finished it'll definitely be an article that people can link to when replying to P/Invoke questions.
|
|
|
|
|
Thanks Nish. Unfortunately it will never be finished, there's always things to be added, until it becomes an unreadable 3-tome encyclopedia. OTOH, I'm doing a lot of P/Invoke but tend to keep things simple (and high-performant), so I may never know all there is to know on the subject.
|
|
|
|
|
I second Nish's comments - I was actually about to send you an email asking 'where your P/Invoke article was' becuase I had lost the link haveing looked at it some time before
(Im contemplating interfacing from c# to a 3rd party DLL .. all the time, my brain was going "where is that article from Luc", that will save me)
so cheers & thanks Luc
|
|
|
|
|
You could always use the "My Articles" link in my sig...
|
|
|
|
|
thats where I waqs heading - I just happened to see on my open browser that you'd answered this question, it saved me a few more clicks
and to you
|
|
|
|
|
Hi techies..
I want to make a pie chart in C#. Basiclly when the user does a quiz on my system, his score(how many they got correct,wrong and needed help on) will be stored in an int array.
I then want them to have a option of them being avabile to view their progress through charts.So,the pie chart should keep updating itself as the user answers more questions.
However I am not sure how to do this. I am not sure how hard this will actually be but I am guessing it might be hard/take some time to do.
Can you please suggest some ideas to do this?
Thanks a lot!!!
|
|
|
|
|
The easiest way is probably to use some chart control that supports pie charts and follow the instructions for the control.
If you are interested in making your own, then you need to decide which graphics library or system to use (GDI, DirectX, SVG, etc.) Once you decide that, most of the graphics libraries have a circle and arc function/method, so you can figure what percentage of 2*PI to draw and where to put it, etc.
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
|
the links were really helpful...
i installed the Microsoft chart control.i cant access a few properties of the chart though..can u please tell me which all namespaces i should b using??
thanks a lot!
JF2015 wrote: Hi,
here are some links for you:
Microsoft Chart control for .NET:
http://code.msdn.microsoft.com/mschart[^]
A codeproject article:
A flexible charting library for .NET[^]
Both should do the job and there are enough samples to get you started
|
|
|
|
|
Hi,
when using ASP.Net:
System.Web.UI.DataVisualization.Charting
and for Windows Forms:
System.Windows.Forms.DataVisualization.Charting
Try to work through the samples delivered by MS. These provide a lot of useful tips and show you how to customize your chart.
Good luck,
JF
|
|
|
|
|
Could anybody tell show me what the C# equivalent of this C++ code is please.
{
srand( ( int ) time (0));
int x,y,z, nums [50];
for ( x = 1 ; x < 50 ; x++ ) nums [x] = x ;
for ( x = 1 ; x < 50 ; x++ )
{
y = ( rand() % 49 ) + 1;
z = nums[x];
nums[x] = nums [y];
nums [y] = z;
}
|
|
|
|
|
Are you sure you want it to be the same? It uses modulo on a random number, the result is a non-uniform chance distribution, and as if that's not bad enough it seeds the PRNG every time, making this code fundamentally flawed.
Here is a better version (correctness not guaranteed - copied from wikipedia)
public static void shuffle(int[] array, Random rand)
{
for (int i = array.Length; i > 1; i--)
{
int j = rand.Next(i);
int tmp = array[j];
array[j] = array[i - 1];
array[i - 1] = tmp;
}
}
Note: do NOT make a new instance of Random each time you call this method, keep using the one you used before
edit: to make it do the same thing, pass in an array [0, 1, 2, 3 etc]
modified on Friday, July 16, 2010 4:48 PM
|
|
|
|
|
<pre>RandomNumbers.Seed((int) time (0));
int x;
int y;
int z;
int[] nums = new int[50];
for (x = 1 ; x < 50 ; x++)
nums [x] = x;
for (x = 1 ; x < 50 ; x++)
{
y = (RandomNumbers.NextNumber() % 49) + 1;
z = nums[x];
nums[x] = nums [y];
nums [y] = z;
}
//----------------------------------------------------------------------------------------
// Copyright © 2006 - 2010 Tangible Software Solutions Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// This class provides the ability to simulate the behavior of the C/C++ functions for
// generating random numbers, using the .NET Framework System.Random class.
// 'rand' converts to the parameterless overload of NextNumber
// 'random' converts to the single-parameter overload of NextNumber
// 'randomize' converts to the parameterless overload of Seed
// 'srand' converts to the single-parameter overload of Seed
//----------------------------------------------------------------------------------------
internal static class RandomNumbers
{
private static System.Random r;
internal static int NextNumber()
{
if (r == null)
Seed();
return r.Next();
}
internal static int NextNumber(int ceiling)
{
if (r == null)
Seed();
return r.Next(ceiling);
}
internal static void Seed()
{
r = new System.Random();
}
internal static void Seed(int seed)
{
r = new System.Random(seed);
}
}</pre>
David Anton
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
|
|
|
|
|
Hi all,
I am looking for code which could help me to disable checkbox for treenode, I had look at Threestate checkboxes article shared on CP, however I want to display Image for Treenode as well, so cant use that code(Threestate checkboxes are displayed as images in that article code).
If anybody help me to achieve this that would be very appreciable.
Thanks in advance
|
|
|
|
|
I have a code for the same.
this.tree.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.tree.DrawNode += new DrawTreeNodeEventHandler(tree_DrawNode);
void tree_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if (e.Node.Level == 1)
HideCheckBox(e.Node);
e.DrawDefault = true;
}
private void HideCheckBox(TreeNode node)
{
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvi));
Marshal.StructureToPtr(tvi, lparam, false);
SendMessage(this.tree.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
}
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
Hi there,
I have a problem with a drived Form.
I have FormA with a Grid on it and FormB and FormC wich are derived from FormA.
Some information of the Grid have to be loaded(in the constuctor) and saved(in the destructor) and do do this correct,
so I need the name of the derived Forms(FormB & FormC).
I tried to get the name from the callstack, but when i have a FormD which is drived from FormC,
the whole thing wont work.
It is possible to get the name of the Form(FormB, FormC or FormD) dynamically, even if it is a derived Form?
Thanks in advance
|
|
|
|
|
Nope, you will not get name of derived form in this manner.
Do one thing. Define one form object inside the parent form as follows.
Form f1;
Now, whenever you create child form assign object of child form to this object
which is of parent form, as follow.
derived from ParentForm.
Public ChildForm():base(this)
{
}
public ParentForm(Form _f1)
{
f1=_f1;
}
That's it. Now, you have whole child form inside the parent form. You
can do anything using f1.
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
As the OP described it, this is not a parent/child issue but a polymorphic one. If it were a parent/child one, what you have suggested is a quick and easy, but nasty and bad solution. It's almost never a good idea for a child to be aware of (never mind explicitly bound to) it's parent.
If this is the way you normally handle things I suggest you learn about events/delegates so you can in future write proper OOP code. Just because the way you suggest works, doesn't mean it's good and should be recommended.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
hi DaveyM69,
Thanks for you suggestion.
However, I never told that the solution I gave is best one or does not have any
side effects.
I have just suggested one of the quick and easy solution, that I thought.
Anyway, thanks once again for your suggestion.
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
you probably want the type information, not the name. You could use the GetType() method, or the is/as keywords. But I don't think you should go that route.
If FormD needs a different finalization from FormA, you really should solve it by providing it with its own specialized Dispose() method.
Since disposing/finalization are non-deterministic (e.g. they won't always happen when your app terminates), you should solve this differently, probably based on the FormClosing/FormClosed events (which you can override too so you could specialize them for all kinds of Forms).
|
|
|
|
|
That sounds like a bad OOP design. A base class should never need to know about any derived classes.
You could write a factory or manager class that knows what's going on and can do what's needed.
But you probably just need to get the polymorphism right.
|
|
|
|
|
Why do you need the name of the form? I assume you mean the class name and not the name of the instance variable. If you used generics in your methods that would probably solve a whole lot of this.
|
|
|
|
|
I need the name of the form to load(in the constuctor) and save(in the destructor) some information of the grid.
So I need the name of the instance variable to get a unique name.
|
|
|
|