Click here to Skip to main content
15,896,469 members
Home / Discussions / C#
   

C#

 
QuestionHelp with TypeLoadException (marshaling unmanaged code) Pin
TomWolfstein30-Jun-07 4:50
TomWolfstein30-Jun-07 4:50 
AnswerRe: Help with TypeLoadException (marshaling unmanaged code) Pin
Luc Pattyn30-Jun-07 5:10
sitebuilderLuc Pattyn30-Jun-07 5:10 
GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
TomWolfstein30-Jun-07 6:05
TomWolfstein30-Jun-07 6:05 
GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
Luc Pattyn30-Jun-07 6:57
sitebuilderLuc Pattyn30-Jun-07 6:57 
GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
TomWolfstein30-Jun-07 8:51
TomWolfstein30-Jun-07 8:51 
GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
Luc Pattyn30-Jun-07 14:27
sitebuilderLuc Pattyn30-Jun-07 14:27 
GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
TomWolfstein1-Jul-07 7:16
TomWolfstein1-Jul-07 7:16 
GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
Luc Pattyn1-Jul-07 8:34
sitebuilderLuc Pattyn1-Jul-07 8:34 
Hi Tom,

I am afraid you've lost me somehow.

1.
For unions the only way out I know in C# is by using FieldOffset.
Your "struct union" is not a union at all, it is a struct with two (sequential) members.

Of course, if the members are small, and the other side is flexible (i.e. no set of registers
cast in hardware, and no software in OBJ/EXE-only form, and no implicit conversion, see below)
then you could change it in this way, but it is a change, it is no longer what was intended
originally.

If you rely on union members to occupy the same memory (e.g. when you write to one member
then read the other to obtain a bit-wise translation, such as 4 bytes to 1 int), then you
need real unions of course.

2.

TomWolfstein wrote:
The 64 bytes I calculated is the same size i got ...


I dont know which struct you refer to. If it is struct vc1_sBlkIntra, then we agree that
the size is 64B; and there is no need for padding, every member is short/ushort so they
all automatically align. If however it is vc1_sBlk from your original post

typedef struct
{
vc1_eBlkType eBlkType; /** Block type */
FLAG Coded; /** Non zero AC coefficients for Intra,
non zero AC/DC for Inter */
union
{
vc1_sBlkIntra sIntra; /** Intra block state information */
vc1_sBlkInter sInter; /** Inter block state information */
} u; /** Intra/Inter union */
} vc1_sBlk;

then depending on the size of vc1_eBlkType (lets say multiple of 4B) and FLAG (1B?)
I expect padding bytes to get the union aligned to a 4B multiple on the C/C++ side,
and also on the C# side (assuming Sequential, otherwise everything may get reordered).

3.
structs are value types, not references.
arrays are references, unless they are in a struct and carry marshaling info to tell
otherwise (ByValArray and SizeConst).

Marshaling references is complex, each pointer points to some memory block that
sooner or later may have to be a managed object. When given the choice, I tend to keep
things as simple as possible in marshaling, e.g. by putting everything as values in
one (big) struct, and by passing reference types one by one, using function calls
instead of struct members; and by avoiding structs/classes that contain references
all together (if needed, one can pass a null reference, then call a function to
accept a reference type and adapt the reference).

4.
I am getting confused about your structs; there are an unknown number of them, and they
have difficult names. So if you want detailed help, please be as specific and complete
as you can (and show both C and C# side as you did in your original post).

5.
it has been my experience one must build up the p/invoke and marshaling knowledge
by starting of with simple cases, observe them in detail, and get them to work
(compile, run without exception, pass the right data, checked by logging and/or
returning as a function result what was passed in).

And then gradually try more complex things. It is in general a bad idea to throw in all
the structs and classes at once, and have limited observability: it keeps you guessing
and probably wasting lots of time.

I tend to include logging code that dumps all the values that cross the borders,
I switch it on or off with one flag, and I leave the code in after I got it working,
so as soon as a functional change is required, it is there to help...


Hope this helps.

Smile | :)




GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
TomWolfstein4-Jul-07 21:57
TomWolfstein4-Jul-07 21:57 
GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
Luc Pattyn5-Jul-07 0:17
sitebuilderLuc Pattyn5-Jul-07 0:17 
GeneralRe: Help with TypeLoadException (marshaling unmanaged code) Pin
Luc Pattyn1-Jul-07 9:14
sitebuilderLuc Pattyn1-Jul-07 9:14 
Questionvalidating a formula Pin
Bojja Lakshmi30-Jun-07 2:50
Bojja Lakshmi30-Jun-07 2:50 
AnswerRe: validating a formula Pin
User 665830-Jun-07 3:32
User 665830-Jun-07 3:32 
QuestionDataGrid with checkbox Pin
-spy-30-Jun-07 2:40
-spy-30-Jun-07 2:40 
AnswerRe: DataGrid with checkbox Pin
DanB198330-Jun-07 8:24
DanB198330-Jun-07 8:24 
QuestionInvalid Argument exception? Pin
$uresh $hanmugam30-Jun-07 1:58
$uresh $hanmugam30-Jun-07 1:58 
AnswerRe: Invalid Argument exception? [modified] Pin
Luc Pattyn30-Jun-07 3:07
sitebuilderLuc Pattyn30-Jun-07 3:07 
Questionreading file error Pin
tauras8130-Jun-07 1:41
tauras8130-Jun-07 1:41 
AnswerRe: reading file error Pin
Luc Pattyn30-Jun-07 1:58
sitebuilderLuc Pattyn30-Jun-07 1:58 
Questionarray of type label Pin
Glen Harvy29-Jun-07 23:47
Glen Harvy29-Jun-07 23:47 
AnswerRe: array of type label Pin
Sathesh Sakthivel29-Jun-07 23:53
Sathesh Sakthivel29-Jun-07 23:53 
GeneralRe: array of type label Pin
Glen Harvy30-Jun-07 0:09
Glen Harvy30-Jun-07 0:09 
AnswerRe: array of type label Pin
Luc Pattyn29-Jun-07 23:57
sitebuilderLuc Pattyn29-Jun-07 23:57 
GeneralRe: array of type label Pin
Glen Harvy30-Jun-07 0:11
Glen Harvy30-Jun-07 0:11 
GeneralRe: no Replace !! Pin
Luc Pattyn30-Jun-07 1:14
sitebuilderLuc Pattyn30-Jun-07 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.