InterOp problem

  • Thread starter Carsten Unterberg
  • Start date
C

Carsten Unterberg

Hi everyone,

i've got a problem with interop.

in this case, i've a c++-application for a mobile device which sends my
other application (.net cf) via SendNotifyMessage a wm_copydata message
with a own structure.

structure in c++

struct sData

{

public:

int id;

char szData[500];


public:

sData()

{

memset(this, NULL, sizeof(sData));

}

public:

void SetData(int iApp, char* pszData)

{


strcpy(szData, pszData);

id = sizeof(szData);

}

};



structure in c#
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential,CharSet
= System.Runtime.InteropServices.CharSet.Unicode)]

public struct sData

{


public System.Int32 id;

[System.Runtime.InteropServices.MarshalAs(
System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=500)]

public sbyte[] szData;

}



sending and receiving works without any problems. but when i try to parse
the incoming data with that:

COPYDATASTRUCT cdata =
(COPYDATASTRUCT)System.Runtime.InteropServices.Marshal.PtrToStructure(e.Message.LParam,
typeof(COPYDATASTRUCT));

sData data =
(sData)System.Runtime.InteropServices.Marshal.PtrToStructure(cdata.lpData,
typeof(sData));

has data.szData not the value which i give him in the c++-application.

maybe there is an error, but i don't know where.



Best Regards,



Carsten Unterberg | Test-Framework

http://test-framework.blogspot.com/
 
C

Carsten Unterberg

Hey Chris,

in c# the copydatastruct looks like:

public struct COPYDATASTRUCT

{

public System.Int32 dwData;

public System.Int32 cbData;

public System.IntPtr lpData;

}

When I receive a message, I could read the cbData-member without any
problems...



Best Regards,



Carsten Unterberg|Test-Framework

http://test-framework.blogspot.com/
 
C

Chris Tacke, MVP

My bet is that you're getting the wrong address or something. I'd debug it
this way:

in the C++ app dump the address of the structure you're sending.

in the C# app, use the memory viewer to verify that your actual data is at
that address. Then verify that the lpData of COPYDATASTRUCT is pointing to
the same place (not pointing to a pointer to that location). If those are
both true, then I'd just do a Marshal.Copy to get the data over to my app.

Though, personally, I'd not use WM_COPY in the first place.

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
C

Carsten Unterberg

Hey Chris,

thanks at first for your fast answer. I am just a newbie in c++ and I have
to get some information from a c++ component and send them to my .netcf
application. for sure, there are several other ways, but for me, the way
with SendNotifyMessage/WM_COPYDATA seems to be the easiest...

I will try it with Marshal.Copy, it's new for me as well.

Best regards,

Carsten Unterberg|Test-Framework
http://test-framework.blogspot.com/
 
C

Carsten Unterberg

Hey Chris,

thanks for you help. I tried it times and times without any success :-(
Where was no way to make it work. Later than I tried an other sending
function... Instead of SendNotifyMessage() I used the well know
SendMessage() and bingo.. it works fine. The MSDN documentation says that
just PostMessage() won't work with WM_COPYDATA, but SendNotifyMessage()
won't work also.

Now, everything is fine..

Best regards,

Carsten Unterberg|Test-Framework
http://test-framework.blogspot.com/
 

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