passing a struct from managed code to unmanaged function

Tech-Archive recommends: Fix windows errors by optimizing your registry



I would like to know how do I pass a pointer to a struct from managed
code to unmanaged code. For example if I create structure like this in
managed code.

StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ]
public struct MYSTRUCT
{
[ MarshalAs( UnmanagedType.LPStr ) ]
public String Text;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 ) ]
public String Font1;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=64 ) ]
public String Font2;
public int Justify;
public int VertJustify;
public bool bMirrored;
public bool bVerticalText;
public int TextColor;
public int BackgroundColor;
public bool IsRichText;
public int Effects;
}

And then I have C function like this which has an argument which is
void* (pointer to void object).

/*
HANDLE WINAPI AddObject(LPCSTR ObjType, LPCSTR ObjName, RECT Size,
int Rotation, void* Attrib);
*/

[DllImport("labels.dll")]
static extern IntPtr AddObject(String ObjType, String ObjName, int
Size, int Rotation, IntPtr Attrib);

How do I pass the structure MYSTRUCT to the Addobject() function. The
last argument of the AddObject() functions wants you to send a pointer
to MYSTRUCT object. Is this the right way to code this.

static void Main(string[] args)
{

MYSTRUCT x = new MYSTRUCT();


x.Text = "Hello World \r\n";
x.Font1 = "Times New Roman, 10, Bold" + '\0';
x.Font2 = "Times New Roman, 10, Bold" + '\0';
x.Justify = 0;
x.VertJustify = 0;
x.bMirrored = false;
x.bVerticalText = false;
x.TextColor = 0;
x.BackgroundColor = 255;
x.IsRichText = false;
x.Effects = 0;


IntPtr mypointer =
Marshal.AllocHGlobal(Marshal.S­izeOf(typeof(MYSTRUCT)));

Marshal.StructureToPtr(x, mypointer, true);

to.oID = (int) AddObject("Text", "sunday", 0, 0, mypointer);

} // end of Main()


Can you correct this code so that it will work. How do you pass a Stuct
from managed code to an unmanaged C function that expects you to pass a
void* pointer to the struct. And how do retrieve a void* pointer that
the unmanaged C function returns after completing its execution.

.



Relevant Pages

  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Re: A C Adventure: your comments are welcome
    ... void * usrRightSegment; ... That is a string is a binary tree of segments, ... struct inside a struct either as the thing itself or as a pointer ... because C makes it easy to change pointer type. ...
    (comp.lang.c)
  • Re: Some issue with pointers
    ... typedef struct { ... void *ptr; ... and a pointer to the items structure. ...
    (comp.lang.c)
  • Re: Pointer to a member variable in a struct without dereferencing the member
    ... I have the below struct and I need to access the pointer to the "void ... * data" member without actually dereferencing the member variable. ...
    (comp.lang.c.moderated)
  • Re: function pointer casting
    ... Here you say the second member of the struct is a function pointer ... apparently is not the same as pointer to void. ... implied assignment of the argument to the parameter. ...
    (comp.lang.c)