Passing nested structures into native dll .. 
Author Message
 Passing nested structures into native dll ..

Hey Guys,
    With the help of all the past posts, I was able to correctly create a
dll function declaration and struct layouts. Now, I have some issues. I have
a simple dll function that expects two structures, and both structures
contain embedded structures. Here's an example:

public struct TMemDef{
    public int handle;
    public int address;



public struct TQuery{

    public short age;
    public short callingArea;
    public TMemDef geo;


Now, if I attempt to pass TQuery to the dll, then it fails with an exception
stating that the TQuery structure cannot be marshalled.  I could simply move
the members of TMemDef into TQuery, however that is some kind of kludge in
cases where I have arrays of nested structures.

Also, some of the postings I have seen have not been using structs, but
rather Classes with the StructLayout attribute. What's the difference? Also,
in some dll declarations I have seen the use if [In, Out], and in others I
simply see "ref". In a small test dll I made myself, [In, Out] didn't work
but ref did. Again, what's the difference.

Thanks guys
Trey Hutcheson

Sun, 21 Mar 2004 06:15:38 GMT  
 Passing nested structures into native dll ..
In my test, the nestd structrue works fine. Below is my declarations in
C/C++ :

typedef struct
        int nF1;
        int nF2;


typedef struct
        short nF1;
        short nF2;
        STRUCT_1 nF3;


EXP_API int WINAPI fnExp(STRUCT_2 stctTest)
        return stctTest.nF1 + stctTest.nF3.nF1;


This is the declaration for the structures in C#:
public struct STRUCT_1
        public int nF1;
        public int nF2;


public struct STRUCT_2
        public short nF1;
        public short nF2;
        public STRUCT_1 nF3;


public class Exp
        [DllImport("Exp.dll", CharSet=CharSet.Ansi)]
        public static extern int Doit(ref STRUCT_2 stc);


And the code to call it:
                        STRUCT_2 stc;

                        stc.nF1 = 1;
                        stc.nF2 = 1;
                        stc.nF3.nF1 = 3;
                        stc.nF3.nF2 = 5;

                        Trace.WriteLine(Exp.fnExp(stc).ToString ());            

Sun, 21 Mar 2004 14:18:10 GMT  
 [ 2 post ] 

 Relevant Pages 

1. native dll calling with structs containing arrays of nested structs

2. passing reference in nested structure

3. Help: Passing structure including SafeArrays to C++ DLL

4. Mixed Language - VBA calling C DLL passing a structure

5. Passing an array of structures from VB4.0 to DLL and back

6. Passing user defined structures from VB to C++ DLL

7. Passing strings to a DLL through a structure.

8. Passing an __event in native C++

9. Nested structures and pretend inheritance

10. serialization of a nest of data structures

11. nested dynamically allocated structures

12. nested structures


Powered by phpBB® Forum Software