Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I use COPYDATASTRUCT with the .Net Compact Framework

38 views
Skip to first unread message

microsoft.public.windowsce.embedded

unread,
Jan 7, 2003, 5:23:02 PM1/7/03
to

I am trying to communicate with an unmanaged application that is
expecting CopyData messages, I've been looking around the newsgroups and
have stumbled onto several ideas, but I'm apparently not quite there. Any
ideas why I get a runtime error when executing this C# code:

using System.Runtime.InteropServices;

const int WM_COPYDATA = 0x004A;

[DllImport("coredll.dll")]private static extern int FindWindow(string
lpClassName, string lpWindowName); //works great

[DllImport("user32.dll")]static extern bool SendMessage(IntPtr hWnd, int
Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);

public struct COPYDATASTRUCT

{

public IntPtr dwData;

public int cbData;

public char[] lpData; //public IntPtr //[MarshalAs(UnmanagedType.LPStr)]

}

private void button1_Click(object sender, System.EventArgs e)

{

IntPtr iAPHwnd;

COPYDATASTRUCT oCDS;

string sScript = "Application.Quit";

iAPHwnd = (IntPtr)FindWindow("AfxArcPad Class", null);

//MessageBox.Show(iAPHwnd.ToString()); //This works

oCDS.cbData = (sScript.Length * 2) + 1; //not sure if I needed to do this
for unicode - should allocate enough space in any case

oCDS.lpData = sScript.ToCharArray();

oCDS.dwData = (IntPtr) 1;

SendMessage(iAPHwnd, WM_COPYDATA, (IntPtr)null, ref oCDS); //get a managed
notsupportedexeception

}


Alex Feinman

unread,
Jan 7, 2003, 5:44:37 PM1/7/03
to
Unfortunately CF does not support marshalling of the complex structures. You
can marshal struct that consists of simple types, but not references or
pointers
If you insist, replace lpData definition with IntPtr lpData, then do this:

[DllImport("coredll.dll")]
private static extern IntPtr LocalAlloc(int flag, int size);
[DllImport("coredll.dll")]
private static extern IntPtr LocalFree(IntPtr p);

public struct COPYDATASTRUCT
{
public int dwData;
public int cbData;
public IntPtr lpData; //public IntPtr //[MarshalAs(UnmanagedType.LPStr)]
}

COPYDATASTRUCT oCDS = new COPYDATASTRUCT();
oCDS.cbData = (sScript.Length + 1) * 2;
oCDS.lpData = LocalAlloc(0x40, oCDS.cbData);
Marshal.Copy(sScript.ToCharArray(), 0, oCDS.lpData, sScript.Length);
oCDS.dwData = 1;
SendMessage(...)
LocalFree(oCDS.lpData);


"microsoft.public.windowsce.embedded" <sd...@sdfa.asd> wrote in message
news:e7fxPtptCHA.1776@TK2MSFTNGP09...

Chris Tacke, eMVP

unread,
Jan 7, 2003, 9:50:32 PM1/7/03
to
I'd consider that "supported", though the idea of letting the CLR handle
memory management goes out the window and you now have the potential for
memory leaks...

This is where it gets fun!

-Chris


"Alex Feinman" <publi...@alexfeinman.com> wrote in message
news:e1dUT5ptCHA.2492@TK2MSFTNGP10...

Alex Feinman

unread,
Jan 7, 2003, 10:34:06 PM1/7/03
to
Unfortunately CF has a limited support for marshalling of the complex
structures.

How's that? <g>

"Chris Tacke, eMVP" <cta...@NOinnovativedssSPAM.com> wrote in message
news:OQRnzCstCHA.2492@TK2MSFTNGP11...

0 new messages