C++ .dll functions in C#

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top