|
Why do people keep answering questions by posting under someone elses answer ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
Why do people keep answering questions by posting under someone elses answer ?
I don't know why, but apparently you seems to be getting a couple of them.
<font=arial>Weiye Chen
Life is hard, yet we are made of flesh...
|
|
|
|
|
Thats because we had answered at the same time! believe from various locations. Take care, sorry for the trouble guys for this thread!
|
|
|
|
|
No, it's not. You clicked on Reply while looking at Christian's answer! Your post is 20 minutes later this his.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hello,
I am trying to call c++ dll function from C#.
Could anybody help me?
Here is the C++ function declaration.
long WINAPI GetPredictValues (double& m_vo2Max, double& m_vo2_kg_max, double& m_o2PulseMax)
And this is my declaration in C#.
[DllImport("c:\\ProgramFiles\\VacuMed\\turboFit\\tf32mmf.dll",SetLastError=true)]
public static extern int GetPredictValues(out double vo2Max,out double vo2_kg_Max,out double o2PulseMax);
And I get an error on the line below.
double vo2max,vo2kgmax,o2Pulsemax;
GetPredictValues( out vo2max, out vo2kgmax,out o2Pulsemax);An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an object
Thank you for any help.
Sincerely,
|
|
|
|
|
On what line does the exception occur? I don't think the exception message came up on the line that you posted...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I'm sorry I had forgotten to write the line that the error message occur.
The message occur on the line below:
double vo2max,vo2kgmax,o2Pulsemax;
GetPredictValues( out vo2max, out vo2kgmax,out o2Pulsemax);
Thank you for your help
Hai
|
|
|
|
|
anyone know of a tool that can convert c/c++ header to c#, i need to
convert a massive struct which is about 150 lines that has nested
structs and unions.
heres a sample of the struct.
*note* chopped off bits and pieces in the middile.
struct R_OMNI_LINK_MESSAGE {
unsigned char MessageLength;
union {
unsigned char Data[255];
struct {
unsigned char MessageType;
union {
struct {
unsigned char ItemType8;
unsigned char ItemName8[16];
};
struct {
unsigned char TimeDateValidFlag;
unsigned char AreaSecurityMode[8]; // index 0-7
struct {
unsigned char BatteryReading;
} ExpansionEnclosure[8]; // index 0-7
};
struct /* olmMESSAGE_STATUS */ {
unsigned char Data;
} MessageStatus[33];
struct {
unsigned char AreaNumber; // (1-8)
};
struct {
unsigned char UserCodeNumber;
};
}; // union
}; // struct
}; // union
};
Kids don't try this at home!
|
|
|
|
|
There are no unions in C#.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
ok, how do i convert that to c#?
Kids don't try this at home!
|
|
|
|
|
I'm not sure that you can, in a way I'd consider production safe. Why do you need to ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
doesnt matter if its safe or not...i just need it to work with c#, i have it partially converted but its giving me errors about [FieldOffset()] collisions and marshalling errors.
i need to convert this to c# so i can communicate with a piece of hardware, since i dont know enough c++, c# is the only way to go ;(
Kids don't try this at home!
|
|
|
|
|
savage_ wrote:
[FieldOffset()] collisions and marshalling errors.
Then you've found the methods I could suggest, I dunno how else you could do it. Excepting that perhaps you could store an object for each union, and then create objects that represent the possibilities that go inside. Honestly, I reckon it would be easier to learn a little C++.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
There is no tool that will do this for you. You'll have to code the thing by hand.
Since you haven't posted your C# code, it's impossible to tell you what you did wrong to generate that error. But, I'll take a guess at it.
Heath posted an example of how to simulate unions sometime last year. I grabbed this little piece of code from my archives:
[StructLayout(LayoutKind.Explicit)]
public struct INPUT{
[FieldOffset(0), MarshalAs(UnmanagedType.U4)] public int type;
[FieldOffset(4)] public MOUSEINPUT mi;
[FieldOffset(4)] public KEYBOARDINPUT ki;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT{
public int dx;
public int dy;
[MarshalAs(UnmanagedType.U4)] public int mouseData;
[MarshalAs(UnmanagedType.U4)] public int dwFlags;
[MarshalAs(UnmanagedType.U4)] public int time;
[MarshalAs(UnmanagedType.SysUInt)] public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct KEYBOARDINPUT{
public short wVk;
public short wScan;
[MarshalAs(UnmanagedType.U4)] public int dwFlags;
[MarshalAs(UnmanagedType.U4)] public int time;
[MarshalAs(UnmanagedType.SysUInt)] public IntPtr dwExtraInfo;
}
The thing to remember is that your inital struct must be LayoutKind.Explicit . Also note the FieldOffsets! If you use LayoutKind.Sequential and try using overlapping FieldOffsets, you'll get the collision exception.
Also, you're going to have to learn a little C++ to do this anyway. Since C++ types and .NET types of the same names don't exactly match in size or function.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
what i dont understand is the offsets. heres where im getting stuck:
struct R_OMNI_LINK_MESSAGE {
unsigned char messagelength // offset 0
unionX {
unsigned char data[255] // offset 4?
struct { // this struct has no name?
unsigned char messagetype // since its inside union this ones offset is 4?
unionY {
struct { // NAME_DATA
unsigned char itemtype // offset ??
} // struct name_data
} // unionY
} // struct
} // unionX
} // r_omni_link_message
this is my c# code for the above bit of code
[StructLayout(LayoutKind.Explicit)]
public struct R_OMNI_LINK_MESSAGE
{
[FieldOffset(0)] public int MessageLength;
[FieldOffset(4)][MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)] public string Data;
[FieldOffset(??)] public int MessageType;
//olmNAME_DATA (8 bit)
[FieldOffset(??)][MarshalAs(UnmanagedType.LPStr)] public string ItemType8;
[FieldOffset(??)][MarshalAs(UnmanagedType.LPStr)] public string ItemNumber8;
[FieldOffset(??)][MarshalAs(UnmanagedType.LPStr, SizeConst=16)] public string ItemName8;
?? i dont know the offsets
Kids don't try this at home!
|
|
|
|
|
The field offsets are just how many bytes into the structure that field starts. What these values are depends on the original structure. Usually, the field offsets put the field immediately after the last one, accounting for the size (in bytes) of the previous fields.
In your example, the first field is an unsigned char, called messagelength, at, obviously, offset 0 from the start of the structure. Since the C++ char type is usually a single byte, the next field would probably be at offset 1, accounting for the size of the fields before it.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
i am enhancing a image button.... i found this web page but dun reali understand.... can someone teach me how to use it?
http://www.codeproject.com/script/comments/forums.asp?forumid=1649
|
|
|
|
|
You've given a link back to this forum. What are you trying to do ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
http://www.codeproject.com/cs/miscctrl/XCtrls.asp
|
|
|
|
|
Looks like a good article to me, what don't you understand ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
hmmm.....i wanted to use this but my supervisor juz came to me and say this is not wat she want....
so for my image button i used this
imageButton1.Image = new
Bitmap(Assembly.GetExcutingAssembly().GetManifestResourceStream("register.png"))
but there is error for this...the first Assembly
-->The type or namespace name 'Assembly' could not be found (are you missing a using directive or an assembly reference?)
:sorry im really veri bad at programming.... so im reali lost
|
|
|
|
|
Serenena wrote:
sorry im really veri bad at programming.... so im reali lost
How do you come to be doing it for a job then ?
You need using System.Reflection at the top of your code.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
actually im onli a student..... this my project...thanks alot
|
|
|
|
|
Ah... OK. Sorry. That's one reason why you shouldn't use code from the web then.
All the functions you can use exist in namespaces, and you either need to explicity state where they are, or the using statement tells the compiler to look there when working out what your code means. msdn.microsoft.com is the microsoft help site, check there first for any calls if you don't know how to use them ( then ask here, of course ).
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
okie thanks alot.... i think i need lots of help from u later on...
|
|
|
|