C++ .dll functions in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have this structure and function defined in C++ .dll:

typedef struct {
POINT ptMouse;
WCHAR Word [1024];
} TXTCAP_EVENT, *PTXTCAP_EVENT;

void txtcapGetData (PTXTCAP_EVENT pe);

I want to use this .dll in C#. How should I declare the function? I don't
know how to declare passing a pointer to a structure.

[DllImport("mydll.dll")]
public static extern void txtcapGetData( ???);



Thank you in advance
 
Jurate,

You would want to do this:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct TXTCAP_EVENT
{
public POINT ptMouse;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
public string Word;
}

If the POINT structure is the same as it is in the Windows API, then you
could replace the POINT declaration with the Point structure from the
System.Drawing namespace.

The function delcaration would be as follows:

[DllImport("mydll.dll")]
public static extern void txtcapGetData(ref TXTCAP_EVENT pe);

Hope this helps.
 
Back
Top