 |
|
 |
hi all
I need help
i have a dll with this function:
int ReadRecordFor_V4XX_1(int Address,unsigned long BeginIndex,BYTE *ReadNumber,THisEvent*pHisEvents ,unsigned char *ExEvents=NULL)
and THisEvent are:
struct THisEvent
{
unsigned long RecordIndex;
BYTE pCardID[4];
Word RecordTypeID; SYSTEMTIME RecordTime;
}
typedef struct _SYSTEMTIME
{
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
how can i use it on C# ?
my code doesn't work. The error messages is "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
This is my code:
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 44)]
public struct THisEvent
{
[FieldOffset(0)]
public uint RecordIndex;
[FieldOffset(4)]
public byte[] pCardID;
[FieldOffset(8)]
public uint RecordTypeID;
[FieldOffset(12)]
public SYSTEMTIME RecordTime;
};
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 32)]
public struct SYSTEMTIME
{
[FieldOffset(0)]
public uint wYear;
[FieldOffset(4)]
public uint wMonth;
[FieldOffset(8)]
public uint wDayOfWeek;
[FieldOffset(12)]
public uint wDay;
[FieldOffset(16)]
public uint wHour;
[FieldOffset(20)]
public uint wMinute;
[FieldOffset(24)]
public uint wSecond;
[FieldOffset(28)]
public uint wMilliseconds;
};
.
.
.
[DllImport("CVIOAccessAPI.dll")]
private static extern int ReadRecordFor_V4XX_1(int Address, ulong BeginIndex, ref byte ReadNumber, ref THisEvent[] pHisEvents, ref char[] ExEvents);
.
.
.
THisEvent[] HisEvent = new THisEvent[5];
HisEvent.Initialize();
int Address = 0;
ulong RecordIndex = 0;
byte RecNumber = 5;
char[] FunKey = new char[5];
int RTN = ReadRecordFor_V4XX_1(Address, RecordIndex, ref RecNumber, ref HisEvent, ref FunKey);
Please Help me.......
|
|
|
|
 |
|
 |
Pretty good introductory material.
--SA
|
|
|
|
 |
|
 |
Thanks SA, the article was written in 2001 when .NET came out.
|
|
|
|
 |
|
 |
Sir I'm confused of Event and Delegate.
I do't know how to creat method or etc.
|
|
|
|
 |
|
 |
Hi all, I am really new to C# and I am having a problem with structs.
in C++ it is perfectly common do something like:
struct foo {
int bar[10];
char otherthing[20];
};
but in c# I tried doing:
public struct foo {
int[] bar = new int[10];
byte[] other = new byte[20];
}
but I got the error 'cannot have instance field initializers in structs';
So, how can I have arrays in structs?
|
|
|
|
 |
|
 |
unsafe struct Foo
{
fixed int bar[10];
fixed char otherThing[20];
}
// not convenient to use.
|
|
|
|
 |
|
 |
Hi Nishant,
One thing that's often overlooked is how to store a bunch of data in a program that will not be changing.
I'm building a coordinate conversion library in C# and would like to store all the various ellipsoid info in the dll, not an XML file or other external file. What would you recommend for a library component.
I plan to use an enum and a struct to define ellipsoids, but with 30 or more, how can I properly initialize all the fields.
enum eEllipsoidType {type1,type2,type3}
struct Ellipsoid
{
eEllipsoidType etype;
double a,b;
}
Then later,
ArrayList alEllipsoid = new ArrayList[];
Will a resource file be useful.
In C++, you can do this, but C# does not allow initialization.
struct
{
double val;
int index;
} DefaultInput[] = {{60,0},{70,0},{20,0},{14.73,0},{2,0},{1,0},{0.6,0},{0.78,0},{1000,0}};
Another example from C++:
const UnitList::Conversion ucTemp[] =
{{"Fahrenheit",1,0},{"Celsius",1.8,32},{"Rankine",1,-459.67},{"Kelvins",1.8,-459.67}};
but again, this is another form of structure initialization.
What is the best practice for including a lot of constant parameter data that will be included in the dll when compiled into a library component.
|
|
|
|
 |
|
 |
Hi Nish,
I decided to quit using classes when I wrote the following code:
public struct Demo
{
public string value;
}
ArrayList al = new ArrayList();
al.Add(new Demo);
((Demo)al[0]).value = “Test”;
..and got an error from the compiler: "The left-hand side of an assignment must be a variable, property or indexer"
I know, I know: an ArrayList pointer is not possible to 'point' to a variable that has been instantiated on the stack (which is the case for structs). Is there any way to overcome this limitation? The only way I found was to copy the Demo in the list to temporary variable, delete it from the list, change its value and re-insert it to list (quite cumbersome!)
Of course if you put "class" instead of "struct" all goes well..
Cheers,
Dimitris
|
|
|
|
 |
|
 |
Actually you mean you quit using structs (as opposed to classes).
And personally I don't like using structs either. Usually when you're dealing with data you're dealing with sets of it. So you need to be able to store N of them. And you want to be able to make changes without having to remember to put the changed value back. Lazy? maybe... but I stopped using structs a long time ago.
|
|
|
|
 |
|
 |
in Net 2.0 you can use generics to create collections of structs (this wasn't available when this discussion was started) for example List<Demo> demolist = new List<Demo>(); Demo demo1; demo1.value = 12345; demolist.Add(demo1); ... demolist[n].value = ...; the list is allocated on the heap so it will persist beyond the scope where the added structures were created as long as the reference to it is kept for use. of course this means a lot of copying around when initializing the list since the semantics are still by value so it's better to use a class, but this is possible.
|
|
|
|
 |
|
 |
Hi, i know that typedef structs creates an aliases for a particular struct in C++. i m trying to convert .h c++ files to a namespace in C#.net so that i use C++ DLL in my C# application. one of the structs in C++ is something like typedef struct mystruct {....} mystruct1, *mystruct2; since typedef is used, mystruct1 and *mystruct2 are aliases to mystruct. how can i acheive the same (i.e aliasses in c#)? i can subtitute mystruct1 and *mystruct2 in the C# namespace (which i am creating) by mystruct. but i am not sure if they are used in the DLL? your help is greatly appreciated. i am new to DLL and C# thanks arssa2020@yahoo.com
|
|
|
|
 |
|
 |
Imagin this code ( with mistakes )
class A
{
enum mynumber { one, two, three }
}
class B : A
{
public B()
{
//Does anybody know somme thing similar to this or
// how to do this
base.nynumber.Add( four );
// to use like
int a = mynumber.one;
int b = mynumber.four;
}
}
my question you know, is this or some thing similar possible.
Thanks
Santi
srlopez@rps.es
|
|
|
|
 |
|
 |
Hmmm... Sort of. It won't be an enum, but you can do this:
[edit]This is simplified code, you'll need to implement some conversions and operators, but that's the skeleton[/edit]
struct A
{
public static A one;
public static A two;
public static A three;
}
struct B : A
{
public static A four;
}
I see dumb people
|
|
|
|
 |
|
 |
Thanks... is the same idea, but ... struct in c# can inherits?
I not sure.
|
|
|
|
 |
|
 |
srlopez wrote:
Thanks... is the same idea, but ... struct in c# can inherits?
I not sure.
Yes, it can.
I see dumb people
|
|
|
|
 |
|
 |
Mate, you cannot subclass structs either. Try your code in VS2005.
Enums are structs and both are subject to this limitation.
|
|
|
|
 |
|
 |
You can't add enum element dynamically, you can use Dictionary, SIMILAR TO enum.
Sample Code:
class ClassA
{
protected Dictionary dic = new Dictionary();
public ClassA()
{
dic.Add("one", 1);
dic.Add("two", 2);
dic.Add("three", 3);
}
}
class ClassB: ClassA
{
public int this[string s]
{
get
{
foreach (KeyValuePair kv in dic)
{
if (kv.Key.Equals(s))
{
return kv.Value;
}
}
return Int32.MinValue;
}
}
public ClassB()
{
dic.Add("four", 4);
}
}
static void Main(string[] args)
{
ClassB b = new ClassB();
Console.WriteLine(b["three"]);
}
|
|
|
|
 |
|
 |
Thanks Nish for the article! But...isn't it odd that code like the following is legal in C#?
Rating average = Rating.Poor + 1;
This is the syntax that underlies IncrementRating and DecrementRating in your article. It's all perfectly legal C#, and you are careful not to go beyond the defined range. But I gotta ask, Is it a good idea to manipulate enums like this? It's not legal in C++.
I can see two drawbacks, and I bet you can guess what they are. First, there is the maintenance problem that arises when the enum changes. When you amend the Rating enum, you also have to double check that IncrementRating and DecrementRating still work. In the United States at least, the pressure of normal grade inflation will soon require the enum to become:
enum Rating {Poor, Average, Okay, Good, Excellent, Awesome, Fantastic}
That may not be so much of a problem in other parts of the world where education standards are better maintained.
Second, the method of incrementing and decrementing enums is not general because enum values don't have to occupy a contiguous range:
enum Rating {Poor=40, Average=60, Okay=75, Good=85, Excellent=92}
Adding and subtracting a constant like 1 will not walk you from one enum value to the next in a case like this. In fact, you will land in a crack:
Rating above_average = Rating.Average + 1; // legal, = 51
What seems strange and inconsistent is that you can assign above_average as shown above, but can't do it directly:
Rating above_average = 51; // error!
So what do you think? Given problems like these, is the C# language flawed with regard to its handling of enum?
|
|
|
|
 |
|
 |
Consistency.
All of the value types require that you initialize them before use.
Does anyone out their believe that int or char should have a default value.
int i;
char c;
Console.WriteLine( "Int i is {0}, Char c is '{c}'" );
--------------
Output
Int i is 0, Char c is 'a'
--------------
Obviously, assumptions like this could lead to problems.
Uninitialized value types have always been a problem in C/C++ programming. C# simply doesn't allow them. I believe that the behavior produces a consistency across ALL value types.
Uninitialized reference types default to null. Value types must be assigned before use, no nullness (as far as the compiler is concerned).
Will T Smith
|
|
|
|
 |
|
 |
I needed to enumerate through an Enum and could not figure it out. I then remembered your article and just knew you would have an example in here as to how to do it. An voila, you did, I did and it rocked!
thanks again Nish.
Paul Watson Bluegrass Cape Town, South Africa Ray Cassick wrote: Well I am not female, not gay and I am not Paul Watson
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Another question
Basically I have an XML file which persists certain data and I guess really represents a class... (maybe I should just serialise my objects? hmm...)
The class has an enum (same as my previous question) with those three "constants", Article, Image and Other. Now in the XML file there is an attribute named type and it holds either Article, Image or Other.
Currently when I pull from the XML file I am using a switch block to assign the right enum "constant" to the object instance. e.g. (pseudo code) If xmlnode.type = "article" then cItem.type = ContentType.Article
I think that is pretty lame and it will get out of hand when more content types are introduced.
So my question is: Is there a better way to do it? Is there a way to sort of "inform" the system that Article in the XML file is equal to ContentType.Article?
There must be, because that will just make enums rock even more! ta (hope my question does not sound too stupid )
|
|
|
|
 |
|
 |
Paul,
See this code :-
enum Rating {Poor, Average, Okay, Good, Excellent}
class Class1
{
[STAThread]
static void Main(string[] args)
{
for(Rating r = Rating.Poor; r<=Rating.Excellent; r++)
Console.WriteLine((int)r);
}
}
It'll show 0,1,2,3,4 on screen. So basically they are integeres starting from 0 (unless you specify otherwise).
So perhaps you could do this in your XML :-
<blah type=x />
where x is an integer. Now simply read this x and cast it to ContentType.Article. That should work. This is clearer from the following code snippet :-
Rating q = (Rating)3;
Console.WriteLine(q);
This will output "Good" on the screen.
If my solution sounds silly, well sorry about that Paul. Gave it my best shot
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9
Click here for review[NW]
|
|
|
|
 |
|
 |
Nishant S wrote:
If my solution sounds silly, well sorry about that Paul. Gave it my best shot
LOL Nish, no your solution was certainly an answer and one I did not think of (do you have a backpack to carry your extra brain btw? )
The only thing is that that means I have to use numbers instead of text in the XML file, which just makes the XML file harder to read and understand outside of the application.
I guess what I have to do is do what you suggested above and then create an XML Schema which has it's own enum defining 1 as Article, 2 as Image etc. It is actually a better way, but damn that is going to take me some time to do and I was hoping for a lazy-ass-programmer way
|
|
|
|
 |